#define BLYNK_TEMPLATE_ID "YourTemplateID" // Replace with your Blynk Template ID
#define BLYNK_TEMPLATE_NAME "YourTemplateName" // Replace with your Blynk Template Name
#include <Wire.h>
#include <RTClib.h>
#include <LiquidCrystal.h>
#include <BlynkSimpleEsp32.h>
#include <MPU6050.h>
// Vector structure
struct Vector {
float XAxis;
float YAxis;
float ZAxis;
};
// Components
RTC_DS3231 rtc;
MPU6050 mpu;
LiquidCrystal lcd(13, 14, 15, 2, 4, 5); // Adjust pins as per your setup
// Pins
const int buzzerPin = 18; // Changed buzzer GPIO pin to 18
// Blynk Auth Token
char auth[] = "YOUR_BLYNK_AUTH_TOKEN"; // Replace with your Blynk Auth Token
char ssid[] = "Your_SSID"; // Replace with your Wi-Fi SSID
char pass[] = "Your_PASSWORD"; // Replace with your Wi-Fi Password
// Variables
int dailyPoints = 0; // Points for the current day
int monthlyPoints = 0; // Total points for the month
unsigned long lastBuzzTime = 0;
const int maxDailyPoints = 30;
const int maxMonthlyPoints = 900;
const int routinesPerDay = 3;
// Routines
const String routines[] = {"Study Time", "Play Time", "Rest Time"};
int currentRoutineIndex = 0;
void setup() {
Serial.begin(9600);
pinMode(buzzerPin, OUTPUT);
// Initialize LCD
lcd.begin(16, 2);
lcd.print("Initializing...");
delay(2000);
lcd.clear();
// Initialize RTC
if (!rtc.begin()) {
lcd.print("RTC Error!");
while (1);
}
// Initialize MPU6050
Wire.begin();
mpu.initialize();
if (!mpu.testConnection()) {
lcd.print("MPU6050 Error!");
while (1);
}
// Blynk setup
Blynk.begin(auth, ssid, pass);
// Set initial date and time
rtc.adjust(DateTime(2024, 12, 20, 12, 0, 0)); // Set to Dec 20, 2024, 12:00:00
}
bool isFollowingRoutine(Vector accel, Vector gyro, const String &routine) {
if (routine == "Study Time") {
return abs(accel.XAxis) < 0.5 && abs(gyro.YAxis) < 0.5;
} else if (routine == "Play Time") {
return abs(gyro.XAxis) > 1.0 || abs(gyro.YAxis) > 1.0;
} else if (routine == "Rest Time") {
return abs(accel.XAxis) < 0.2 && abs(accel.YAxis) < 0.2 && abs(accel.ZAxis - 9.8) < 0.5;
}
return false;
}
void updateRoutine() {
int16_t ax, ay, az, gx, gy, gz;
mpu.getAcceleration(&ax, &ay, &az);
mpu.getRotation(&gx, &gy, &gz);
Vector accel = {ax / 16384.0, ay / 16384.0, az / 16384.0};
Vector gyro = {gx / 131.0, gy / 131.0, gz / 131.0};
String routine = routines[currentRoutineIndex];
lcd.clear();
lcd.print(routine);
lcd.setCursor(0, 1);
if (isFollowingRoutine(accel, gyro, routine)) {
dailyPoints += 10;
lcd.print("Points: " + String(dailyPoints) + "/30");
digitalWrite(buzzerPin, LOW);
} else {
unsigned long currentMillis = millis();
if (currentMillis - lastBuzzTime < 6000) {
digitalWrite(buzzerPin, HIGH);
lcd.print("Warning!");
} else {
digitalWrite(buzzerPin, LOW);
lastBuzzTime = millis();
}
dailyPoints -= 5;
}
}
void endOfDay() {
monthlyPoints += dailyPoints;
dailyPoints = 0;
currentRoutineIndex = (currentRoutineIndex + 1) % routinesPerDay;
}
void endOfMonth() {
lcd.clear();
if (monthlyPoints >= 750) { // 75% of total points
lcd.print("Congrats!");
} else {
lcd.print("Try Again!");
}
lcd.setCursor(0, 1);
lcd.print("Total: " + String(monthlyPoints) + "/900");
monthlyPoints = 0;
}
void loop() {
Blynk.run();
DateTime now = rtc.now();
lcd.setCursor(0, 0);
lcd.print(now.hour() + ":" + now.minute());
updateRoutine();
// Check if end of day
if (now.hour() == 23 && now.minute() == 59 && now.second() == 59) {
endOfDay();
}
// Check if end of month
if (now.day() == 30 && now.hour() == 23 && now.minute() == 59 && now.second() == 59) {
endOfMonth();
}
delay(1000);
}
This is my code as I am making a watch that will help children to maintain their routine daily routine is present has now a desperate tension how to maintain their child routing so this watch will help him according to this watch there will be three routine bedtime with the breast time play time and study time is study time I have used MPU 6050 and LCD 16 into 2 without I 2c and and if the child doesn't do his work on time the buffer well bus and according to it point will be deducted or added and congratulation or morning message welcome in a day they are 30 points and the point weld reset after the day is over and it is calculated to the total mark at the end of the month and the end of the month is the mark matlab is the points code is mode then 75% total code is 900 then congratulation message welcome and reward system and the child will get some reward from the parents will come will come this will have the children to maintain their daily routine and the work to do they work on time so has to achieve the reward and the artificial will display the time on my LCD matlab on the watch and this is how it will function and more detail are there in the code you can make but the problem is coming that the sensor is not behaving and buzzing I have check put the components separately what is separately there working well but when put together they are not working as the same size reading something and the bus not bus you check the code and please provide me as soon as possible as on 26th December I have to submit my project like during study time if there is movement by they should sound in during play time no movement but I should sound but sometime the person I really upload the code the continuously bus without stopping the buzzer continuously give sound without stopping then otherwise sometime it behave ok if I move the sensor then only sound come but after 1 minute again this problem start so please check and give me the correct code .