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);
}
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