Hi All ,
I am playing around with adding a random LED flicker in a switch case . I have added a random flickering LED effect in Case 3 and I am happy with the effect . My problem happens when I push the button again to go to Case 4 , the LEDs continue doing their effect when I would like them to stop . I think the problem is the delay in Case 3 , how would I replace the delay with a random millis ? Would that stop the effect in Case 4 with next button push ?
Thanks .
[code]
// Experiment with random in switch case
// LEDS
int led1 = 11; // PWM Dim
int led2 = 10; // PWM Dim
int led3 = 9; // PWM Dim
int led4 = 6; // PWM Dim
int led5 = 5; // PWM Dim
//Button
int button1Pin = 7;//
int state = 0 ;
int old = 0 ;
int buttonPoll = 0 ;
void setup() {
pinMode(button1Pin, INPUT_PULLUP);
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(led3, OUTPUT);
pinMode(led4, OUTPUT);
pinMode(led5, OUTPUT);
}
void loop() {
// read the state of the pushbutton value:
buttonPoll = digitalRead(button1Pin);
if (buttonPoll == 1) {
delay (50);
buttonPoll = digitalRead(button1Pin);
if (buttonPoll == 0) {
state = old + 1 ;
}
}
else {
delay (100) ;
}
switch (state) {
case 1 :
analogWrite(led4, 200);
old = state ;
break;
case 2 :
analogWrite(led5, 200);
analogWrite(led4, 0);
old = state ;
break;
case 3 :
analogWrite(led4, 200);
analogWrite(led5, 200);
analogWrite(led1, random(120) + 135);
analogWrite(led2, random(120) + 135);
analogWrite(led3, random(120) + 135);
delay(random(100));
old = 0 ;
break;
case 4 :
analogWrite(led4, 0);
analogWrite(led5, 0);
old = 0 ;
break;
default :
analogWrite(led1, 0);
analogWrite(led2, 0);
analogWrite(led3, 0);
analogWrite(led4, 0);
analogWrite(led5, 0);
old = 0 ;
break;
//}
//}
}
}
[/code]