/*
>Machine control_Jimin.ino
>DM6001 - LOW COST AUTOMATED SYSTEMS 2024/5 SEM1
>Assignment 1 - Machine Control
>Date: 04/10/2024
>Author: Jimin P Mathew [24044245]
*/
/* -----------------------------------------------------------------------------------
INCLUDES, DEFINES, ETC:
------------------------------------------------------------------------------------*/
// #include<interrupt.h> //include the library for attachInterrupt() function
// Define input pins
#define EMERGENCYSTOP 2 //Red button for emergency stop
#define SYSTEM 3 //Green button for On/Off [Ctrl+click - to latch the button]
#define CYCLE 4 //Yellow button for turning on clutch and strip_feed
// Define output pins
#define LIGHT 5 //Blue LED
#define INTERLOCK 6 //Green LED
#define MCU 7 //Amber LED
#define STRIP_FEED 8 //Pink LED
#define CLUTCH 9 //Cyan LED
#define EMER_BUZZER 10 //Red LED
// Declaring flags to be used
volatile bool is_on = false; // Flag to track system status
volatile bool interruptFlag = false; // Flag set in the ISR for determining if emergency button is pressed
int onoff; //Falg to read state of SYSTEM
int cyclepressed; //Flag to read state of CYCLE
//Function prototypes
void startup(void);
void shutdown(void);
void cycle(void);
/* -----------------------------------------------------------------------------------
SETUP:
------------------------------------------------------------------------------------*/
void setup()
{
Serial.begin(9600); // Initialize serial communication
// Set pins modes of inputs and outputs
pinMode(EMERGENCYSTOP, INPUT);
pinMode(SYSTEM, INPUT);
pinMode(CYCLE, INPUT);
pinMode(LIGHT, OUTPUT);
pinMode(INTERLOCK, OUTPUT);
pinMode(MCU, OUTPUT);
pinMode(STRIP_FEED, OUTPUT);
pinMode(CLUTCH, OUTPUT);
pinMode(EMER_BUZZER, OUTPUT);
// interupt added to stop all function of system when EMERGENCYSTOP is pressed
attachInterrupt(digitalPinToInterrupt(EMERGENCYSTOP), emergencyStop, RISING);
}
/* -----------------------------------------------------------------------------------
MAIN LOOP:
------------------------------------------------------------------------------------*/
void loop()
{
// Read the state of input pins
onoff = digitalRead(SYSTEM); // Read the state of SYSTEM
cyclepressed = digitalRead(CYCLE); // Read the state of CYCLE
if (onoff == 1) //Check the state of SYSTEM, whether green PB is pressed or not
{
startup(); // Call the startup function
}
else
{
shutdown(); // Call the shutdown function
}
if (cyclepressed == 1 && is_on) //Check the state of CYCLE, whether yellow PB is pressed or not
{
cycle(); // Call the cycle function
}
}
/* -----------------------------------------------------------------------------------
FUNCTIONS:
------------------------------------------------------------------------------------*/
//----------------------------------------
// Function to start up the system
//----------------------------------------
void startup()
{
digitalWrite(LIGHT, HIGH); // Turn on the lights
delay(500);
digitalWrite(INTERLOCK, HIGH); // Activate the door interlock
digitalWrite(MCU, HIGH); // Activate the Motor Control Unit
is_on = true; // Set the system flag 'is_on' to indicate it's switched ON
}
//----------------------------------------
// Function to shut down the system
//----------------------------------------
void shutdown()
{
digitalWrite(LIGHT, LOW); // Turn off the lights
delay(500);
digitalWrite(INTERLOCK, LOW); // Deactivate the door interlock
digitalWrite(MCU, LOW); // Deactivate the MCU
is_on = false; // Reset the system flag 'is_on' to indicate it's switched OFF
}
//-----------------------------------------------------------
//Function for the cyclic operation of CLUTCH & STRIP_FEED
//-----------------------------------------------------------
void cycle()
{
if (interruptFlag == 0) //Checks if emergency PB was pressed or not
{
digitalWrite(CLUTCH, HIGH); // Engage the clutch
delay(500);
}
if (interruptFlag == 0)
{
digitalWrite(STRIP_FEED, HIGH); // Activate the strip feeder
delay(500);
}
if (interruptFlag == 0)
{
digitalWrite(STRIP_FEED, LOW); // Deactivate the strip feeder
delay(500);
}
if (interruptFlag == 0)
{
digitalWrite(CLUTCH, LOW); // Disengage the clutch
delay(500);
}
if (interruptFlag) interruptFlag = false; //Resets emergency interrupt flag if it was pressed
}
//-----------------------------------------------------------
// Function to activate emergency stop sequence
//-----------------------------------------------------------
void emergencyStop()
{
if (onoff) //emergency interrupt works only if main power, ie,SYSTEM (Green PB) is pressed ON
{
is_on = false; // Reset the system flag 'is_on' to indicate it's switched OFF
digitalWrite(MCU, LOW); // Turn off the Amber LED
digitalWrite(CLUTCH, LOW); // Turn off the Cyan LED
digitalWrite(STRIP_FEED, LOW);// Turn off the Pink LED
/*If we want the lights and door interlock to be shut down when pressing the emergency push button,
we can add the following commands above;
digitalWrite(LIGHT, LOW); // Turn off the Blue LED
digitalWrite(INTERLOCK, LOW); // Turn off the Green LED
But in an industrial point of view, when emergency is pressed, only the motor and feeder control needs to be switched off,
leaving the lights and door interlocks to be ON.
*/
while (digitalRead(EMERGENCYSTOP) == HIGH) // loop to hang up the program till emergency button is pressed
{
interruptFlag = true;
digitalWrite(EMER_BUZZER, HIGH); //Turn on Buzzer LED (RED) indicating emergency is pressed
}
digitalWrite(EMER_BUZZER, LOW); //Turn OFF Buzzer LED (RED) when Emergency button is released
}
}
/**************************************END OF CODE**************************************/