#include <ButtonDebounce.h>
#include <LiquidCrystal_I2C.h>
//For Documentation for the Libraries:
/*
LiquicCrystal_I2C = https://github.com/fdebrabander/Arduino-LiquidCrystal-I2C-library
ButtonDebounce = https://github.com/maykon/ButtonDebounce
*/
//For Defining Input Pins
#define e_stop_pin 8
#define auto_pin 9
#define manual_pin 10
//For Degining Output Pins
#define red_led 7
#define green_led 6
#define yellow_led 5
//Initialization for the ButtonDebounce Library
ButtonDebounce e_stop (e_stop_pin, 50);
ButtonDebounce auto_mode (auto_pin, 50);
ButtonDebounce manual_mode (manual_pin, 50);
//for storing last states of the button/switches used
bool last_e_stop_state, last_auto_state, last_manual_state;
// Set the LCD address to 0x27 for a 16 chars and 2 line display
LiquidCrystal_I2C lcd(0x27, 16, 2);
//For Millis() prerequisite
unsigned long currentMillis;
unsigned long lastCurrentMillis;
unsigned long conveyorTimeRun = 3000; //Equivalent to 3 seconds
unsigned long conveyorTimeStop = 1000;
unsigned long injectingTimeRun = 3000;
unsigned long injectingTimeRetract = 3000;
//For processes state
/*
Stat:
ConvRun --Conveyor Run
ConvStop --Conveyor Stop
Inject --Syringe Inject
Retract --Syringe Retract
*/
String runningStat = "ConvRun"; //Default value
void setup()
{
Serial.begin(9600);
lcd.begin(16, 2);
//For Outputs
pinMode(red_led, OUTPUT);
pinMode(green_led, OUTPUT);
pinMode(yellow_led, OUTPUT);
//Setting up what the current state of the btns
last_e_stop_state = !e_stop.state();
last_auto_state = !auto_mode.state();
last_manual_state = !manual_mode.state();
}
void loop()
{
output(); //for outputs function
display(); //for display function
//Necessary to put these to void loop
//as required by the ButtonDebounce Library
e_stop.update();
auto_mode.update();
manual_mode.update();
}
void output()
{
if (e_stop.state() == LOW)
{
if (auto_mode.state() == LOW)
{
//Sets the Green Condition On
digitalWrite(red_led, LOW);
digitalWrite(green_led, HIGH);
digitalWrite(yellow_led, LOW);
//Insert auto mode process here
//avoid using delay(), use millis() instead
//example process:
/*
1. 3 seconds conveyor run
2. 1 seconds conveyor stop
3. 3 seconds injecting
4. 3 seconds retraction
*/
//ConvRun
if (runningStat == "ConvRun")
{
//time starts
currentMillis = millis();
//Serial.println("im here!");
//For Serial Monitor
Serial.println("Conveyor Run!");
//For LCD Display
lcd.setCursor(0,1);
lcd.print("Conveyor Running");
//Insert your output commands here
if (currentMillis - lastCurrentMillis >= conveyorTimeRun)
{
//makes the currentMillis as the lastCurrentMillis
lastCurrentMillis = currentMillis;
//moves the next status to ConvStop
runningStat = "ConvStop";
}
}
//ConvStop
else if (runningStat == "ConvStop")
{
//time starts
currentMillis = millis();
//For Serial Monitor
Serial.println("Conveyor Stop!");
//For LCD Display
lcd.setCursor(0,1);
lcd.print(" Conveyor Stop ");
//Insert your output commands here
if (currentMillis - lastCurrentMillis >= conveyorTimeStop)
{
//makes the currentMillis as the lastCurrentMillis
lastCurrentMillis = currentMillis;
//moves the next status to Inject
runningStat = "Inject";
}
}
//Inject
else if (runningStat == "Inject")
{
//time starts
currentMillis = millis();
//For Serial Monitor
Serial.println("Spice Injecting");
//For LCD Display
lcd.setCursor(0,1);
lcd.print("Spice Injecting");
//Insert your output commands here
if (currentMillis - lastCurrentMillis >= injectingTimeRun)
{
//makes the currentMillis as the lastCurrentMillis
lastCurrentMillis = currentMillis;
//moves the next status to Retract
runningStat = "Retract";
}
}
//Retract
else if (runningStat == "Retract")
{
//time starts
currentMillis = millis();
//For Serial Monitor
Serial.println("Syringe Retracting");
//For LCD Display
lcd.setCursor(0,1);
lcd.print("Syringe Retract");
//Insert your output commands here
if (currentMillis - lastCurrentMillis >= injectingTimeRetract)
{
//makes the currentMillis as the lastCurrentMillis
lastCurrentMillis = currentMillis;
//moves the next status to ConvRun
runningStat = "ConvRun";
}
}
}
//Manual Mode
//Insert your desired manual mode process here
else if (manual_mode.state() == LOW)
{
//Insert similar process to manual control like
/*
1. Button/Switch for Conveyor Run
2. Button for Injection Process
3. Button for Retraction Process
*/
}
//No Mode is selected
else if (auto_mode.state() == HIGH || manual_mode.state() == HIGH)
{
//Sets the Yellow Condition On
digitalWrite(red_led, LOW);
digitalWrite(green_led, LOW);
digitalWrite(yellow_led, HIGH);
//Resets Back all the timers and Millis
runningStat = "ConvRun"; //Default runningStat Value
lastCurrentMillis = currentMillis; //Stores the current millis as last millis
}
}
//If emergency stop
else if (e_stop.state() == HIGH)
{
//Sets the Green Condition On
digitalWrite(red_led, HIGH);
digitalWrite(green_led, LOW);
digitalWrite(yellow_led, LOW);
}
}
void display()
{
if (last_e_stop_state != e_stop.state())
{
if (e_stop.state() == HIGH)
{
//For Serial Communication
Serial.println("Emergency Stop!");
//For LCD Display
lcd.setCursor(0,0);
lcd.print(">> EMERGENCY <<");
lcd.setCursor(0,1);
lcd.print(">> STOP!! <<");
}
else if (e_stop.state() == LOW && auto_mode.state() == HIGH && manual_mode.state() == HIGH)
{
//Sets the Green Condition On
digitalWrite(red_led, LOW);
digitalWrite(green_led, LOW);
digitalWrite(yellow_led, HIGH);
//For Serial Communication
Serial.println("Automated Meat Spice Injector");
//For LCD Display
lcd.setCursor(0,0);
lcd.print("Automated Meat ");
lcd.setCursor(0,1);
lcd.print(" Spice Injector");
}
//stores the current read state of the btn as last state
last_e_stop_state = e_stop.state();
}
else if (last_auto_state != auto_mode.state())
{
//Auto Mode
if (e_stop.state() == LOW && manual_mode.state() == HIGH)
{
if (auto_mode.state() == LOW)
{
//For Serial Communication
Serial.println(">> Auto Mode <<");
//For LCD Display
lcd.clear();
lcd.setCursor(0,0);
lcd.print(">> AUTO MODE <<");
}
else if (auto_mode.state() == HIGH)
{
//For Serial Communication
Serial.println(">> Select Mode <<");
//For LCD Display
lcd.setCursor(0,0);
lcd.print("Automated Meat ");
lcd.setCursor(0,1);
lcd.print(" Spice Injector");
}
}
//stores the current read state of the btn as last state
last_auto_state = auto_mode.state();
}
else if (last_manual_state != manual_mode.state())
{
//Manual Mode
if (e_stop.state() == LOW && auto_mode.state() == HIGH)
{
if (manual_mode.state() == LOW)
{
//For Serial Communication
Serial.println(">> Manual Mode <<");
//For LCD Display
lcd.clear();
lcd.setCursor(0,0);
lcd.print("> MANUAL MODE <");
}
else if (manual_mode.state() == HIGH)
{
//For Serial Communication
Serial.println(">> Select Mode <<");
//For LCD Display
lcd.setCursor(0,0);
lcd.print("Automated Meat ");
lcd.setCursor(0,1);
lcd.print(" Spice Injector");
}
}
//stores the current read state of the btn as last state
last_manual_state = manual_mode.state();
}
}