HI,
I am a novice when i comes to arduino and programming, I just picked up mine about two weeks ago and am having some trouble wrapping my head around software debouncing. I've been trying to figure out what I'm doing wrong, it doesn't seem to make any difference no matter what i do with the code.
I have one button attached to pin 2 and an rgb led attached to pin 9 for red, 10 for green, and 11 for blue
the way my program is supposed to work goes like this:
I press the button and it should light up white (all three led's at once) and then fade out.
then if i press it a second time it should light up red then fade out, and the third press goes to green, and the fourth to blue, then back to white.
what happens is most of the times it skips 1 or more of the other colors, sometimes it skips all the way back to the one that just lit.
I'm sure I'm missing something simple here,
my code looks like this:
int trigger = 2;
int rled = 9;
int gled = 10;
int bled = 11;
int dimmingTimer = 2;
boolean lastButton = LOW;
boolean currentButton = LOW;
void setup ()
{
pinMode (trigger, INPUT);
pinMode (rled, OUTPUT);
pinMode (gled, OUTPUT);
pinMode (bled, OUTPUT);
}
//Debouce function
boolean debounce(boolean last)
{
boolean current = digitalRead(trigger);
if (last != current)
{
delay(5);
current = digitalRead(trigger);
}
return current;
}
void loop()
{
currentButton = debounce(lastButton);
if (lastButton == LOW && currentButton == HIGH)
for(int fadeValue = 255 ; fadeValue >= 0; fadeValue -=1)
{
// sets the value (range from 0 to 255):
analogWrite(rled, fadeValue);
analogWrite(gled, fadeValue);
analogWrite(bled, fadeValue);
// wait time between dimming steps
delay (dimmingTimer);
}
lastButton = currentButton;
currentButton = debounce(lastButton);
if (lastButton == LOW && currentButton == HIGH)
for(int fadeValue = 255 ; fadeValue >= 0; fadeValue -=1)
{
// sets the value (range from 0 to 255):
analogWrite(rled, fadeValue);
// wait time between dimming steps
delay (dimmingTimer);
}
lastButton = currentButton;
currentButton = debounce(lastButton);
if (lastButton == LOW && currentButton == HIGH)
for(int fadeValue = 255 ; fadeValue >= 0; fadeValue -=1)
{
// sets the value (range from 0 to 255):
analogWrite(gled, fadeValue);
// wait time between dimming steps
delay (dimmingTimer);
}
lastButton = currentButton;
currentButton = debounce(lastButton);
if (lastButton == LOW && currentButton == HIGH)
for(int fadeValue = 255 ; fadeValue >= 0; fadeValue -=1)
{
// sets the value (range from 0 to 255):
analogWrite(bled, fadeValue);
// wait time between dimming steps
delay (dimmingTimer);
}
lastButton = currentButton;
}
my button is pulled down to ground wit a 10k resisterand pulls high to 5v when pressed, and I am lighting up a a 12v led strip by controlling 3 mosfetts with pwm signal.
Any help would be much appreciated. Thank you in advance