SOLVED:How do I get program past the third condition

This program doesn't go past the third if statement. Once all three lights are on, green is supposed to stay on and the other two turn off, then the count begins.
What have I done wrong?

int button1 = 13;
int button2 = 12;
int button3 = 11;
int button1State = LOW;
int button2State = LOW;
int button3State = LOW;
int Ready;
int greenL = 4;
int yellowL = 3;
int redL = 2;
int t = 500;

int productC;
int boxC;
int palletC;
int start = 0;
int moveBox = 0;
int movePallet = 0;
int boxesOnPallet = 0;
void setup() {
  // put your setup code here, to run once:
Serial.begin(9600);
pinMode(button1, INPUT);
pinMode(button2, INPUT);
pinMode(button3, INPUT);
pinMode(greenL, OUTPUT);
pinMode(yellowL, OUTPUT);
pinMode(redL, OUTPUT);
}

void loop() { 
button1State = digitalRead(button1);
button2State = digitalRead(button2);
button3State = digitalRead(button3);
boxC = 0;
productC = 0;
palletC = 0;
if (button3State == true) {
  digitalWrite(redL, HIGH);
  Serial.println("Empty pallet in position");
  
}
if (button2State == true) {
  digitalWrite(yellowL, HIGH);
  Serial.println("Empty box ready");
  
}
if (button1State == true) {
  digitalWrite(greenL, HIGH);
  Serial.println("Product ready"); 
}

if (button3State == HIGH && button2State == HIGH && button1State == HIGH) {
  digitalWrite(redL, LOW);
  digitalWrite(yellowL, LOW);
  digitalWrite(greenL, HIGH);
  start++;
  delay(t); 
}

while (start == 1 && productC != 5) {
  productC = productC + 1;
  Serial.print(productC);
  delay(t);
}
if (productC == 5) {
  digitalWrite(greenL, LOW);
  digitalWrite(yellowL, HIGH);
  moveBox = 1;
  Serial.println("Box ready");
  delay(t);
}
while(moveBox == 1 && productC == 5) {
  boxC = boxC + 1;
  Serial.print(boxC);
  Serial.println("Full box count: ");
  delay(t);
}
while(moveBox == 1 && palletC != 6) {
  boxesOnPallet = boxesOnPallet + 1;
  Serial.print(boxesOnPallet);
  Serial.println("Boxes on pallet: ");
  delay(t);
}
if(boxesOnPallet == 6) {
  digitalWrite(yellowL, LOW);
  digitalWrite(redL, HIGH);
  movePallet = 1;
  Serial.print("Move pallet");
  delay(t);
}
while(movePallet == 1 && palletC%6 == 0) {
  palletC = palletC + 1;
  Serial.print("Full pallets: ");
  Serial.println(palletC);
  delay(t);
}
delay(t);
}

Already found a mistake.

