Can't figure out the for loop inside an if loop

Hello everyone. I am trying to create a "for" loop inside an "if" statement which I fail to have executed even when the condition for the if statement is met. I need someone experienced than me to have a look and give me an insight for why this may be happening. This program will work for counting to 30 when a button is pressed and do something particular when that time interval ends. Can you please have a look and help me out? I am fairly sure that the wiring is OK. I'll try to attach the code below.

Thank you for taking your time to read this,
Cheers

const char buttonPin = 11; // D8 on Arduino Nano
bool pressed = false; //Created a boolean for a clear code look
void setup() {
  // put your setup code here, to run once:
  pinMode(buttonPin, INPUT_PULLUP);
  Serial.begin(9600);
}

void loop() {
  // put your main code here, to run repeatedly:
  bool buttonState = digitalRead(buttonPin);
  if (buttonState == pressed){
    for (int i = 0; i <= 30; i++){ //Can't enter this loop, even when bool pressed = true
      Serial.print("Counting, i=\t");
      delay(1000);
    }
    Serial.print("Time to open the door\n"); // I need this to only execute once for each time the for loop has ended
  }else{
  Serial.print("No interaction\n"); //This is just a serial print for validation of functioning
  delay(1000);
  }
}
    for (int i = 0; i <= 30; i++);

The only code that the for loop will execute is the semicolon on the end of the line

Delete it

See an issue there at the end of the line?

You meant:

    for (int i = 0; i <= 30; i++) { //Can't enter this loop, even when bool pressed = true
      

And then the issue is edited out of the op.

With the edit does the code work now?

Nope. That I recognized and tried to edit without seeing any comments. The problem still persists without the semicolon. Would love any other feedback

Thank you for the reply. I still have the same issue when it is deleted

Thank you for the answer. Seeing the same output still

does the A print?

are you holding down the button for longer than 2 seconds?

The time it takes the CPU to execute the instructions:

is very tiny. Against the time the routine is put to sleep, up to 2 seconds. During the sleep time, the program will not detect a button push.

No sadly the A does not print I also tried that. And yes, at this point I think I had held the button for longer than 2 seconds, shorter than 2 seconds, also probably exactly two seconds. I have been trying for a while now...

I was replacing all the wiring with newer cables, another push button but still no luck. I am certain that this must be a very simple issue that I miss since I lack the experience

tried that?

Untested here, but maybe try this -

const uint8_t buttonPin = 11; // D8 on Arduino Nano

void setup() 
{
  // put your setup code here, to run once:
  pinMode(buttonPin, INPUT_PULLUP);
  Serial.begin(9600);
}

void loop() 
{
  // put your main code here, to run repeatedly:
  bool buttonState = digitalRead(buttonPin);
  if (buttonState == false)                     // false == zero (0)           
    {
        for (int i = 0; i <= 30; i++)
        { //Can't enter this loop, even when bool pressed = true
          Serial.print("Counting ");
          Serial.println(i);
          delay(1000);
        }
        Serial.print("Time to open the door\n"); // I need this to only execute once for each time the for loop has ended
    }
    else
    {
          Serial.print("No interaction\n"); //This is just a serial print for validation of functioning
          delay(1000);
    }
}

I think it always returns 1 because of the INPUT_PULLUP. I tried to change the orientation of my 4 legged push button (figured it might be the case) but still it returned 1 even when the button is pressed. One of the switching legs is connected to D8 (11) and the other is connected to the GND.

Thanks for the reply. Couldn't get it to work

It will always return 1 (true) while the button is not pressed (because of the pull up) If you press the button for longer than one second, your for loop should activate. One other thing to check with a multimeter is - are you absolutely certain you are connecting to the correct pair of pins on the switch?

No I am not. That's why I try to run each solution offered by using the 2 possible orientations of the switch. One orientation should have worked, shouldn't it?

Use a multimeter to make sure you have the correct pair of pins selected and operate the switch to ensure that it's working? Then we can be sure the switch (and your connections to it) are ok ?

I don't have a multimeter right now. I will try to check it tomorrow. Thank you for helping, much appreciated.

If I cannot solve this by tomorrow I will post my updates here around these hours. Hope you good people will be available

Cheers

For purposes of troubleshooting, substitute a wire for the switch.

const char buttonPin = 11; // D8 on Arduino Nano
//bool pressed = false; //Created a boolean for a clear code look
const byte statusLED = 13;

void setup() 
{
  // put your setup code here, to run once:
  pinMode(buttonPin, INPUT_PULLUP);
  Serial.begin(19200);
  pinMode(statusLED, OUTPUT);
  digitalWrite(statusLED, LOW);
}

void loop() 
{
  // put your main code here, to run repeatedly:
  byte buttonState = digitalRead(buttonPin);
  if (!buttonState)
  {
    digitalWrite(statusLED, HIGH);
    for (int idx = 0; idx <= 30; idx++)
    { 
      //Can't enter this loop, even when bool pressed = true
      Serial.print("Counting ");
      Serial.println(idx);
      delay(500);
    }
    Serial.println("Time to open the door");
    digitalWrite(statusLED, LOW);
  }
  //else
  //{
  //  Serial.print("No interaction\n"); //This is just a serial print for validation of functioning
  //  delay(500);
  //}
}

Excuse me I could not understand. Should I use an LED to see if the button works?