If loop repeating

I am making a servo move when there is no light and there is motion. This happens successfully but when it does it keeps on looping until one of those are false. here is the code:

#include <Servo.h>

const int SERVO_PIN  = 9;
const int SENSOR = 2;

int sensorVal = 0;

Servo servo;

// variables will change:
int reset = 0;
int turn = 180;
int check = 0;

bool lightOn = false;

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

  pinMode (SENSOR, INPUT);
  servo.attach(SERVO_PIN);
  servo.write(reset);
}

void loop() {
  int analogValue = analogRead(A0);
  sensorVal = digitalRead(SENSOR);

  if (analogValue > 100) {
    lightOn = true;
    Serial.write("On");
  }
  else if (analogValue < 99) {
    lightOn = false;
    Serial.write("Off");
  }

  if (sensorVal == HIGH && lightOn == false) {
    Serial.write(" movement");
    servo.write(turn);
    delay(1000);
    servo.write(reset);
    lightOn = true;
  }
  if(sensorVal == LOW) {
    if(lightOn == true) {
      check = check + 1;
      Serial.write(check);
      delay(1000);
      if(check > 30){
        servo.write(turn);
        delay(1000);
        servo.write(reset);
        lightOn = false;
        check = 0;
      }
    }
  }
  delay(1000);
}

I would agree with you. If both situations are true, the servo will turn. And it will continue to turn 30 steps, at which point it will stop turning. However, the situation which caused it to turn in the first place still exists, so it starts turning another 30 times. If you are trying to make it turn 30 times only once, then you need to add another variable such as, bool turned; Set it to false if either situation doesn*t exist. Set it to true immediately after doing the 30 turns, but ONLY DO 30 TURNS IF that variable is not already true.

And, BTW, create that variable either globally or as static within loop.

An if statement does not a loop alone... it's a Selection statement not an Iteration statement (see Statements - cppreference.com) so if it is executed multiple times it likely means the loop is looping and the condition is still true

the code sets lightOn within each of the 4 conditions (except when analogValue is exactly equal to 100). Shouldn't lightOn only be set based on the conditions testing analogValue

in other words, the code sets lightVal = true when sensorVal == HIGH and lightOn == false, but above that, lightOn is set to false if analogVal is < 99.

which condition should set lightOn?

It sounds like it's the "keeps on looping" part you don't like. So you want the servo to move once, each time there was light or no motion changes to no light and motion.

#include <Servo.h>

const int SERVO_PIN  = 9;
const int MOTION_SENSOR = 2;

Servo servo;

const int reset = 0;
const int turn = 180;

bool lightOn = false;

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

  pinMode (MOTION_SENSOR, INPUT);

  servo.write(reset);
  servo.attach(SERVO_PIN);
}

void loop()
{
  bool light = analogRead(A0) > 100;
  bool motion = digitalRead(MOTION_SENSOR) == HIGH;
  static bool servoMoved = false;

  if (!light && motion)
  {
    if (!servoMoved)
    {
      Serial.print(" no light and motion");

      servo.write(turn);
      delay(1000);
      servo.write(reset);

      servoMoved = true;  // Don't move again
    }
  }
  else
  {
    if (servoMoved)
    {
      if (light)
        Serial.print(" light");
        
      if (!motion)
        Serial.print(" no motion");
        
      servoMoved = false;  // OK to move again
    }
  }

  delay(1000);
}

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.