// Define pins
const int flexSensorPin = A0; // Analog pin for the flex sensor
const int motorPin = 9; // Digital pin for the vibration motor
// Variables
int flexValue = 0; // Variable to store flex sensor value
int sensitivityThreshold = 300; // Threshold to activate the motor
unsigned long lastSendTime = 0; // Last time a message was sent
const unsigned long sendInterval = 60000; // 1 minute interval in milliseconds
void setup() {
// Initialize Serial for Bluetooth communication and debugging
Serial.begin(9600);
// Set pin modes
pinMode(flexSensorPin, INPUT); // Flex sensor as input
pinMode(motorPin, OUTPUT); // Motor as output
}
void loop() {
// Read the flex sensor value
flexValue = analogRead(flexSensorPin);
// Print the flex sensor value to the Serial Monitor for debugging
Serial.print("Flex Sensor Value: ");
Serial.println(flexValue);
// Check if the flex sensor value exceeds the threshold
if (flexValue >= sensitivityThreshold) {
// Turn ON the vibration motor
digitalWrite(motorPin, HIGH);
Serial.println("Vibration Motor ON");
// Check if it’s time to send a Bluetooth notification
unsigned long currentTime = millis();
if (currentTime - lastSendTime >= sendInterval) {
// Send "Bad posture detected" message over Bluetooth
Serial.println("Bad posture detected");
lastSendTime = currentTime; // Update the last send time
}
} else {
// Turn OFF the vibration motor
digitalWrite(motorPin, LOW);
Serial.println("Vibration Motor OFF");
}
// Small delay for stability
delay(500);
}