int green=21;
int red=18;
int motion=14;
int ldr=33;
int buzzer=19;
int motion_value=0;
int ldr_value=0;
int sonic_echo=27;
int sonic_trig=26;
int distance_threshold=250;
float duration_us, weight_g;
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
Serial.println("Hello, ESP32!");
pinMode(green,OUTPUT);
pinMode(red, OUTPUT);
pinMode(motion, INPUT);
pinMode(ldr, INPUT);
pinMode(sonic_echo, INPUT);
pinMode(sonic_trig, OUTPUT);
pinMode(buzzer, OUTPUT);
}
void loop() {
motion_value = digitalRead(motion);
Serial.println("Motion value: "+ String(motion_value));
ldr_value = analogRead(ldr);
Serial.println("Ldr value: "+ String(ldr_value));
// to sense motion
motion_value = digitalRead(motion);
if(motion_value==HIGH && ldr_value>=1001)
{
digitalWrite(green, HIGH);
}
else
{
digitalWrite(green, LOW);
}
//to sense coffee making
if(green==HIGH)
{
digitalWrite(sonic_trig, HIGH);
digitalWrite(sonic_echo, HIGH);
}
else
{
digitalWrite(sonic_trig, LOW);
digitalWrite(sonic_echo, LOW);
}
//to sense Filled Cup
digitalWrite(sonic_trig, HIGH);
delayMicroseconds(15);
digitalWrite(sonic_trig, LOW);
duration_us = pulseIn(sonic_echo, HIGH);
weight_g = 0.02 * duration_us;
if (weight_g > distance_threshold)
{
digitalWrite(red, HIGH);
tone(buzzer,200); // turn on LED
}
else
{
digitalWrite(red, LOW);
noTone(buzzer); // turn off LED
}
// print the value to Serial Monitor
Serial.print("weight: ");
Serial.print(weight_g);
Serial.println(" g");
}
//in order for the green light to turn on, turn off the red light as to indicate that the coffee making process is starting and hasnt finished