Created
June 5, 2017 13:58
-
-
Save ItKindaWorks/512be9da4c6c6a2e61c99f48eaacb4f9 to your computer and use it in GitHub Desktop.
A small program that uses the Metro library to fade one LED while blinking another
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <Metro.h> // Include Metro library | |
const int ledPin1 = 13; | |
const int ledPin2 = 10; | |
// Create variables to hold the LED states | |
boolean ledState = HIGH; //boolean variables only hold 2 values, true or false (also equal to high or low on Arduino) | |
int ledBrightness = 0; | |
int fadeAmount = 5; //the amount to fade each time we execute a fade | |
//create metro objects, one for each action we want to do (one for blink and one for fade) | |
Metro blinkMetro = Metro(1000); | |
Metro fadeMetro = Metro(20); | |
void setup(){ | |
//set our ledPins as outputs | |
pinMode(ledPin1,OUTPUT); | |
pinMode(ledPin2,OUTPUT); | |
} | |
void loop(){ | |
if (blinkMetro.check()) { //check the blink timer to see if we need to blink | |
ledState = !ledState; //flip the value of ledState (if it was high/true, set it to low/false, and vise versa) | |
digitalWrite(ledPin1, ledState); //change the state of the led pin | |
} | |
if (fadeMetro2.check()) { //check the fade timer to see if we need to fade | |
ledBrightness2 = ledBrightness2 + fadeAmount2; // change the brightness value for next time through the loop by incrementing the current brightness by fadeAmount | |
// reverse the direction of the fading at the ends of the fade (ie if fade amount is 5 while fading up, make it -5 while fading down): | |
if (ledBrightness2 <= 0 || ledBrightness2 >= 255) { | |
fadeAmount2 = -fadeAmount2 ; | |
} | |
analogWrite(ledPin3, ledBrightnes2); // set the brightness of pin 20 | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment