Hi everyone, Im very new to arduino and have a function question, I took a C programming class a couple years ago and never understood this (some how i passed).
here is the code im working with (its a debounce on/off button) Im trying to understand.
const int led=9;
const int button=2;
boolean lastbutton = LOW;
boolean currentbutton = LOW;
boolean ledon = false;
void setup()
{
pinMode(led, OUTPUT);
pinMode(button, INPUT);
}
// start debounce
boolean debounce(boolean last)
{
boolean current = digitalRead(button);
if (last != current)
{
delay(5);
current = digitalRead(button);
return current;
}
}
void loop()
{
currentbutton = debounce(lastbutton);
if (lastbutton == LOW && currentbutton == HIGH)
{
ledon = !ledon;
}
lastbutton = currentbutton;
digitalWrite(led, ledon);
}
basically what im trying to understand is the "return current" and then down in the loop "debounce(lastbutton)"
so from what im getting here is....
we have in the debounce function "debounce(boolean last)"
so because its returning "current" at the end of the function does "last" then become "current"?
next question, in the loop now we have "currentButton = debounce(lastbutton)"
so is "lastbutton" set to the value of "last" inside the function or is it the other way around eg. "last" in the function takes the value of "lastbutton"?
complicated question here for me so im not sure im asking the correct question.
If some one can help me figure this out cause its driving me mental that would be awesome.