Another 'For loop' problem

I’m having trouble with ‘for’ loops. Can’t even get the most basic one to run properly in void loop.

I’ve been struggling with this for quite some time. Have read all the forums, studied the samples, and googled it. Can’t figure out why this doesn’t work in the void loop. Please help!

It works fine in void setup, but runs forever in void loop.

I know stuff is supposed to run forever in the void loop, but I’m sure I’ve seen many samples where the ‘for’ loop runs and stops in the void loop (haven’t I??). I mean that’s where you put many of them, correct?

void setup() {
  pinMode(13, OUTPUT);

  for (int a = 1; a < 5; a++) {
    digitalWrite(13, HIGH);
    delay(300);
    digitalWrite(13, LOW);
    delay(300);
  }
}

void loop() {

}
void setup() {
  pinMode(13, OUTPUT);
}

void loop() {
  for (int a = 1; a < 5; a++) {
    digitalWrite(13, HIGH);
    delay(300);
    digitalWrite(13, LOW);
    delay(300);
  }
}

You want it just run once? Put in a 'flag:

byte flag = 0;
void setup(){
// setup code
}

void loop(){
  if (flag == 0){
  // your blink code
  flag = 1;
  }
}

The Setup() function is called once, when the Arduino is rebooted or powered on.

The Loop() function itself is called by the Arduino System repeatedly. Your Loop() function is ending properly, but the Arduino is restarting it over and over as Long as the power is on. That's the way the Arduino is designed to work.

Haha! Very cool! Thanks for the quick reply! And thanks to you experts for all your contributions. It's amazing how easy things are when you know what you're doing, and how hard the easy fixes can be to find for rookies!

The flag idea worked in my simple example. But I’m having bigger problems.

My ‘master plan’ –

If somebody steps in front of a big Halloween spider I built, a photocell embedded in the spider activates causing two motors to spin very briefly and then stop. (they jiggle his legs). The program should then wait for person to step out of photocell range to reset and make it ready for another person to step in front of it to jiggle the legs again.

If they step in front of the spider again, motors spin again, etc, etc, etc. I have used LEDs for my trial program.

The program idea is to have the photo cell monitoring loop run forever to monitor light levels. So this loops forever – (‘if (motionSensorState == 1)’

But the other routine that drives two motors should only fire for 5 ‘jiggles’ each time someone fires the photocell.

I just can’t figure out how to make this work. Is it possible for you experts to help me figure this out?

byte flag = 0;            //flag to stop loop in void loop

int motionSensor = 8;     // motion sensor inputs to pin 8
int motorControl = 9;     // for use later
int motorControl_1 = 10;  // output to motor 1
int motorControl_2 = 11;  // output to motor 2



void setup() {
  pinMode(motionSensor, INPUT);
  pinMode(motorControl_1, OUTPUT);
  pinMode(motorControl_2, OUTPUT);
}

void loop() {
  int motionSensorState = digitalRead(motionSensor);  // to read motion sensor state

  if (motionSensorState == 1)                                    // if someone steps in front of photocell it goes high
    if (flag == 0) {
      for (int a = 1; a < 6; a++)                                  // cycle the little motors 5 times
      {
        digitalWrite(motorControl_1, HIGH);                 
        delay(50);
        digitalWrite(motorControl_2, HIGH);
        delay(50);
        digitalWrite(motorControl_1, LOW);
        delay(50);
        digitalWrite(motorControl_2, LOW);
        flag = 1;                                                           //stop this routine from running continually
      }
    }
    else
    {
                     // sit idle and wait for someone to step in front of photocell (I don't know what to put here)
    }

}

You need to detect when the motion sensor state becomes 1 rather than when it is 1. This will prevent it triggering continuously. Look at the StateChangeDetection example in the ID to see how to do it.

Thanks for that UKHeliBob! I’m making progress.

Currently I’m using LEDs in place of motors.

This program starts up with both LEDs lit. They should be off.

Making the photocell go dark causes the sequence to run once properly, but…

-Will not run again until reset manually
-Leaves both LEDs lit
-Nothing prints to the serial monitor

byte flag = 0;            //flag to stop loop in void loop

int photoCellSensor = 8;     // motion sensor inputs to pin 8
int motorControl = 9;     // for use later
int motorControl_1 = 10;  // output to motor 1
int motorControl_2 = 11;  // output to motor 2
int lastphotoCellState = 0;
int photoCellCounter = 0;
int delayTime = 50;



void setup() {
  pinMode(photoCellSensor, INPUT);
  pinMode(motorControl_1, OUTPUT);
  pinMode(motorControl_2, OUTPUT);
}

void loop() {
  int photoCellState = digitalRead(photoCellSensor);  // to read photocell state
  if (photoCellState != lastphotoCellState)
  {
    if (photoCellState == HIGH)    // if someone stepped in front of photocell it goes high
    {
      photoCellCounter = 0;
      Serial.println("On");
      Serial.print("number of times gone dark (high):  ");
      Serial.println(photoCellCounter);
      if (flag == 0)
      {
        for (int a = 1; a < 6; a++)            // cycle the motors 5 times
        {
          digitalWrite(motorControl_1, HIGH);   //activate the motors briefly
          delay(delayTime);
          digitalWrite(motorControl_2, HIGH);
          delay(delayTime);
          digitalWrite(motorControl_1, LOW);
          delay(delayTime);
          digitalWrite(motorControl_2, LOW);
          flag = 1;                             //stop this routine from running continually
        }
      }
    }
  } else
  {
    // if the current state is LOW then the button
    // went from on to off (dark to light):
    Serial.println("off(dark)");
    // sit idle and wait for someone to step in front of photocell (I don't know what to put here)
  }
  // save the current state as the last state,
  //for next time through the loop
  lastphotoCellState = photoCellState;

}

This program starts up with both LEDs lit. They should be off.

Set them the way you want in setup()

Making the photocell go dark causes the sequence to run once properly, but…
Will not run again until reset manually

The flag variable is never reset to zero. Why is it needed anyway ?

-Leaves both LEDs lit

That is the state you leave them in at the end of the for loop

-Nothing prints to the serial monitor

No Serial.begin() in setup()

Eureka! it works. Thanks so much, UKHeliBob! (Sent a karma to Crossroads and couple your way!)

Set them the way you want in setup()

Thanks!

The flag variable is never reset to zero. Why is it needed anyway ?

Crossroads suggested it to keep motors from running forever in my simple first example above. I removed the flag in this version and it works!

That is the state you leave them in at the end of the for loop.

Thanks!

No Serial.begin() in setup()

Oh I missed an obvious one there!