Created
May 14, 2018 23:05
-
-
Save jgillick/f8fc7722f2c0d29d82b955d190b8b755 to your computer and use it in GitHub Desktop.
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
// THIS GIST HAS NOT BEEN COMPILED OR TESTED | |
/** | |
* Using the arduino-LEDFader library, this will fade an LED | |
* connected to pin 3 up, down, up and then hold that brightness | |
* for 10 minutes before fading the LED again. | |
* | |
* This is similar to the other gist (https://gist.github.com/jgillick/b2268084d656baf4ba0149e2f4c6a008) | |
* however, it does not block the processor when the brightness is being held. | |
*/ | |
#include <LEDFader.h> | |
#define HOLD_PWM 200 // The brightness to hold the LED at between breathing. | |
#define HOLD_TIME 600000 // How long to hold the LED between breathing. | |
#define FADE_TIME 3000 // The number of milliseconds each fade should take. | |
// Every time a fade completes, the program will move to the next step. | |
#define STEP_FADE_1 1 // fade all the way up | |
#define STEP_FADE_2 2 // fade all the way down | |
#define STEP_FADE_3 3 // fade up to HOLD_PWM brightness | |
#define STEP_HOLD 4 // hold this brightness for HOLD_TIME | |
LEDFader led = LEDFader(3); | |
uint8_t step = 0; | |
uint8_t is_holding = 0; | |
uint16_t hold_until = 0; | |
void setup() { | |
led.set_value(0); | |
} | |
void loop() { | |
led.update(); | |
// Transition to the next step when fading is done | |
if (led.is_fading() == false) { | |
// Currently holding the brightness | |
if (is_holding) { | |
// All done | |
if (millis() >= hold_until) { | |
hold_until = 0; | |
is_holding = 0; | |
step = STEP_FADE_1; | |
} | |
// Still holding | |
else { | |
return; | |
} | |
} | |
change_step(); | |
} | |
} | |
void change_step() { | |
if (is_holding) { | |
return; | |
} | |
step += 1; | |
switch(step) { | |
// Fade up | |
case STEP_FADE_1: | |
led.fade(255, FADE_TIME); | |
break; | |
// Fade down | |
case STEP_FADE_2: | |
led.fade(0, FADE_TIME); | |
break; | |
// Fade to hold brightness | |
case STEP_FADE_3: | |
led.fade(HOLD_PWM, FADE_TIME); | |
break; | |
// Hold it for HOLD_TIME milliseconds | |
case STEP_HOLD: | |
is_holding = 1; | |
hold_until = millis() + HOLD_TIME; | |
break; | |
} | |
if (step >= STEP_HOLD) { | |
step = STEP_FADE_1; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment