Hi!
I have tried this excellent code from UKHeliBob with some modifications
UKHeliBob:
Here is an extended example of a state machine controlling LEDsconst byte ledPins[] = {5, 6, 9, 10};
byte LEDCOUNT = sizeof(ledPins) / sizeof(ledPins[0]);
byte ledIndex = 0;
const unsigned long FIVESECS = 5000;
unsigned long currentTime;
unsigned long startTime;
enum states
{
ALL_FADE_UP_TOGETHER,
ALL_ON_FOR_5_SECS,
...
...
What I want:
Im am running the machine all the time and by pass state "Do nothing";
- ALL_FADE_UP_TOGETHER,
- ALL_ON_FOR_5_SECS,
- ALL_FADE_DOWN_TOGETHER,
- ALL_OFF_FOR_5_SECS,
No. 1 again and so on...
What happens:
Because the pwmLevel in state "ALL_FADE_DOWN_TOGHETER" is static when arduino runs the program second time it becomes 256, third 257 etcetera (or in my case -1, -2, depending on my connections).
I have tried
different IF-statements but gets a small flash before fading down.
Maybe there is standard solution to this?
case ALL_FADE_DOWN_TOGETHER:
// case ALL_FADE_UP_TOGETHER:
{
static int pwmLevel = 255;
static int pwmIncrement = 1;
unsigned long period = 7; // 7 egen
if (currentTime - startTime >= period)
{
if (pwmLevel == -1)
{
Serial.println("Fade down level -1: ");
Serial.println(pwmLevel);
pwmLevel = 255;
}
setPwmLevelForAllLeds(pwmLevel);
startTime = millis();
Serial.println("Fade down - period: ");
Serial.println(pwmLevel);
pwmLevel -= pwmIncrement;
if (pwmLevel == 0) // original <=
{
setPwmLevelForAllLeds(0); //all off
currentState = ALL_OFF_FOR_5_SECS;
startTime = currentTime;
setPwmLevelForAllLeds(0); //all off
}
}
}
break;
Best Regards Johan