/*
Assignment 2 Implementation
Within this Code I chose IOT to show a real life issue and solve here
so I used a Retail store system of checking wether the stock is low to alert the employees.
Called 'Retail Iot Smart Shelf' keeping track of the distance of the stock and sensor to detect wether
it is low or not
*/
// The Following are the Pin definitions
// These are the Ultrasonic Sensor Pins
const int TRIG_PIN = 5;
const int ECHO_PIN = 18;
// The LED Pin
const int LED_PIN = 14;
// The Buzzer Pin
const int BUZZER_PIN = 27;
// Manual Reset Button Which will cause the system to restart
const int BUTTON_PIN = 4;
// The Stock Thresholds that will be alerted
const int LOW_STOCK_D = 20; // >20 cm = LOW
const int MEDIUM_STOCK_D = 10; // 10–20 cm = MEDIUM
// Track whether alarm is active
bool alarmActive = false;
// This section reads the Ultrasonic Distance.
float readDistance() {
digitalWrite(TRIG_PIN, LOW);
delayMicroseconds(2);
digitalWrite(TRIG_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW);
long duration = pulseIn(ECHO_PIN, HIGH);
float distance_cm = (duration * 0.034) / 2.0;
return distance_cm;
}
// The Pins have been defined already so I will SetUp the environment
void setup() {
Serial.begin(115200);
// Pin Modes
pinMode(TRIG_PIN, OUTPUT);
pinMode(ECHO_PIN, INPUT);
pinMode(LED_PIN, OUTPUT);
pinMode(BUZZER_PIN, OUTPUT);
pinMode(BUTTON_PIN, INPUT_PULLUP);
digitalWrite(LED_PIN, LOW);
noTone(BUZZER_PIN);
Serial.println("Retail IoT Smart Shelf - System Started");
}
// The Loop
void loop() {
float distance = readDistance();
bool buttonPressed = (digitalRead(BUTTON_PIN) == LOW);
// The Alarm will reset once the button is pressed
if (buttonPressed) {
alarmActive = false;
digitalWrite(LED_PIN, LOW);
noTone(BUZZER_PIN);
Serial.println("Alarm Reset - Button Pressed");
delay(500);
}
// This will detect the Stock level in order to inform the employees of the status
String status;
if (distance > LOW_STOCK_D) {
status = "LOW";
}
else if (distance > MEDIUM_STOCK_D && distance <= LOW_STOCK_D) {
status = "MEDIUM";
}
else {
status = "OK";
}
// Once the stock is detected it will be displayed
if (status == "LOW" && !alarmActive) {
// The led light will be turned and the buzzer will beep
digitalWrite(LED_PIN, HIGH);
tone(BUZZER_PIN, 1500, 300);
Serial.print("LOW STOCK! Distance = ");
Serial.print(distance);
Serial.println(" cm --> PLEASE RESTOCK");
alarmActive = true; // This will stop the alarm from repeating constantly
}
else if (status == "MEDIUM") {
// If it is medium the LED will blink slow
digitalWrite(LED_PIN, HIGH);
delay(150);
digitalWrite(LED_PIN, LOW);
delay(150);
Serial.print("Medium stock level. Distance = ");
Serial.print(distance);
Serial.println(" cm");
}
else if (status == "OK") {
// the led light will turn off and no buzzer as the stock is okay
digitalWrite(LED_PIN, LOW);
noTone(BUZZER_PIN);
Serial.print("Stock OK. Distance = ");
Serial.print(distance);
Serial.println(" cm");
alarmActive = false; // the alarm reset state
}
delay(400);
}