/* This is just a rudimentary code to *hopefully* attach to Marek's overall code. Using WOKWI, I as able
to simulate and verify the function of it. This will indicate if the locker is full, empty, or open.
Not sure how to implement a code to indicate that someone filled the locker but left it open, unless
we use an additional sensor. But more sensors = more $$$. */
#define PIN_TRIG 2
#define PIN_ECHO 3
#define BLUE_LED 7
#define GREEN_LED 6
#define RED_LED 5
const int FULL_LOCKER = 10; // Distance in CM when considered full.
const int EMPTY_LOCKER = 31; // Distance in CM when considered empty.
const int OPEN_DOOR = 35; // Distance in CM when considered open!
void setup() {
pinMode(PIN_TRIG, OUTPUT);
pinMode(PIN_ECHO, INPUT);
pinMode(BLUE_LED, OUTPUT);
pinMode(GREEN_LED, OUTPUT);
pinMode(RED_LED, OUTPUT);
Serial.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
long duration, distance;
digitalWrite(PIN_TRIG, LOW);
delayMicroseconds(2);
digitalWrite(PIN_TRIG, HIGH);
delayMicroseconds(10);
digitalWrite(PIN_TRIG, LOW);
duration = pulseIn(PIN_ECHO, HIGH);
distance = (duration / 2) / 29.1;
/* Lets say that the distance between the sensor and the
the compartment door is 12 inches. We could use an if/else
statement that will light the green LED if the locker is empty,
the red LED if the locker is full, and the blue LED if the door
is open.
*/
if (distance < FULL_LOCKER){
digitalWrite(RED_LED, HIGH);
digitalWrite(GREEN_LED, LOW);
digitalWrite(BLUE_LED, LOW);
}
else if (distance > OPEN_DOOR){
digitalWrite(BLUE_LED, HIGH);
digitalWrite(GREEN_LED, LOW);
digitalWrite(RED_LED, LOW);
}
else if (distance == EMPTY_LOCKER){
digitalWrite(GREEN_LED, HIGH);
digitalWrite(BLUE_LED, LOW);
digitalWrite(RED_LED, LOW);
}
Serial.print("Distance: "); // Displays the distance in centimeters.
Serial.print(distance);
Serial.println(" cm");
Serial.print("Distance in inches: "); // Converts distance from centimeters to display in inches.
Serial.print(distance / 2.54);
Serial.println(" inches");
delay(500); // Delay between measurements.
}