hi. i have this code here:
int counter = 0;
long randNumber;
long randNumber2;
int ledPin = 9;
int buttonPin = 13;
void setup()
{
pinMode(buttonPin, INPUT);
pinMode(ledPin, OUTPUT);
}
void loop()
{
//Handle input
int switchVal = digitalRead(buttonPin);
if(switchVal == HIGH)
{
delay(500);
counter ++;
//Reset count if over max mode number
if(counter == 5)
{
counter = 0;
}
}
else
//Change mode
switch (counter) {
case 1:
// fade in from min to max in increments of 5 points:
for(int fadeValue = 0 ; fadeValue <= 255; fadeValue +=5) {
// sets the value (range from 0 to 255):
analogWrite(ledPin, fadeValue);
// wait for 30 milliseconds to see the dimming effect
delay(20);
}
// fade out from max to min in increments of 5 points:
for(int fadeValue = 255 ; fadeValue >= 0; fadeValue -=5) {
// sets the value (range from 0 to 255):
analogWrite(ledPin, fadeValue);
// wait for 30 milliseconds to see the dimming effect
delay(20);
}
break;
case 2:
randNumber = random(300);
randNumber2 = random(300);
digitalWrite(ledPin, 255);
delay(randNumber);
digitalWrite(ledPin, 0);
delay(randNumber2);
break;
case 3:
digitalWrite(ledPin, 255);
break;
case 4:
digitalWrite(ledPin, 0);
break;
}
}
it changes the led modes upon button push and it works fine, except for one thing. it only registers the button push when the loop is through (for example when the fade goes back to 0 before it restarts to dim).
i would like to have the arduino register the push of the button at any time without having to hold the button till the fade is through and it registers the push. just one short push of the button at any given time and the mode should proceed to the next.
another small problem which i dont understand is that the modes are: fade, random blink, on, off. but after on i have to push twice to get to fade. tried some but wasnt able to fix it.
thanks in advance.