If/While statement help

Hello all!
I am using a proximity sensor to detect objects on a conveyor table. If the prox is triggered (Sensor > 10) then I want the conveyor motor to stop until the table is cleared and the prox is no longer engaged and would then start the motor again. The sensor works wonderfully and the LED's work as they should, however, once the prox is tripped in my while statement the motor will not engage once the table is clear. So essentially it is staying engaged even after the object has passed. Is there an easy way to fix this? I think the issue lies in the last part of the code. This is the only thing preventing me from finishing this project! Thanks a bunch!

int enA = 6;
int in1 = 5;
int in2 = 7;
int enB = 10;
int in3 = 9;
int in4 = 8;
#define SENSORPIN A0
int Sensor;
const unsigned int onTime = 100;
const unsigned int offTime = 5000;
const unsigned int onTime2 = 5000;
unsigned long previousMillis = 0;
int interval = onTime;
boolean motorState1 = true;
boolean motorState2 = true;

void setup() {
  pinMode(enA, OUTPUT);
  pinMode(in1, OUTPUT);
  pinMode(in2, OUTPUT);  
  pinMode(enB, OUTPUT);
  pinMode(in3, OUTPUT);
  pinMode(in4, OUTPUT);  
  pinMode(A0, INPUT);//prox
  pinMode(2, OUTPUT);//green
  pinMode(3, OUTPUT);//red
  pinMode(4, OUTPUT);//yellow


void loop() {
 int Sensor = analogRead(SENSORPIN);
 digitalWrite(in4, motorState2); //motor on forward
digitalWrite(in3, LOW);
analogWrite(enB, 55);

digitalWrite(in1, motorState1); //motor on forward
digitalWrite(in2, LOW);
analogWrite(enA, 100);
digitalWrite(2, HIGH); //green on
digitalWrite(4, LOW);
digitalWrite(3, LOW);
unsigned long currentMillis = millis();
if ((unsigned long)(currentMillis - previousMillis) >= interval) {
 if (motorState1) {
  interval = offTime;
  } else {
    interval = onTime;}
    motorState1 = !(motorState1);
    previousMillis = currentMillis;
  while (Sensor > 10) {
digitalWrite(4, HIGH);//yellow on
digitalWrite(2, LOW);
 digitalWrite(in3, LOW); //motor off
digitalWrite(in4, LOW);
analogWrite(enB, 0);


digitalWrite(in4, motorState2); //motor on forward
digitalWrite(in3, LOW);
analogWrite(enB, 55);


You have a variable called Sensor inside your loop function. Sensor is just a varable. Variables cannot change by themselves. They only change when you explicitly change them.

You also wrote a while cycle in your loop function that starts if Sensor > 10 is true and keeps iterating as long as it is true. However, you never change Sensor variable inside that cycle. This means that once your program enters that cycle, it will iterate in that cycle forever. It will be trapped in that cycle forever.

That exactly what you see in your experiments.

Running cycles that impede progress of loop function for potentially extended periods time is generally not a good Arduino programming practice. But for now let's keep it that way. However, you have to make sure that you update the value of Sensor with fresh data inside that cycle. Re-read your sensor data periodically inside the cycle. I.e. put

Sensor = analogRead(SENSORPIN);

somewhere in the cycle.

Thank you for your help! I will give that a try! Would there be any noticeable negative effects from running it this way? Typically the sensor would be clear again in a few minutes once engaged.

Minutes is like millenniums for the Arduino. Thing is, the loop() is already looping, so why not use that? That way you're able to do other stuff while waiting :slight_smile: