Boolean variable not changing within if statements that contain delay

Hello everyone,

I have a program that changes a boolean variable every time a button switch is pressed. When true an LED is turned on and vice versa when false, so in essence the button switch acts sort of of like a toggle switch. My problem is that when I introduced a delay function to make the LED blink when 'on.' With the delay functions the boolean variable remains true even when the button is pressed. Here is my code:

int buttonState = 0;
int buttonPin = 2;

boolean isOn = false;

void setup() {
  Serial.begin(9600);

  pinMode(3, OUTPUT);
  pinMode(buttonPin, INPUT);
  //digitalWrite(buttonPin, LOW);

}

void loop() {
  buttonState = digitalRead(buttonPin);

  if (buttonState == HIGH){
    isOn = !isOn;
  }
  
  Serial.println(isOn);

  if (isOn == true){
    digitalWrite(3, HIGH);
    delay(500);
    digitalWrite(3, LOW);
    delay(500);
  }
  else{
    digitalWrite(3, LOW);
  }

}

'delay()' is a 'blocking' function - nothing else can happen during the delay period, including reading a button. Take a look at "BlinkWithoutDelay" and this for a guide to 'millis()'-based timing.

Also, do you have a 'pulldown' resistor on the button input?
It's usually better to make it 'active-low', by setting the chip's internal pullup resistor then connecting the button between the pin and ground. Then, in your code, the button has been pressed when the pin goes low. Then no external resistor is necessary. (An internal pullup resistor is in the range 30K to 50K, from memory.)
To turn on the internal pullup for a pin:-

pinMode(pin, INPUT_PULLUP);

Also, a search on debouncing a button press will be helpful. Maybe use "Arduino button debounce" as a search term.

Yup.

While it is easy to create a blinking LED with the delay() function, and many sketches use short delays for such tasks as switch debouncing, the use of delay() in a sketch has significant drawbacks. No other reading of sensors, mathematical calculations, or pin manipulation can go on during the delay function, so in effect, it brings most other activity to a halt.

John_Everything:
Yup.

I forgot to add, "BlinkWithoutDelay" is an example included with the IDE.

'millis()'-based timing is harder to implement, but it's an essential that you must learn for Arduino programming, to enable you to effectively do multiple things at once, and keep the 'loop()' spinning over quickly.

Hello, just an extra reply with a different solution.

I have an application where I actually want to just use a plain old delay because I'm stubborn.

A DIFFERENT issue that I'm having is that sometimes good old arduino will just "forget" to change my boolean variable for whatever reason. I think it may have to do with too many things happening in the RAM, or maybe my particular program just doesn't like assigning a value to a variable while changing the value of that variable. It SHOULD be able to accomplish this with the first method (see code) but here we are...

I have a feeling it's something in my coding strategies. NEVERTHELESS I have found a solution that at least works for me.

void setup()
{

  bool temp_bool; //use for bool switching
  bool var; // var to switch

}

void loop()
{

  var = !var; //doesnt always work

  temp_bool = !var;
  var = temp_bool; // this does work

}

kaakrc:
Hello, just an extra reply with a different solution.

I have an application where I actually want to just use a plain old delay because I'm stubborn.

A DIFFERENT issue that I'm having is that sometimes good old arduino will just "forget" to change my boolean variable for whatever reason. I think it may have to do with too many things happening in the RAM, or maybe my particular program just doesn't like assigning a value to a variable while changing the value of that variable. It SHOULD be able to accomplish this with the first method (see code) but here we are...

I have a feeling it's something in my coding strategies. NEVERTHELESS I have found a solution that at least works for me.

void setup()

{

bool temp_bool; //use for bool switching
  bool var; // var to switch

}

void loop()
{

var = !var; //doesnt always work

temp_bool = !var;
  var = temp_bool; // this does work

}

Nope. var and temp_bool are local to setup(). They cannot be seen in loop(). This won't even compile.

Post an actual sketch that demonstrates the issue.

kaakrc:
A DIFFERENT issue that I'm having is that sometimes good old arduino will just "forget" to change my boolean variable for whatever reason.

Nope. The Arduino doesn't work that way. It is doing what you told it to do, apparently not what you want it to do. If you show your problem sketch then we can probably explain why it wasn't working as you wanted, and maybe explain why your changes cause it to work.
Your problem is probably similar to the OP's: You have to be holding the button down at the instant the delays end to toggle the blink OFF but then you have to release the button in the few microseconds before it is checked again to keep from toggling the blink back ON again. That makes it nearly impossible to turn the blinking off.