int C1_1 = 3;
int C1_2 = 4;
int C2_1 = 5;
int C2_2 = 6;
int Act = 7;
int G = 8;
int R = 9;
int fan = 10;
int S1 = 12;
int heater = 13;
int btn1 = A0;
int btn2 = A1;
int S2 = A2;
int S3 = A3;
int S4 = A4;
int S5 = A5;
int btn1state;
int btn2state;
int s1state;
int s2state;
int s3state;
int s4state;
int s5state;
int delaytime = 3;
int i = 0;
int heatingdone = 0;
int fail = 0;
int sensor = 0; // variable to ensure that sensors dont work before start button.
unsigned long time;
void C1_forward();
void C2_forward();
void heating();
void cooling();
void C2_backward();
void initial();
void setup() {
// put your setup code here, to run once:
pinMode(C1_1, OUTPUT);
pinMode(C1_2, OUTPUT);
pinMode(C2_1, OUTPUT);
pinMode(C2_2, OUTPUT);
pinMode(Act, OUTPUT);
pinMode(G, OUTPUT);
pinMode(R, OUTPUT);
pinMode(fan, OUTPUT);
pinMode(heater, OUTPUT);
pinMode(S1, INPUT_PULLUP);
pinMode(S2, INPUT_PULLUP);
pinMode(S3, INPUT_PULLUP);
pinMode(S4, INPUT_PULLUP);
pinMode(S5, INPUT_PULLUP);
pinMode(btn1, INPUT_PULLUP);
pinMode(btn2, INPUT_PULLUP);
Serial.begin(9600);
initial(); //initial state
}
void loop() {
e_stop:
// put your main code here, to run repeatedly:
Serial.println("loop");
btn1state = digitalRead(btn1);
while(btn1state == LOW && sensor == 0) // if btn1 is pressed. Requires sensors to be at 0 to run.
{
digitalWrite(G, HIGH);
digitalWrite(R, LOW);
C1_forward(); // conveyor 1 moves forward, motor turns counter-clockwise.
if(digitalRead(btn2) == LOW)
{
initial();
goto e_stop;
}
s1state = digitalRead(S1);
if(s1state == LOW) // if s1 is activated.
{
digitalWrite(C1_1, LOW);
digitalWrite(C1_2, LOW); // conveyor 1 stops.
delay(1000);
digitalWrite(Act, HIGH); // actuator extends.
sensor = 1; // set sensor to 1 so that s2 can work.
break;
}
}
while(digitalRead(S2)==LOW && sensor == 1) //if s2 is activated.
{
digitalWrite(Act, LOW); // actuator retracts.
delay(1000);
while(i < 4)
{
C2_forward(); // conveyor 2 moves forward, motor turns counter-clockwise.
if(digitalRead(btn2) == LOW)
{
initial();
goto e_stop;
}
Serial.println("while less than 4 loop");
s3state = digitalRead(S3);
if(s3state == LOW)
{
Serial.println("S3 pressed.");
digitalWrite(C2_1, LOW);
digitalWrite(C2_2, LOW); // conveyor 2 stops.
delay(1000);
Serial.println("heating start");
heating(); // heating process.
Serial.println("heating end");
heatingdone = 1;
delay(1000);
while(heatingdone == 1) // when heating is complete.
{
Serial.println("heating done loop");
C2_forward(); // counter-clockwise.
if(digitalRead(btn2) == LOW)
{
initial();
goto e_stop;
}
s4state = digitalRead(S4);
if(s4state == LOW)
{
digitalWrite(C2_1, LOW);
digitalWrite(C2_2, LOW); // conveyor 2 stops.
delay(1000);
cooling(); // cooling process.
delay(1000);
heatingdone = 0; //set heating to zero/not complete so that program goes back.
i = i + 1;
if(i < 4) // if i is smaller than 4.
{
Serial.println("less than 4");
s3state = digitalRead(S3);
while(s3state == HIGH)
{
C2_backward(); // clockwise.
if(digitalRead(btn2) == LOW)
{
initial(); // goes back to initial state when e stop is pressed.
goto e_stop; // go to e stop destination.
}
s3state = digitalRead(S3);
}
}
}
if(i == 4)
{
Serial.println("while4");
s5state = digitalRead(S5);
while(s5state == HIGH)
{
C2_forward(); // counter-clockwise.
if(digitalRead(btn2) == LOW)
{
initial();
goto e_stop;
}
Serial.println("while5");
s5state = digitalRead(S5);
}
sensor = 2;
break; // exit loop.
}
}
}
}
}
s5state = digitalRead(S5);
while(s5state == LOW && sensor == 2)
{
digitalWrite(C2_1, LOW);
digitalWrite(C2_2, LOW); // conveyor 2 stops.
time = millis() + 5000; // time is millis plus 5 seconds
fail = 1; // set variable fail to 1.
while(millis() < time) // if millis is smaller than the set time, then 5 second is not over.
{
btn1state = digitalRead(btn1);
if(btn1state == LOW) // if button pressed, grrn light blinks
{
digitalWrite(G, HIGH);
delay(500);
digitalWrite(G, LOW);
delay(500);
digitalWrite(G, HIGH);
delay(500);
digitalWrite(G, LOW);
delay(500);
digitalWrite(G, HIGH);
delay(500);
digitalWrite(G, LOW);
delay(500);
digitalWrite(G, HIGH);
fail = 0; // set fail to 0 if pass, so that program does not jump to inspection fail.
sensor = 0;
}
}
if(fail == 1) // if inspection fail, red light blinks.
{
digitalWrite(R, HIGH);
delay(500);
digitalWrite(R, LOW);
delay(500);
digitalWrite(R, HIGH);
delay(500);
digitalWrite(R, LOW);
delay(500);
digitalWrite(R, HIGH);
delay(500);
digitalWrite(R, LOW);
delay(500);
sensor = 0;
}
}
}
void initial() //initial state.
{
digitalWrite(R, HIGH);
digitalWrite(G, LOW);
digitalWrite(Act, LOW);
digitalWrite(heater, LOW);
digitalWrite(fan, LOW);
sensor = 0;
}
void C1_forward() //counter-clockwise.
{
digitalWrite(C1_1, HIGH);
digitalWrite(C1_2, HIGH);
delay(delaytime); //pos1
digitalWrite(C1_1, HIGH);
digitalWrite(C1_2, LOW);
delay(delaytime); //pos4
digitalWrite(C1_1, LOW);
digitalWrite(C1_2, LOW);
delay(delaytime); //pos3
digitalWrite(C1_1, LOW);
digitalWrite(C1_2, HIGH);
delay(delaytime); //pos2
}
void C2_forward() //counter-clockwise.
{
digitalWrite(C2_1, HIGH);
digitalWrite(C2_2, HIGH);
delay(delaytime); //pos1
digitalWrite(C2_1, HIGH);
digitalWrite(C2_2, LOW);
delay(delaytime); //pos4
digitalWrite(C2_1, LOW);
digitalWrite(C2_2, LOW);
delay(delaytime); //pos3
digitalWrite(C2_1, LOW);
digitalWrite(C2_2, HIGH);
delay(delaytime); //pos2
}
void heating() // heating process.
{
digitalWrite(heater, HIGH);
delay(3000);
digitalWrite(heater, LOW);
}
void cooling() //cooling process.
{
digitalWrite(fan, HIGH);
delay(5000);
digitalWrite(fan, LOW);
}
void C2_backward() // clockwise.
{
digitalWrite(C2_1, HIGH);
digitalWrite(C2_2, HIGH);
delay(delaytime); //pos1
digitalWrite(C2_1, LOW);
digitalWrite(C2_2, HIGH);
delay(delaytime); //pos2
digitalWrite(C2_1, LOW);
digitalWrite(C2_2, LOW);
delay(delaytime); //pos3
digitalWrite(C2_1, HIGH);
digitalWrite(C2_2, LOW);
delay(delaytime); //pos4
}