while(moveBox == 1 && palletC != 6) {```

Ive got it to start counting but once it gets to Full box count it does doesn't go to the next condition.

int button1 = 13;
int button2 = 12;
int button3 = 11;
int button1State = LOW;
int button2State = LOW;
int button3State = LOW;
int Ready;
int greenL = 4;
int yellowL = 3;
int redL = 2;
int t = 500;

int productC;
int boxC;
int palletC;
int start = false;
int moveBox = 0;
int movePallet = 0;
int boxesOnPallet = 0;
void setup() {
  // put your setup code here, to run once:
Serial.begin(9600);
pinMode(button1, INPUT);
pinMode(button2, INPUT);
pinMode(button3, INPUT);
pinMode(greenL, OUTPUT);
pinMode(yellowL, OUTPUT);
pinMode(redL, OUTPUT);
}

void loop() { 
button1State = digitalRead(button1);
button2State = digitalRead(button2);
button3State = digitalRead(button3);
boxC = 0;
productC = 0;
palletC = 0;
if (button3State == true) {
  digitalWrite(redL, HIGH);
  Serial.println("Empty pallet in position");
  
}
if (button2State == true) {
  digitalWrite(yellowL, HIGH);
  Serial.println("Empty box ready");
  
}
if (button1State == true) {
  digitalWrite(greenL, HIGH);
  Serial.println("Product ready"); 
}

while (digitalRead(redL) == HIGH &&  digitalRead(yellowL) == true && digitalRead(greenL) == HIGH) {
  start = true;
  digitalWrite(redL, LOW);
  digitalWrite(yellowL, LOW);
  digitalWrite(greenL, HIGH);
  delay(t); 
}

while (start == true && productC != 5) {
  productC = productC + 1;
  Serial.println(productC);
  delay(t);
}
if (productC == 5) {
  digitalWrite(greenL, LOW);
  digitalWrite(yellowL, HIGH);
  moveBox = 1;
  Serial.println("Box ready");
  delay(t);
  
}
while(moveBox == 1 && productC == 5) {
  boxC = boxC + 1;
  Serial.print(boxC);
  Serial.println("Full box count: ");
  delay(t);
  
}
while (moveBox == 1) {
  boxesOnPallet = boxesOnPallet + 1;
  Serial.print(boxesOnPallet);
  Serial.println("Boxes on pallet: ");
  delay(t);
  moveBox = 0;
}
if(boxesOnPallet == 6) {
  digitalWrite(yellowL, LOW);
  digitalWrite(redL, HIGH);
  movePallet = 1;
  Serial.print("Move pallet");
  delay(t);
  movePallet = 0;
}
while(movePallet == 1 && palletC%6 == 0) {
  palletC = palletC + 1;
  Serial.print("Full pallets: ");
  Serial.println(palletC);
  delay(t);
}
delay(t);
}

It's always good to find your own coding issues :slight_smile:

I'm not a great coder but I've noticed:

You test the button state compared to "true" while the variable in an int. It probably works but not a good practice.

In your if statements if all three buttons are pushed, the first 4 if statements will be executed.
Consider the "switch" structure and start with all 3 being pressed

1 Like

Once it enters this while loop, there is no way to exit. moveBox and productC are not updated within that loop.

1 Like

How come it is able to get out of the previous while loops?

Why is it able to get out of the previous while loops?

The variables in those while statements are changed within the loop.

while (digitalRead(redL) == HIGH &&  digitalRead(yellowL) == true && digitalRead(greenL) == HIGH) {
  start = true;
  digitalWrite(redL, LOW);
  digitalWrite(yellowL, LOW);
  digitalWrite(greenL, HIGH);
  delay(t); 
}

while (start == true && productC != 5) {
  productC = productC + 1;
  Serial.println(productC);
  delay(t);
}

Because in this one productC is being updated so eventually is = 5 and the loop stops.

And this one...

Is an "if" statement not a "while" loop.

1 Like

Thanks everybody, got it figured out.

int button1 = 13;
int button2 = 12;
int button3 = 11;
int button1State = LOW;
int button2State = LOW;
int button3State = LOW;
int Ready;
int greenL = 4;
int yellowL = 3;
int redL = 2;
int t = 200;

int productC;
int boxC;
int palletC;
int start = false;
int moveBox = 0;
int movePallet = 0;
int boxesOnPallet = 0;

void setup() {
  // put your setup code here, to run once:
Serial.begin(9600);
pinMode(button1, INPUT);
pinMode(button2, INPUT);
pinMode(button3, INPUT);
pinMode(greenL, OUTPUT);
pinMode(yellowL, OUTPUT);
pinMode(redL, OUTPUT);
}

void loop() { 
button1State = digitalRead(button1);
button2State = digitalRead(button2);
button3State = digitalRead(button3);

productC = 0;
if (button3State == true) {
  digitalWrite(redL, HIGH);
  Serial.println("Empty pallet in position");
  
}
if (button2State == true) {
  digitalWrite(yellowL, HIGH);
  Serial.println("Empty box ready");
  
}
if (button1State == true) {
  digitalWrite(greenL, HIGH);
  Serial.println("Product ready"); 
}

while (digitalRead(redL) == HIGH &&  digitalRead(yellowL) == true && digitalRead(greenL) == HIGH) {
  start = true;
  digitalWrite(redL, LOW);
  digitalWrite(yellowL, LOW);
  digitalWrite(greenL, HIGH);
  delay(t); 
}

while (start == true && productC != 5) {
  productC = productC + 1;
  Serial.println(productC);
  delay(t);
}
if (productC == 5) {
  digitalWrite(greenL, LOW);
  digitalWrite(yellowL, HIGH);
  moveBox = 1;
  Serial.println("Box ready");
  delay(t); 
}
if (moveBox == 1 && productC == 5) {
  boxC = boxC + 1;
  Serial.print("Full box count: ");
  Serial.println(boxC);
  delay(t);
}
if (moveBox == 1 && productC%5 == 0) {
  boxesOnPallet = boxesOnPallet + 1;
  Serial.print("Boxes on pallet: ");
  Serial.println(boxesOnPallet);
  delay(t);
}
if (boxesOnPallet == 6) {
  digitalWrite(yellowL, LOW);
  digitalWrite(redL, HIGH);
  movePallet = 1;
  Serial.println("Move pallet");
  delay(t);
  boxesOnPallet = 0;
  delay(t);
}
if (movePallet == 1) {
  digitalWrite(redL, LOW);
  palletC = palletC + 1;
  Serial.print("Full pallets: ");
  Serial.println(palletC);
  delay(t);
  movePallet = 0;
}
delay(t);

}

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