Help with my code for making my my project named Routine Ranger

#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 .

Please read and follow the advice given in How to get the best out of this forum, paying special attention to the first paragaph in the Code Problems section.

Also, very few people are going to go to the trouble of wading through that monolithic block of text. It would be in your best interest to use sentences and paragraphs.

3 Likes

Wow!
21 lines of text without a single comma!
I think this is the longest sentence I've seen on this forum... You are our champion!

Well at least he did start with a capital letter, and ended with a full stop / period.

1 Like

A breathtaking challenge.

Hahaha. You need to make the "Routine Ranger" for yourself.

One for all, all for one.

I fed that word salad to chatGPT and after it made me beg, it produced this:

Project Overview:

  • Goal: Build a children's watch to help them maintain a daily routine with specific activities: Bedtime, Playtime, and Study Time.
  • Components:
    • MPU 6050 (Accelerometer and Gyroscope sensor)
    • LCD 16x2 (to display time and messages)
    • Buzzer (to alert the child about their activities)
  • Features:
    1. Points System:
    • The child starts each day with 30 points.
    • Points are deducted or added based on whether the child completes their routine tasks on time (study time, play time, bedtime).
    • Points reset daily and accumulate for a monthly total.
    • At the end of the month, the child’s performance is assessed, and rewards are given based on their points.
    1. Sensor Functionality:
    • During study time, the sensor should detect movement and trigger a buzzer if the child is not still.
    • During playtime, the sensor should not trigger the buzzer.
    • Congratulatory messages appear when the child achieves certain point milestones.

Problem Description:

  • The sensor (MPU 6050) is not behaving consistently.
  • When the code is uploaded, sometimes the buzzer sounds continuously, even when there is no movement or activity.
  • Other times, it works as expected: the buzzer sounds only when there is movement, but after a minute or so, the buzzer starts buzzing without stopping.
  • You’ve tested the components separately, and they work fine, but when put together, the sensor seems to malfunction.

Specific Issues:

  1. Continuous Buzzer Sounding: The buzzer is going off continuously in some cases, even when it shouldn’t.
  2. Erratic Sensor Behavior: The MPU 6050 sensor is giving incorrect readings or triggering the buzzer unexpectedly.
  3. Intermittent Functionality: Sometimes the sensor behaves correctly (detects movement during study time), but other times it doesn't (buzzer continuously buzzing).

What You Need Help With:

  • Debugging the code that controls the MPU 6050 sensor and buzzer.
  • Ensuring the sensor detects movement only during study time, and the buzzer only sounds when needed.
  • Fixing the issue of the buzzer continuously sounding or not sounding at the right times.

What to Check:

  • Sensor Readings: Ensure that the MPU 6050 readings are being processed correctly. Sometimes faulty readings or unfiltered data can cause erratic behavior.
  • Code Logic: Check the timing and conditions under which the buzzer is triggered. You may need to adjust thresholds for movement detection (e.g., requiring more significant motion before triggering the buzzer).
  • Interrupts/Delays: Ensure the program isn't caught in a loop or constantly triggering the buzzer. Adding proper delays or checks to only activate the buzzer under specific conditions might help.

HTH

a7

You got my comment displayed before I had a chance!

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.