#define powerUp 2 //RED PB
#define motorCtrl 3 //Red LED
#define lightCtrl 4 //Green LED
#define doorCtrl 5 //Blue LED
#define buzzerCtrl 6 //Yellow LED
#define punchCtrl 7 //Blue PB
#define punchMotor 8
int latchState = LOW;
int currentState = LOW;
int lastState = LOW;
int punch = LOW;
void setup()
{
//defining pinmodes of pins
pinMode(powerUp, INPUT); //main control switch
pinMode(motorCtrl, OUTPUT); //motor control circuit ouput
pinMode(lightCtrl, OUTPUT); //workarea lighting
pinMode(doorCtrl, OUTPUT); //door interlock control
pinMode(buzzerCtrl, OUTPUT); //buzzer to indicate ready state of punching m/c
pinMode(punchCtrl, INPUT); //PB to operate punching m/c
pinMode(punchMotor, OUTPUT); //Light blue LED to indicate the punching motor
//initializing the serial monitor @ 9600 baudrate
Serial.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
//latching the powerUp input as start/stop is regged to a single PB
currentState = digitalRead(powerUp);
if (lastState == LOW && currentState == HIGH)
{
latchState = !latchState;
}
lastState = currentState;
//switching ON devices when PB/latch State is ON
if (latchState == HIGH)
{
digitalWrite(lightCtrl, HIGH); //green
delay(300);
digitalWrite(doorCtrl, HIGH); //blue
delay(300);
digitalWrite(motorCtrl, HIGH); //red
delay(300);
digitalWrite(buzzerCtrl, HIGH); //yellow
}
//switching OFF devices when PB/latch State is OFF
else if (latchState == LOW)
{
digitalWrite(buzzerCtrl, LOW); //yellow
delay(300);
digitalWrite(motorCtrl, LOW); //red
delay(300);
digitalWrite(doorCtrl, LOW); //blue
delay(300);
digitalWrite(lightCtrl, LOW); //green
}
Serial.println(latchState);
Serial.println(punch);
if (latchState == 1 )
{
punch = digitalRead(punchCtrl);
if (punch == HIGH)
{
digitalWrite(punchMotor, HIGH);
delay(2000);
}
else
{
digitalWrite(punchMotor, LOW);
}
}
}