#include <LiquidCrystal.h>
// initialize the library by associating any needed LCD interface pin
// with the arduino pin number it is connected to
const int rs = 53, en = 51, d4 = 49, d5 = 47, d6 = 45, d7 = 43;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
int red = 13; // the red LED for Salad.
int green = 12; // the green LED for fish.
int yellow = 11; // the ywllow LED for Escalope.
int white = 10; // the white LED for emergency case.
int customerButton = 8; // push button to indicate new customer
int waiterButton = 7; // push button to to accept to serve a customer
int fishButton = 6; // push button to indicate the customer wants fish meal
int escalopeButton = 5; // push button to indicate the customer wants Escalope meal
int saladButton = 4; // push button to indicate the customer wants salad meal
int chefButton = 3; // push button to indicate the chef has started to prepare that customer’s order
int emergencyButton = 2; // push button to indicate emergency case
int potentiometer = A0; // potentiometer to choose the weight of fish meal
int normalCase = 1; // variable to indicate the normal case
int emergencyCase = 2; // variable to indicate the emergency case
int status; // variable to indicate the current status
int counter = 0; // variable to store customer’s number
int waitingNumber = 0; // variable to store a waiting number
int customerOrder = 0; // variable to store the customer order
int fish = 8000; // will be with the chef for 8 seconds
int Escalope = 6000; // will be with the chef for 6 seconds
int salad = 4000; // will be with the chef for 4 seconds
int chefBreak = 3000; // 3 second break between every 2 customer
int emergencyTime = 0; // will be with the chef for home delivery order
int fishWeight = 0; // variable to store the weight of fish meal
int weight250g = 3; // variable to indicate the weight of fish is 250 g
int weight500g = 2; // variable to indicate the weight of fish is 500 g
int weight750g = 1; // variable to indicate the weight of fish is 750 g
int breakCounter = 0; // variable to store the customer counter according to chef break
int none = 0; // variable to indicate none status
int normalState = 1; // variable to indicate normal status
int emergencyState = 2; // variable to indicate the emergency status
int breakTime = 3; // variable to indicate the break time
int holdState = 4; // variable to indicate the hold status
int mode; // varible to store current mode
unsigned long elapsedTime; // variable to store the elapsed time of current mode before emergency
int previousMode; // variable to store the previous mode before the emergency
unsigned long previousMillis = 0; // variable to store the previous time in millis
unsigned long currentMillis; // vraiable to store the current time in millis
bool waitConfirmation = false; // variable to indicate the chef confiramation incase of emergency
int PWM_Value = 0; // variable to set the duty cycle of PWM
int randCustomer = 0; // variable to store the customer random value
bool newOrder = false; // variable to indicate the new order
bool buttonFlag = false; // store the customer button state
void setup() {
// put your setup code here, to run once:
pinMode(green, OUTPUT); // set the digital pin as output
pinMode(red, OUTPUT); // set the digital pin as output
pinMode(white, OUTPUT); // set the digital pin as output
pinMode(yellow, OUTPUT); // set the digital pin as output
pinMode(customerButton, INPUT_PULLUP); // set the digital pin as input
pinMode(waiterButton, INPUT_PULLUP); // set the digital pin as input
pinMode(fishButton, INPUT_PULLUP); // set the digital pin as input
pinMode(escalopeButton, INPUT_PULLUP); // set the digital pin as input
pinMode(saladButton, INPUT_PULLUP); // set the digital pin as input
pinMode(chefButton, INPUT_PULLUP); // set the digital pin as input
pinMode(emergencyButton, INPUT_PULLUP); // set the digital pin as input
// attach external interrupt to digital pin
attachInterrupt(digitalPinToInterrupt(emergencyButton), emergency, FALLING);
lcd.begin(16, 2); // initialize LCD
Serial.begin(9600); // initialize serial
Serial3.begin(9600); // initialize serial
Serial.println("Start the system"); // serial monitor
Serial3.println("Start the system");
delay(1000);
status = normalCase;
lcd.clear(); // clear LCD
waitConfirmation = false;
mode = none;
}
void loop()
{
// put your main code here, to run repeatedly:
if (digitalRead(customerButton) == LOW && buttonFlag == false) {
waitingNumber++; // increment customer’s number
lcd.clear(); // clear LCD
lcd.setCursor(0, 0); // set the LCD position
lcd.print("C #: "); // display text on LCD
lcd.setCursor(6, 0); // set the LCD position
lcd.print(waitingNumber); // display text on LCD
lcd.setCursor(8, 0); // set the LCD position
lcd.print(" welcome"); // set the LCD position
lcd.setCursor(0, 1); // set the LCD position
lcd.print("let's Order"); // display text on LCD
buttonFlag = true;
delay(1000);
}
else{
buttonFlag = false;
}
if (digitalRead(waiterButton) == LOW) // check the waiter button
{
if (mode == none) {
if (counter < waitingNumber) // check there are waiting customers
{
counter++; // increment the counter
breakCounter++; // increment the break counter
newOrder = true;
delay(500);
} else {
lcd.clear(); // clear LCD
lcd.setCursor(0, 0); // set the LCD position
lcd.print("no customer"); // display text on LCD
lcd.setCursor(0, 1); // set the LCD position
lcd.print("are waiting"); // display text on LCD
delay(2000);
lcd.clear(); // clear LCD
return;
} // check the customer status should be none
if (breakCounter > 3 && mode != breakTime) // check the break status
{
breakCounter = 0; // reset the break counter
mode = breakTime; // change the mode of the system to break
previousMillis = millis(); // store the previous time
} else {
mode = normalState; // change the current mode to normal
previousMillis = millis(); // store the previous time
}
} else {
lcd.clear(); // clear LCD
lcd.setCursor(0, 0); // set the LCD position on row 0 column 1
lcd.print("chef "); // display text on LCD
lcd.setCursor(0, 1); // set the LCD position on row 1 column 1
lcd.print("still busy"); // display text on LCD
delay(2000);
lcd.clear(); // clear LCD
}
}
if (mode == normalState) {
if(newOrder == true)
{
lcd.clear(); // clear LCD
lcd.setCursor(0, 0); // set the LCD position
lcd.print("customer's #"); // display text on LCD
lcd.print(counter); // display text on LCD
lcd.print(" "); // display text on LCD
lcd.setCursor(0, 1); // set the LCD position
lcd.print("ordering"); // display text on LCD
while(digitalRead(fishButton) && digitalRead(escalopeButton) && digitalRead(saladButton));
if(digitalRead(fishButton) == LOW)
{
customerOrder = fish;
lcd.clear(); // clear LCD
lcd.setCursor(0, 0); // set the LCD position
lcd.print("fish is selected"); // display text on LCD
lcd.setCursor(0, 1); // set the LCD position
int potValue = analogRead(potentiometer);
if(potValue < 300){
fishWeight = weight750g;
lcd.print("weight 750 g "); // display text on LCD
}
else if(potValue >= 300 && potValue < 800){
fishWeight = weight500g;
lcd.print("weight 500 g "); // display text on LCD
}
else{
fishWeight = weight250g;
lcd.print("weight 250 g"); // display text on LCD
}
newOrder = false;
}
if(digitalRead(escalopeButton) == LOW)
{
customerOrder = Escalope;
lcd.clear(); // clear LCD
lcd.setCursor(0, 0); // set the LCD position
lcd.print("Escalope is"); // display text on LCD
lcd.setCursor(0, 1); // set the LCD position
lcd.print("selected"); // display text on LCD
newOrder = false;
}
if(digitalRead(saladButton) == LOW)
{
customerOrder = salad;
lcd.clear(); // clear LCD
lcd.setCursor(0, 0); // set the LCD position
lcd.print("salad is"); // display text on LCD
lcd.setCursor(0, 1); // set the LCD position
lcd.print("selected"); // display text on LCD
newOrder = false;
}
while(digitalRead(chefButton) == HIGH); // check the chef button
previousMillis = millis(); // store the previous time
}
lcd.clear();
lcd.setCursor(0, 0); // set the LCD position
lcd.print("#"); // display text on LCD
lcd.print(counter); // display text on LCD
lcd.setCursor(5, 0); // set the LCD position
lcd.print("meal being "); // display text on LCD
lcd.setCursor(0, 1); // set the LCD position
lcd.print("prepared-"); // display text on LCD
currentMillis = millis(); // update the value of the current time
int sec = (currentMillis - previousMillis) / 1000; // calculate the time in sec
char buffer[8];
sprintf(buffer, "%d sec ", sec); // convert the time to string array
lcd.setCursor(11, 1); // set the LCD position
lcd.print(buffer); // display text on LCD
if(customerOrder == fish)
digitalWrite(green, HIGH); // turn on the green LED
if(customerOrder == Escalope)
digitalWrite(yellow, HIGH); // turn on the yellow LED
if(customerOrder == salad)
digitalWrite(red, HIGH); // turn on the red LED
if ((currentMillis - previousMillis) > customerOrder) // check the minimum interval of the break time is completed
{
delay(500);
mode = none; // change the system mode to none
customerOrder = none;
lcd.clear(); // clear LCD
lcd.setCursor(0, 0); // set the LCD position
lcd.print("customer's #"); // display text on LCD
lcd.print(counter); // display text on LCD
lcd.print(" "); // display text on LCD
lcd.setCursor(0, 1); // set the LCD position
lcd.print("being served"); // display text on LCD
digitalWrite(red, LOW); // turn off the red LED
digitalWrite(yellow, LOW); // turn off the yellow LED
digitalWrite(green, LOW); // turn off the green LED
delay(2000);
lcd.clear(); // clear LCD
}
delay(200);
}
else if (mode == breakTime) {
lcd.setCursor(0, 0); // set the LCD position
lcd.print("break time "); // display text on LCD
lcd.setCursor(0, 1); // set the LCD position
lcd.print(" "); // display text on LCD
currentMillis = millis(); // update the value of the current time
int sec = (currentMillis - previousMillis) / 1000;
char buffer[8];
sprintf(buffer, "(%d sec) ", sec);
lcd.setCursor(8, 1); // set the LCD position
lcd.print(buffer); // display text on LCD
digitalWrite(red, LOW); // turn off the red LED
digitalWrite(yellow, LOW); // turn off the yellow LED
digitalWrite(green, LOW); // turn off the green LED
if ((currentMillis - previousMillis) > chefBreak) // check the minimum interval of the break time is completed
{
delay(500);
mode = normalState;
previousMillis = millis();
}
}
else if (mode == emergencyState) {
if(randCustomer == fish)
{
lcd.setCursor(0, 0); // set the LCD position
lcd.print("home delivery"); // display text on LCD
lcd.setCursor(0, 1); // set the LCD position
lcd.print("fish meal "); // display text on LCD
emergencyTime = randCustomer;
randCustomer = 0;
delay(2000);
}
else if(randCustomer == Escalope)
{
lcd.setCursor(0, 0); // set the LCD position
lcd.print("home delivery"); // display text on LCD
lcd.setCursor(0, 1); // set the LCD position
lcd.print("Escalope meal "); // display text on LCD
emergencyTime = randCustomer;
randCustomer = 0;
delay(2000);
}
else if(randCustomer == salad)
{
lcd.setCursor(0, 0); // set the LCD position
lcd.print("home delivery"); // display text on LCD
lcd.setCursor(0, 1); // set the LCD position
lcd.print("salad "); // display text on LCD
emergencyTime = randCustomer;
randCustomer = 0;
delay(2000);
}
lcd.setCursor(0, 0); // set the LCD position
lcd.print("customer's #"); // display text on LCD
lcd.print(counter); // display text on LCD
lcd.print(" "); // display text on LCD
lcd.setCursor(0, 1); // set the LCD position
lcd.print("on hold ");// display text on LCD
currentMillis = millis(); // update the value of the current time
int sec = (currentMillis - previousMillis) / 1000; // calculate the time in sec
PWM_Value = sec * 10;
char buffer[8];
sprintf(buffer, "(%d sec) ", sec); // convert the time to string array
lcd.setCursor(8, 1); // set the LCD position on row 1 column 9
lcd.print(buffer); // display text on LCD
digitalWrite(red,LOW); // turn off red LED
digitalWrite(yellow,LOW); // turn off yellow LED
digitalWrite(green,LOW); // turn off green LED
analogWrite(white, PWM_Value); // turn on white LED with PWM
if ((currentMillis - previousMillis) > emergencyTime) // check the minimum interval of the emergency time is completed
{
delay(500);
analogWrite(white, 0); // turn off white LED
mode = previousMode; // change the current mode to the previous mode
previousMillis = millis() - elapsedTime; // update the prevoius time
}
}
else if (mode == none && waitingNumber == 0) {
lcd.setCursor(0, 0); // set the LCD position
lcd.print("Welcome "); // display text on LCD
lcd.setCursor(0, 1); // set the LCD position
lcd.print("Let's Order "); // display text on LCD
}
else if (mode == none && waitingNumber != 0) {
lcd.setCursor(0, 0); // set the LCD position
lcd.print("Welcome "); // display text on LCD
lcd.setCursor(0, 1); // set the LCD position
lcd.print("Let's Order "); // display text on LCD
}
if(waitConfirmation == true) // check the emergency
{
Serial3.println(" An emergency case has arrived; press (y) to begin it."); // inform the doctor with the coming emergency case
waitConfirmation = false; // set the emergency confirmation
}
if(Serial.available()) // check serial monitor in Arduino IDE
{
char x = Serial.read(); // read serial data from chef
if((x == 'y' || x == 'Y') && waitConfirmation == false) // check 'y' is received and emergency case is true
{
PWM_Value = 0;
int ran = random(0, 3);
if (ran == 0) // generate random number (0,1 or 2)
randCustomer = fish; // fish is selceted
else if (ran == 1)
randCustomer = Escalope; // Escalope is selected
else if (ran == 2)
randCustomer = salad; // salad is selected
delay(100);
if (mode != 0) // check the current mode is none
{
elapsedTime = millis() - previousMillis; // store the elapsed time
previousMode = mode; // store the previous mode
}
else
{
previousMode = none; // clear the previous mode
elapsedTime = 0; // clear the elapsed time
}
mode = emergencyState; // change the system mode to emergency
previousMillis = millis(); // store the previous time
}
}
}
void emergency() {
waitConfirmation = true;
}