/*
*************
***READ ME***
*************
What is what?
~~~~~~~~~~~~~
The 4 buttons represent the 4 destination inputs
The slide switch represents the IR sensor input
The 4 LEDs represent the selected bays
How do i use the system?
~~~~~~~~~~~~~~~~~~~~~~~~
keyboard shortcuts:
- Hold 1: Red (Bay A)
- Hold 2: Yellow (Bay B)
- Hold 3: Green (Bay C)
- Hold 4: Blue (Bay D)
1. Hold down the button that correspond to the wanted
output using the keyboard shortcuts.
2. Flip the switch to trigger the sensor.
3. The Serial output and LCD should show the randomly generated parcel ID,
the routed bay, and the status
4. Flip the switch back to reset the sensor for the next parcel.
*/
#include <LiquidCrystal.h>
// Arduino Pin Definition
int button1 = A0;
int button2 = A1;
int button3 = A2;
int button4 = A3;
int IRswitch = 6;
int buzzer = 7;
int led1 = 2;
int led2 = 3;
int led3 = 4;
int led4 = 5;
//State Variables
int but1Status = 0;
int but2Status = 0;
int but3Status = 0;
int but4Status = 0;
int IRStatus = 0;
int lastIRStatus = HIGH;
//Variable for randomly generated parcel ID
long parcelID;
// Defining pins and instantiating LCD
const int rs = 8, en = 9, d4 = 10, d5 = 11, d6 = 12, d7 = 13;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
void setup() {
//Starting + printing to serial monitor
Serial.begin(9600);
Serial.println("System Ready. Waiting for parcels...");
//Starting + displaying on LCD
lcd.begin(16, 2);
lcd.print("System Ready.");
lcd.setCursor(0, 1);
lcd.print("Waiting...");
//Defining buttons and switch as inputs
pinMode(button1, INPUT_PULLUP);
pinMode(button2, INPUT_PULLUP);
pinMode(button3, INPUT_PULLUP);
pinMode(button4, INPUT_PULLUP);
pinMode(IRswitch, INPUT_PULLUP);
//Defining LEDs and Buzzer as outputs
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(led3, OUTPUT);
pinMode(led4, OUTPUT);
pinMode(buzzer, OUTPUT);
}
void loop() {
//Read the button inputs
but1Status = digitalRead(button1);
but2Status = digitalRead(button2);
but3Status = digitalRead(button3);
but4Status = digitalRead(button4);
IRStatus = digitalRead(IRswitch);
if (IRStatus != lastIRStatus) {
//Debounce
delay(50);
//Read the new sensor input
IRStatus = digitalRead(IRswitch);
//Checking to see if the switch is flipped
if (IRStatus == LOW) {
//reset all LEDs and buzzer to be off
digitalWrite(led1, LOW);
digitalWrite(led2, LOW);
digitalWrite(led3, LOW);
digitalWrite(led4, LOW);
digitalWrite(buzzer, LOW);
parcelID = random(1000, 10000);
// Check button 1 condition
if (but1Status == LOW){
digitalWrite(led1, HIGH);
digitalWrite(buzzer, HIGH);
Serial.print("Parcel ID: ");
Serial.println(parcelID);
Serial.println("Bay: 1");
lcd.clear();
lcd.print("Parcel ID: ");
lcd.print(parcelID);
lcd.setCursor(0, 1);
lcd.print("Bay:1 ");
lcd.print("Status:OK");
}
// Check button 2 condition
if (but2Status == LOW){
digitalWrite(led2, HIGH);
digitalWrite(buzzer, HIGH);
Serial.print("Parcel ID: ");
Serial.println(parcelID);
Serial.println("Bay: 2");
lcd.clear();
lcd.print("Parcel ID: ");
lcd.print(parcelID);
lcd.setCursor(0, 1);
lcd.print("Bay:2 ");
lcd.print("Status:OK");
}
// Check button 1 condition
if (but3Status == LOW){
digitalWrite(led3, HIGH);
digitalWrite(buzzer, HIGH);
Serial.print("Parcel ID: ");
Serial.println(parcelID);
Serial.println("Bay: 3");
lcd.clear();
lcd.print("Parcel ID: ");
lcd.print(parcelID);
lcd.setCursor(0, 1);
lcd.print("Bay:3 ");
lcd.print("Status:OK");
}
// Check button 1 condition
if (but4Status == LOW){
digitalWrite(led4, HIGH);
digitalWrite(buzzer, HIGH);
Serial.print("Parcel ID: ");
Serial.println(parcelID);
Serial.println("Bay: 4");
lcd.clear();
lcd.print("Parcel ID: ");
lcd.print(parcelID);
lcd.setCursor(0, 1);
lcd.print("Bay:4 ");
lcd.print("Status:OK");
}
}
// Update lastIRStatus ONLY when a state change occurs
lastIRStatus = IRStatus;
}
}