Everything I have been reading about void setup() says its run one time at the start of the program.
But I'm working on a code example that appears to continually use the code in void setup().
I have not found an example or explanation that shows/tells me how this works.
in the code example below, the boolean debounce function seems to be run on each loop.
I'm not fully understanding how or why if the code in void setup() is only run once at the beginning
of the program. The debounce function would need to be looked at in every loop. Why is the debounce
function in void setup() and not part of the void loop()?
[quote
// Ch2-5 Jeremy Blum
// edits and tweeks by me..
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);
}
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);
}
[/quote]