I wrote simple function to changed led state
void changeLedState(boolean led)
{
led = !led;
}
and pass it in the main loop to change led state once pin read HIGH after switch is pressed
void loop() {
currentState = debounce(lastState);
if (lastState == LOW && currentState == HIGH)
{
changeLedState(ledState); // ledState initialized globally to LOW
}
lastState = currentState;
digitalWrite (ledPin, ledState);
}
I am not sure why the code doesn't work as led does not change state on pressing switch.
I manage to write function that works
void changeLedState()
{
led = !led;
}
void loop() {
// put your main code here, to run repeatedly:
currentState = debounce(lastState);
if (lastState == LOW && currentState == HIGH)
{
changeLedState();
}
lastState = currentState;
digitalWrite (ledPin, ledState);
}
Just curious to know why the function did not work the first time.