Looping a set of functions for x times - under an "if" condition

Basically, when the "if" condition is true, I would like all of the following to occur at once:

  • An LCD display will show text
  • An LED will blink on and off at regular intervals
  • A speaker will alternate between two notes, also at regular intervals.
  • All of this will occur for x times before stopping.

Here's a simplified version of the code:

int value; void setup () {}
void loop() {

if (value >= 10 ){
    for (int x = 0; x<500: x++); {           

      // LCD
      lcd.clear();
      lcd.home();
      lcd.print("WARNING:");
      lcd.setCursor(0,1);
      lcd.print("Intruder Alert");
      

      // LED
      if (timerLED > intervalLED1) {
        timerLED -= intervalLED1;         //reset timer
        digitalWrite(LEDPin, !digitalRead(LEDPin));
      }

      
      // SPEAKER
      switch(note)
     {
      case 0:       //for first note
          tone (SpeakerPin,262,250);
          break;
      case 1:      //for second note
          tone (SpeakerPin,310,250);
          break;
     }
     if (timerMUSIC >= intervalMUSIC1)
     {
       timerMUSIC -= intervalMUSIC1;
       note = !note;
     }


}
}
}     // E N D

What I get is this instead: [youtube video]

I think it's because all the desired actions (I'll refer to them as DA) are placed in a loop function under an if statement, hence once the if statement becomes false the entire DA will be skipped, causing the sudden stops as seen in the video.

I've tried placing the DA into a separate function and getting the "if" statement to call that function, but somehow none of the DA will be executed in that situation, even when the "if" statement is true.

How do I make the DA repeat x no. of times the moment the if statement is true? Thanks in advance.

Delta_G:

for (int x = 0; x<500: x++);

This says, do nothing 500 times. Lose the semicolon at the end.

Thanks for spotting that mistake! I dropped the semicolon and indeed, it's working as it should now. Thank you so much for the help! :slight_smile:

My only query left is why the code won't work when I rewrite the if statement to call a function, as shown below. Shifting the "for..." statement into the if statement (under void loop) doesn't work either.
Nothing is being executed when the if statement is fulfilled.

void loop() {

if ( ((millis()) >= 5000) &&  ((newValue - oldValue) >= 10) ){             
  void touchSensorActivated();
}
}


void touchSensorActivated(){
    for (int x = 0; x<300; x++) {

     --snip-- 
     } 
}
if ( ((millis()) >= 5000) &&  ((newValue - oldValue) >= 10) ){             
  void touchSensorActivated();
}

That's an unusual place to put a function prototype.