Hi All ,
Looking for some help with my code please .
What I Want It To Do :
Nano powers up
4 LEDs fade in
1 red LED stays at full brightness for 2 seconds
3 LEDs pulse dim to bright randomly for 2 seconds after fade in
All LEDs fade out
A blue LED fades in
Blue LED stays at full brightness for 2 seconds
Blue LED fades out
Sketch loops again until power is turned off
What It Does :
Nano powers up
4 LEDs fade in
1 red LED stays at full brightness for 2 seconds
*Here is the problem *
The 3 LEDs pulse randomly until they reach full brightness then they stay on for 2 seconds instead of pulsing dim to bright randomly for 2 seconds after fade in
Everything after the above step does what is expected
All LEDs fade out
A blue LED fades in
Blue LED stays at full brightness for 2 seconds
Blue LED fades to black
Code loops again until power is turned off
I think the problem is the “ if ( ledLevel == 255 ) “ but I don’t know what to do to separate the pulsing LEDs from the static blue and red LEDs .
Code attached below , thanks for reading ,
Tom
[code]
// Hot / Cold try continuous LED pulsing during HOT state
enum
{
FADE_IN_HOT,
WAIT_HOT ,
FADE_OUT_HOT,
FADE_IN_COOL,
WAIT_COOL ,
FADE_OUT_COOL,
} state;
unsigned long previousMillis;
unsigned long currentMillis;
unsigned long fadeInTime = 15;
unsigned long fadeOutTime = 15;
unsigned long waitHot = 2000;
unsigned long waitCool = 2000;
unsigned int ledLevel = 0;
const int ember1Pin = 11;
const int ember2Pin = 10;
const int ember3Pin = 9;
const int redPin = 5;
const int bluePin = 3;
void setup() {
// put your setup code here, to run once:
pinMode( ember1Pin, OUTPUT);
pinMode( ember2Pin, OUTPUT);
pinMode( ember3Pin, OUTPUT);
pinMode( redPin, OUTPUT);
pinMode( bluePin, OUTPUT);
}
void loop() {
unsigned long currentMillis = millis();
switch (state)
{
case FADE_IN_HOT:
if (currentMillis - previousMillis >= fadeInTime)//time to change LED level
{
ledLevel++;
analogWrite(redPin, ledLevel);
analogWrite(ember1Pin, random(120) + 135);
analogWrite(ember2Pin, random(120) + 135);
analogWrite(ember3Pin, random(120) + 135);
if (ledLevel == 255)
{
state = WAIT_HOT;
}
previousMillis = currentMillis;
}
break;
case WAIT_HOT:
if (currentMillis - previousMillis >= waitHot)
{
state = FADE_OUT_HOT;
previousMillis = currentMillis;
}
break;
case FADE_OUT_HOT:
if (currentMillis - previousMillis >= fadeOutTime)//time to change LED level
{
ledLevel--;
analogWrite(redPin, ledLevel);
analogWrite(ember1Pin,ledLevel);
analogWrite(ember2Pin, ledLevel);
analogWrite(ember3Pin, ledLevel);
if (ledLevel == 0)
{
state = FADE_IN_COOL;
}
previousMillis = currentMillis;
}
break ;
case FADE_IN_COOL:
if (currentMillis - previousMillis >= fadeInTime)//time to change LED level
{
ledLevel++;
analogWrite(bluePin, ledLevel);
if (ledLevel == 255)
{
state = WAIT_COOL;
}
previousMillis = currentMillis;
}
break;
case WAIT_COOL:
if (currentMillis - previousMillis >= waitCool)
{
state = FADE_OUT_COOL;
previousMillis = currentMillis;
}
break;
case FADE_OUT_COOL:
if (currentMillis - previousMillis >= fadeOutTime)//time to change LED level
{
ledLevel--;
analogWrite(bluePin, ledLevel);
if (ledLevel == 0)
{
state = FADE_IN_HOT;
}
previousMillis = currentMillis;
}
break ;
}
}
[/code]