#include <LiquidCrystal_I2C.h>
#include <Adafruit_MPU6050.h>
#include <Adafruit_Sensor.h>
#include <Wire.h>
LiquidCrystal_I2C lcd(0x27, 20, 4);
Adafruit_MPU6050 mpu;
int relay = 18;
int microphone = 35;
int ledtelegram = 32;
int buttonState1 = LOW; // Current state of the first button (Mic)
int lastButtonState1 = LOW; // Previous state of the first button
// The debounce time; increase if the output flickers
unsigned long debounceDelay = 50;
// Timers for debouncing
unsigned long lastDebounceTime1 = 0;
unsigned long previousMillis = 0;
const long interval = 1000; //1 sec delay
float car_temp;
float x = 0;
float y = 0;
float z = 0;
bool statussend = 0;
void setup() {
Serial.begin(9600);
lcd.init();
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print("Baby Car Monitoring");
pinMode(relay, OUTPUT);
pinMode(microphone, INPUT_PULLUP);
pinMode(ledtelegram, OUTPUT);
// Try to initialize!
if (!mpu.begin()) {
Serial.println("Failed to find MPU6050 chip");
while (1) {
delay(10);
}
}
Serial.println("MPU6050 Found!");
}
void loop() {
//##############Vibration Sensor#################################
sensors_event_t a, g, temp;
mpu.getEvent(&a, &g, &temp);
x = g.gyro.x;
y = g.gyro.y;
z = g.gyro.z;
if ((x != 0 || y != 0 || z != 0)&& statussend == 0) { //detect vibration on axis x,y,z
statussend = 1;
lcd.clear();
Serial.println("Vibration detected");
digitalWrite(ledtelegram, HIGH);
lcd.setCursor(0, 0);
lcd.print(" Vibration detected ");
lcd.setCursor(0, 1);
lcd.print(" Send 2 Telegram ");
digitalWrite(relay, HIGH); //alarm triggered
delay(5000);
digitalWrite(ledtelegram, LOW);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Baby Car Monitoring");
lcd.setCursor(0, 1);
lcd.print(" Vibration detected ");
}
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
previousMillis = currentMillis;
lcd.setCursor(0, 2);
lcd.print(" Car Temp: ");
car_temp = temp.temperature;
lcd.setCursor(10, 2);
lcd.print(car_temp);
lcd.write(223); // Code for the degree symbol in many character sets
lcd.print("C");
Serial.print("Rotation X: ");
Serial.print(g.gyro.x);
Serial.print(", Y: ");
Serial.print(g.gyro.y);
Serial.print(", Z: ");
Serial.print(g.gyro.z);
Serial.println(" rad/s");
}
//################################################################
//##############microphone button debounce########################
// Read the state of the first button
int reading1 = digitalRead(microphone);
// Check if the button state has changed
if (reading1 != lastButtonState1) {
lastDebounceTime1 = millis();
}
// If the button state has been stable for longer than the debounce delay, update the button state
if ((millis() - lastDebounceTime1) > debounceDelay) {
if (reading1 != buttonState1) {
buttonState1 = reading1;
if (buttonState1 == HIGH) {
Serial.println("Button mic pressed");
digitalWrite(ledtelegram, HIGH);
lcd.setCursor(0, 0);
lcd.print(" Voice detected ");
lcd.setCursor(0, 1);
lcd.print(" Send 2 Telegram ");
digitalWrite(relay, HIGH); //alarm triggered
delay(5000);
digitalWrite(ledtelegram, LOW);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Baby Car Monitoring");
lcd.setCursor(0, 1);
lcd.print(" Voice detected ");
}
}
}
lastButtonState1 = reading1;
//############################################################
}