/*
DM6001 Assignment 1
Ruo Yi Chew (24163147)
*/
// inputs
#define INTERRUPT 2 // Red Button (EMO Button)
#define SYSTEM 3 // Green LED
#define CYCLE 6 // Yellow LED
// outputs
#define EMERGENCY 5 // Red LED (Emergency Light)
#define LIGHT 7 // Green LED
#define INTERLOCK 8 // Orange LED 1
#define MCU 10 // Orange LED 2
#define CLUTCH 11 // Yellow LED 1
#define STRIP_FEED 12 // Yellow LED 2
#define POWER 9 // used for power supply to the cycle
//variables
int onoff; // used to test if power is on
int cyclepressed; // used to test if cycle button is pressed
bool first_time = true; // used to print emergency message ONCE
bool emergency_state = false; // used to declare if it is emergency
bool is_on = false;
void setup() {
Serial.begin(9600); // start the Serial Monitor with baud rate 9600
pinMode(INTERRUPT, INPUT); // declare the inputs and outputs
pinMode(SYSTEM, INPUT);
pinMode(CYCLE, INPUT);
pinMode(POWER, OUTPUT);
pinMode(EMERGENCY, OUTPUT);
pinMode(LIGHT, OUTPUT);
pinMode(INTERLOCK, OUTPUT);
pinMode(MCU,OUTPUT);
pinMode(CLUTCH, OUTPUT);
pinMode(STRIP_FEED, OUTPUT);
//declare the interrupt under pin 2 with increasing button condition to trigger INTER() func
attachInterrupt(digitalPinToInterrupt(INTERRUPT), INTER, RISING);
}
void loop() {
// check if the emergency state is true;
if (emergency_state == true){
EMO();
return;
}
digitalWrite(POWER, HIGH);
// Check the state of both buttons at the start of each loop
onoff = digitalRead(SYSTEM);
cyclepressed = digitalRead(CYCLE);
// check if the start button is pressed
if(onoff == 1)
{
if (is_on == false){ // if the system is not powered on
StartUp(); // Execute the startup sequence
} else if (is_on == true){ // if the system is powered on
ShutDown(); // Excute shutdown sequence
}
}
// now test if the cycle pressed button is high.
if(cyclepressed == 1){
Cycle(); // Run the cycle
}
}
// function to start up the system
void StartUp(){
Serial.println("System On!");
digitalWrite(LIGHT, HIGH);
delay(500);
digitalWrite(INTERLOCK, HIGH);
digitalWrite(MCU, HIGH);
// set a "system on" flag
is_on = true;
}
// function to shut down the system
void ShutDown(){
Serial.println("System Shut Down!");
digitalWrite(LIGHT, LOW);
delay(500);
digitalWrite(INTERLOCK, LOW);
digitalWrite( MCU, LOW);
// set a "system off" flag;
is_on = false;
}
// function to carry out the cycle
void Cycle(){
if (is_on == true){ // check that power is already on
digitalWrite(CLUTCH, HIGH); // carrying out a cycle
delay(500);
digitalWrite(CLUTCH, LOW);
delay(500);
digitalWrite(STRIP_FEED,HIGH);
delay(500);
digitalWrite(STRIP_FEED, LOW);
delay(500);
}
digitalWrite(CYCLE, LOW); // reset the Cycle Button
}
// interrupt function
void INTER(){
if (is_on == true){
emergency_state = true; // toggle the emergency state
}
}
// function to carry out emergencyt procedures
void EMO(){
if (first_time == true){ // do this ONCE only
for (int system = 7; system <= 12; system++){ // shut off the entire system
digitalWrite(system, LOW);
}
Serial.println("EMERGENCY!!!!"); // print the emergency message
is_on = false; // reset the entire system
first_time = false;
}
digitalWrite(EMERGENCY, HIGH); // blink the emergency LED
delay(100);
digitalWrite(EMERGENCY, LOW);
delay(100);
}
// end of entire program.