HOW TO LOOP " IF" UNTIL CONDITION MET? AND THEN RUN MAIN LOOP

Hello and thanks for taking a look at my question

I am using the code below to flash large self powered LED fixtures with MOSFETs as switches to simulate a lightning effect for Halloween display.

I need to have the code only run when a INPUT pin is LOW other wise loop until the condition is met.

I have tried to use "if" and "continue" in the code below but not having any luck.

I am new to coding and trying to learn the syntax for Arduino. I'm using a mega board for the project.

If someone could share their expertise and experience it would be so welcome.

I know for most this is a simple problem, but I am getting a bald spot where I'm pulling my hair out.

Thanks for any help.

/*
* Blink_Randomly
*
* Modified from the basic Arduino example.  Turns an LED on for a random time,
* then off for a (most likely) different random time.  We use pin 13 because,
* depending on your Arduino board, it has either a built-in LED
* or a built-in resistor so that you need only an LED.
*
* Original at - http://www.arduino.cc/en/Tutorial/Blink
*/



int readPin = 12;
int ledPin = 13;                  // LED connected to digital pin 13
long randOn = 0;                  // Initialize a variable for the ON time
long randOff = 0;                 // Initialize a variable for the OFF time


void setup()                      // run once, when the sketch starts
{
  
 randomSeed (analogRead (0));    // randomize
 pinMode(ledPin, OUTPUT);    // sets the digital pin as output
 pinMode(readPin, INPUT);
}

void loop(){

   if (digitalRead(readPin) == true){
    delay(1)
   if (digitalRead(readPin) == false)
   (continue);
}


{
// NEED TO CHECK READPIN IF "TRUE" DO NOTHING. IF FALSE RUN MAIN LOOP. 


}
        
  
  randOn = random (5, 50);    // generate ON time between 0.1 and 1.2 seconds
  randOff = random (70, 250);    // generate OFF time between 0.2 and 0.9 seconds
   digitalWrite(ledPin, HIGH);   // sets the LED on
   delay(randOn);                // waits for a random time while ON
   digitalWrite(ledPin, LOW);    // sets the LED off
   delay(randOff);               // waits for a random time while OFF
}

Nothing wrong with bald.

What do you think is going to happen here?

 if (digitalRead(readPin) == true){
    delay(1)
   if (digitalRead(readPin) == false)
   (continue);
}

Are you sure about the comment? randOn = random (5, 50); // generate ON time between 0.1 and 1.2 seconds

I thought the" IF "statements would loop back until second IF was met? Then run the main loop.

The random time for "on" is fast for a lightning type flashes. offs are longer it makes a nice effect.

Thanks for taking a look!

What's up with your comments in the sketch?

The if() function doesn't loop anything. https://www.arduino.cc/en/Reference/If

You could loop using 'while' or 'do/while'

What does this do?

int readPin = 12;
int ledPin = 13;                  // LED connected to digital pin 13
long randOn = 0;                  // Initialize a variable for the ON time
long randOff = 0;                 // Initialize a variable for the OFF time


void setup()                      // run once, when the sketch starts
{
  randomSeed (analogRead (0));    // randomize
  pinMode(ledPin, OUTPUT);        // sets the digital pin as output
  pinMode(readPin, INPUT_PULLUP);
}

void loop()
{

  if (digitalRead(readPin) == LOW)
  {
    randOn = random (5, 50);      // generate ON time between 0.1 and 1.2 seconds
    randOff = random (70, 250);   // generate OFF time between 0.2 and 0.9 seconds
    digitalWrite(ledPin, HIGH);   // sets the LED on
    delay(randOn);                // waits for a random time while ON
    digitalWrite(ledPin, LOW);    // sets the LED off
    delay(randOff);               // waits for a random time while OFF
  }
}

Take a look how project is implemented in steps and pseudo code. You are on right track putting comments into code , just need to maintain the desired flow.

Just FYI - loop() runs "forever" by itself, your "if" only alters the flow temporary into "do nothing" task.

project task - base loop void loop() { // NEED TO CHECK READPIN IF "TRUE" DO NOTHING. IF FALSE RUN MAIN LOOP. } pseudocode loop void loop() { // NEED TO CHECK READPIN IF "TRUE" DO NOTHING. IF FALSE RUN MAIN LOOP. // if input pin true // do nothing //else // continue main loop } new loop void loop() { // NEED TO CHECK READPIN IF "TRUE" DO NOTHING. IF FALSE RUN MAIN LOOP. // if read input pin true if (digitalRead(readPin) == true){ // do nothing } //else implies readPin was indeed false // continue main loop }

I thought the" IF "statements would loop back until second IF was met? Then run the main loop.

An if statement does NOT loop. A while statement does. You have to use the correct kind of statement.

Try:

void loop()
{
  while(digitalRead(readPin))
    digitalWrite(ledPin, LOW);
    
 randOn = random (5, 50);    // generate ON time between 0.1 and 1.2 seconds
 randOff = random (70, 250);    // generate OFF time between 0.2 and 0.9 seconds
  digitalWrite(ledPin, HIGH);   // sets the LED on
  delay(randOn);                // waits for a random time while ON
  digitalWrite(ledPin, LOW);    // sets the LED off
  delay(randOff);               // waits for a random time while OFF
}

Thanks for all the help.

The code works great. :)