Hello everyone. I have a project I'm working on where I have to use millis to do several things as to not interrupt a few led matrix and sensors.
What I'm trying to to is 2 separate things.
-
Blink an LED fast for 10 seconds, then Turn OFF and stop.
-
Fade and led ON, Stay ON for 3 seconds, then fade out and stay off for 10 seconds.
What I have so Far works for both BUT:
for #1) I can't figure out how to stop the Led after 10 seconds.
and #2) I can't get the led to do anything but fade in then fade out, and repeat.
Can anyone help explain how I'd do this?
#1): Blinks Led fast
// blink for 10 seconds then turn off
const int magicRed = 47;
int led4State = LOW;
long previousMillis4 = 0;
const long interval4 = 50;
void setup() {
pinMode(magicRed, OUTPUT);
}
void loop() {
magicRedPanic();
}
void magicRedPanic()
{
// blink the red LED.
unsigned long currentMillis4 = millis();
if (currentMillis4 - previousMillis4 > interval4) {
previousMillis4 = currentMillis4;
// if the LED is off turn it on and vice-versa:
if (led4State == LOW)
{
led4State = HIGH;
}
else
{
led4State = LOW;
}
// set the LED with the ledState of the variable:
digitalWrite(magicRed, led4State);
}
}
#2): Fade in fade out
const byte magicWhite = 45;
#define UP 0
#define DOWN 1
// constants
const int minPWM = 0;
const int maxPWM = 255;
byte fadeDirection = UP;
int fadeValue = 0;
byte fadeIncrement = 5;
unsigned long previousFadeMillis;
int fadeInterval = 50;
void setup() {
analogWrite(magicWhite, fadeValue);
}
void loop() {
unsigned long currentMillis = millis();
magicWhiteFade(currentMillis);
}
void magicWhiteFade(unsigned long thisMillis) {
if (thisMillis - previousFadeMillis >= fadeInterval) {
if (fadeDirection == UP) {
fadeValue = fadeValue + fadeIncrement;
if (fadeValue >= maxPWM) {
fadeValue = maxPWM;
fadeDirection = DOWN;
}
} else {
fadeValue = fadeValue - fadeIncrement;
if (fadeValue <= minPWM) {
fadeValue = minPWM;
fadeDirection = UP;
}
}
analogWrite(magicWhite, fadeValue);
previousFadeMillis = thisMillis;
}
}