My code is designed to turn on an LED if the button is turned on when the timer checks in, keep it on until the next check in, and repeat. A momentary clicking of the button should NOT turn on the LED. The interval is currently 8 seconds to make the change easy to read.
In puedocode:
if (timer checks in && button is HIGH
turn LED ON
else if the timer has not checked in, but the LED was turned ON at the last check in
keep LED ON until next check in
else
keep LED OFF until next check in
My current code turns the LED on and keeps it on for the interval as desired, but will not wait until the interval is up to turn back on, allowing momentary button clicks to turn on the LED. What do I need to add to my if statements to keep momentary button clicks from affecting my results?
Do you REALLY need ints to store pin numbers? Does your Arduino HAVE that many pins? No, so quit wasting SRAM using ints to hold byte sized values.
Are your pins capable of being in more than 255 states? Off, a tiny bit on, a tiny bit more on, even more on, etc? No, so quit wasting SRAM using ints to hold byte sized values.
unsigned long previousMillis = 0;
So, this holds the last time millis happened? Why not use a name that means something? This names doesn't mean squat.
int buttonState = LOW;
void loop() {
// determine when the led should turn on
unsigned long currentMillis = millis();
//timer
int buttonState = digitalRead(buttonPin);
Good idea having two variables named buttonState. Even better would be three or four.
What do I need to add to my if statements to keep momentary button clicks from affecting my results?
Don't ADD anything. Split those overly-complex up if statements into nested if statements.
The current state of the switch doesn't mean anything if the LED is supposed to be on. So, don't do anything with respect to the switch state unless the time is right.
I don't know what you mean by "the timer checks in", since you are not using a timer. The millis() function tells you the time. It is NOT a timer, in that it does not automatically make things happen.