#include <Wire.h>
#include <LiquidCrystal.h>
#include <math.h>
// ----- Sensor Addresses -----
const uint8_t MPU_ADDR = 0x68;
// ----- NTC Sensor Pin -----
#define NTC_PIN PA0
// ----- Motor Relay Pins -----
#define RELAY_X_PIN PB13
#define RELAY_Y_PIN PB14
// ----- Buzzer Pin -----
#define BUZZER_PIN PB15
// ----- Thresholds -----
const float ACCEL_THRESHOLD = 0.20; // g units
const float TEMP_THRESHOLD = 30.0; // °C
// NTC constants
const float B = 3950.0; // Beta coefficient
// LCD pin mapping (RS, EN, D4, D5, D6, D7)
const int lcdRS = PB0;
const int lcdEN = PB1;
const int lcdD4 = PB10;
const int lcdD5 = PB11;
const int lcdD6 = PA8;
const int lcdD7 = PA9;
LiquidCrystal lcd(lcdRS, lcdEN, lcdD4, lcdD5, lcdD6, lcdD7);
// Simulated 20 readings for ax and ay (some below and some above threshold)
const int NUM_READINGS = 20;
float ax_readings[NUM_READINGS] = {
0.1, 0.15, 0.3, 0.25, 0.18, 0.05, 0.22, 0.19, 0.21, 0.3,
0.12, 0.14, 0.28, 0.1, 0.17, 0.23, 0.2, 0.15, 0.27, 0.11
};
float ay_readings[NUM_READINGS] = {
0.05, 0.22, 0.18, 0.25, 0.14, 0.19, 0.31, 0.1, 0.17, 0.2,
0.12, 0.28, 0.15, 0.22, 0.11, 0.3, 0.09, 0.21, 0.16, 0.2
};
int currentIndex = 0;
float readNTCTemperature() {
int adcValue = analogRead(NTC_PIN);
float tempC = 1 / (log(1 / (1023. / adcValue - 1)) / B + 1.0 / 298.15) - 273.15;
return tempC;
}
void setup() {
Serial1.begin(115200);
// Set LCD pins as output
pinMode(lcdRS, OUTPUT);
pinMode(lcdEN, OUTPUT);
pinMode(lcdD4, OUTPUT);
pinMode(lcdD5, OUTPUT);
pinMode(lcdD6, OUTPUT);
pinMode(lcdD7, OUTPUT);
Wire.begin(PB7, PB6);
Wire.beginTransmission(MPU_ADDR);
Wire.write(0x6B);
Wire.write(0);
Wire.endTransmission();
pinMode(RELAY_X_PIN, OUTPUT);
pinMode(RELAY_Y_PIN, OUTPUT);
pinMode(BUZZER_PIN, OUTPUT);
pinMode(NTC_PIN, INPUT_ANALOG);
digitalWrite(RELAY_X_PIN, LOW);
digitalWrite(RELAY_Y_PIN, LOW);
digitalWrite(BUZZER_PIN, LOW);
delay(50); // Small delay before LCD init
lcd.begin(16, 2);
lcd.clear();
lcd.print("System Started");
delay(2000);
lcd.clear();
}
void loop() {
float ax_g = ax_readings[currentIndex];
float ay_g = ay_readings[currentIndex];
currentIndex = (currentIndex + 1) % NUM_READINGS;
float tempC = readNTCTemperature();
// Control relays and buzzer
if (abs(ax_g) > ACCEL_THRESHOLD) {
digitalWrite(RELAY_X_PIN, HIGH);
digitalWrite(BUZZER_PIN, HIGH);
} else {
digitalWrite(RELAY_X_PIN, LOW);
}
if (abs(ay_g) > ACCEL_THRESHOLD) {
digitalWrite(RELAY_Y_PIN, HIGH);
digitalWrite(BUZZER_PIN, HIGH);
} else {
digitalWrite(RELAY_Y_PIN, LOW);
}
if (abs(ax_g) <= ACCEL_THRESHOLD && abs(ay_g) <= ACCEL_THRESHOLD) {
digitalWrite(BUZZER_PIN, LOW);
}
if (tempC > TEMP_THRESHOLD) {
digitalWrite(BUZZER_PIN, HIGH);
}
// Display on LCD
lcd.setCursor(0, 0);
lcd.print("Ax:");
lcd.print(ax_g, 2);
lcd.print(" Ay:");
lcd.print(ay_g, 2);
lcd.print(" "); // Clear leftover chars
lcd.setCursor(0, 1);
lcd.print("T:");
lcd.print(tempC, 1);
lcd.print("C ");
lcd.print("X:");
lcd.print(digitalRead(RELAY_X_PIN) ? "1 " : "0");
lcd.print("Y:");
lcd.print(digitalRead(RELAY_Y_PIN) ? "1" : "0");
lcd.print(" "); // Clear leftover chars
delay(500);
}