ARDUINO CODE
1.BASIC HELLO WORLD
code:-
void setup() {
// Start the serial communication at 9600 baud rate
Serial.begin(9600);
void loop() {
// Print "Hello, World!" to the Serial Monitor
Serial.println("Hello, World!");
// Wait for 1 second before sending the message again
delay(1000);
2.LED INTERFACE
// Define the pin where the LED is connected
const int ledPin = 8;
void setup() {
// Initialize the digital pin as an output
pinMode(ledPin, OUTPUT);
void loop() {
// Turn the LED on (HIGH is the voltage level)
digitalWrite(ledPin, HIGH);
// Wait for a second
delay(1000);
// Turn the LED off by making the voltage LOW
digitalWrite(ledPin, LOW);
// Wait for a second
delay(1000);
3. ULTRA SONIC WITH ARDUINO
// Define the pins for the HC-SR04 sensor
const int trigPin = 9;
const int echoPin = 10;
void setup() {
// Start the serial communication
Serial.begin(9600);
// Set the trigPin as an OUTPUT and echoPin as an INPUT
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
void loop() {
// Clear the trigPin by setting it LOW
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
// Set the trigPin HIGH for 10 microseconds to generate a pulse
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Read the echoPin, which is HIGH for the duration of the pulse
long duration = pulseIn(echoPin, HIGH);
// Calculate the distance in centimeters
float distance = (duration / 2.0) * 0.0344;
// Print the distance to the Serial Monitor
Serial.print("Distance: ");
Serial.print(distance);
Serial.println(" cm");
// Wait for a short period before taking the next measurement
delay(500);
5.HUMAN FOLLOWING ROBOT
Adafruit Motor Shield library installed
a. dc motor test code
#include <Wire.h>
#include <Adafruit_MS_PWMServoDriver.h>
#include <utility/imumaths.h>
// Create an instance of the motor shield
Adafruit_MS_PWMServoDriver pwm = Adafruit_MS_PWMServoDriver();
// Define motor speed and delay
const int motorSpeed = 255; // Max speed is 255
const int delayTime = 2000; // Delay time in milliseconds
void setup() {
// Initialize serial communication
Serial.begin(9600);
// Initialize the motor shield
pwm.begin();
pwm.setPWMFreq(1600); // Set the PWM frequency to 1600 Hz
// Allow time for the motor shield to initialize
delay(500);
void loop() {
// Run all motors forward
Serial.println("Running motors forward");
runMotorForward();
delay(delayTime);
// Run all motors backward
Serial.println("Running motors backward");
runMotorBackward();
delay(delayTime);
// Stop all motors
Serial.println("Stopping all motors");
stopAllMotors();
delay(delayTime);
// Function to run all motors forward
void runMotorForward() {
pwm.setPWM(0, 0, motorSpeed); // Motor 1 forward
pwm.setPWM(1, 0, motorSpeed); // Motor 2 forward
pwm.setPWM(2, 0, motorSpeed); // Motor 3 forward
pwm.setPWM(3, 0, motorSpeed); // Motor 4 forward
// Function to run all motors backward
void runMotorBackward() {
pwm.setPWM(0, motorSpeed, 0); // Motor 1 backward
pwm.setPWM(1, motorSpeed, 0); // Motor 2 backward
pwm.setPWM(2, motorSpeed, 0); // Motor 3 backward
pwm.setPWM(3, motorSpeed, 0); // Motor 4 backward
// Function to stop all motors
void stopAllMotors() {
pwm.setPWM(0, 0, 0); // Stop Motor 1
pwm.setPWM(1, 0, 0); // Stop Motor 2
pwm.setPWM(2, 0, 0); // Stop Motor 3
pwm.setPWM(3, 0, 0); // Stop Motor 4
b.main code
#include <Wire.h>
#include <Adafruit_MS_PWMServoDriver.h>
// Create an instance of the motor shield
Adafruit_MS_PWMServoDriver pwm = Adafruit_MS_PWMServoDriver();
// Define motor control pins (adjust based on your motor shield setup)
const int motor1Forward = 0; // Motor 1 (left front)
const int motor1Backward = 1; // Motor 2 (left rear)
const int motor2Forward = 2; // Motor 3 (right front)
const int motor2Backward = 3; // Motor 4 (right rear)
// Define sensor pins
const int leftSensorPin = 2;
const int rightSensorPin = 3;
// Define speed and delay
const int motorSpeed = 4095; // Max speed (0-4095 for the Adafruit Motor Shield)
const int delayTime = 200; // Time delay in milliseconds
void setup() {
Serial.begin(9600);
pwm.begin();
pwm.setPWMFreq(1600); // Set the PWM frequency
pinMode(leftSensorPin, INPUT);
pinMode(rightSensorPin, INPUT);
// Allow time for the motor shield to initialize
delay(500);
void loop() {
// Read sensor values
int leftSensorValue = digitalRead(leftSensorPin);
int rightSensorValue = digitalRead(rightSensorPin);
if (leftSensorValue == LOW && rightSensorValue == LOW) {
// Move forward if no obstacles are detected
moveForward();
} else if (leftSensorValue == HIGH && rightSensorValue == LOW) {
// Turn right if the left sensor detects an obstacle
turnRight();
} else if (leftSensorValue == LOW && rightSensorValue == HIGH) {
// Turn left if the right sensor detects an obstacle
turnLeft();
} else {
// Stop if both sensors detect obstacles
stopAllMotors();
delay(delayTime);
void moveForward() {
pwm.setPWM(motor1Forward, 0, motorSpeed);
pwm.setPWM(motor1Backward, motorSpeed, 0);
pwm.setPWM(motor2Forward, 0, motorSpeed);
pwm.setPWM(motor2Backward, motorSpeed, 0);
void turnRight() {
pwm.setPWM(motor1Forward, motorSpeed, 0);
pwm.setPWM(motor1Backward, 0, motorSpeed);
pwm.setPWM(motor2Forward, 0, motorSpeed);
pwm.setPWM(motor2Backward, motorSpeed, 0);
void turnLeft() {
pwm.setPWM(motor1Forward, 0, motorSpeed);
pwm.setPWM(motor1Backward, motorSpeed, 0);
pwm.setPWM(motor2Forward, motorSpeed, 0);
pwm.setPWM(motor2Backward, 0, motorSpeed);
void stopAllMotors() {
pwm.setPWM(motor1Forward, 0, 0);
pwm.setPWM(motor1Backward, 0, 0);
pwm.setPWM(motor2Forward, 0, 0);
pwm.setPWM(motor2Backward, 0, 0);
6.AI GLOVE
a.FLEXSENSOR
const int flexSensorPin = A0; // Analog pin connected to the flex sensor
int flexValue = 0; // Variable to store the flex sensor value
void setup() {
Serial.begin(9600); // Initialize serial communication at 9600 bps
void loop() {
flexValue = analogRead(flexSensorPin); // Read the value from the flex sensor
Serial.print("Flex Sensor Value: ");
Serial.println(flexValue); // Print the value to the Serial Monitor
delay(500); // Delay for 500 milliseconds before reading again
b.DFMINI WITH ARDUINO
***** AURDINO NANO *****
`#include <DFRobotDFPlayerMini.h>
#include "mp3tf16p.h"
MP3Player mp3(10, 11); //MP3Player mp3(TX, RX);
void setup() {
Serial.begin(9600);
mp3.initialize();
mp3.playTrackNumber(1, 30);
void loop() {
// put your main code here, to run repeatedly:
10 - TX
11 - RX
****** AURDINO UNO *****
#include <DFRobotDFPlayerMini.h>
#include "mp3tf16p.h"
MP3Player mp3(11,10); // MP3Player mp3(TX,RX);
void setup() {
Serial.begin(9600);
mp3.initialize();
mp3.playTrackNumber(3, 30);
void loop() {
// put your main code here, to run repeat
cvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
vvvvvvvvvvvedly:
10 - RX
11 - TX
c.AI VOICE GENERATE
d.ai glove
#include <DFRobotDFPlayerMini.h>
#include "mp3tf16p.h"
MP3Player mp3(10, 11); // digital signal pin for dfmini player
const int flexPin1 = A1; // Pin for the first flex sensor
const int flexPin2 = A2; // Pin for the second flex sensor
const int flexPin3 = A3; // Pin for the third flex sensor
const int flexPin4 = A4; // Pin for the fourth flex sensor
void setup() {
Serial.begin(9600);
mp3.initialize();
void loop() {
int flexValue1 = analogRead(flexPin1); // Read value from the first flex sensor
int flexValue2 = analogRead(flexPin2); // Read value from the second flex sensor
int flexValue3 = analogRead(flexPin3); // Read value from the third flex sensor
int flexValue4 = analogRead(flexPin4); // Read value from the fourth flex sensor
Serial.print("Flex Sensor 1 Value: ");
Serial.println(flexValue1);
Serial.print("Flex Sensor 2 Value: ");
Serial.println(flexValue2);
Serial.print("Flex Sensor 3 Value: ");
Serial.println(flexValue3);
Serial.print("Flex Sensor 4 Value: ");
Serial.println(flexValue4);
// Check if flex sensor 1 value is greater than 800
if (flexValue1 > 800) {
mp3.playTrackNumber(1, 30); // Play track 1 for flex sensor 1
delay(500); // Add a delay to avoid continuous triggering
// Check if flex sensor 2 value is greater than 800
if (flexValue2 > 800) {
mp3.playTrackNumber(2, 30); // Play track 2 for flex sensor 2
delay(500); // Add a delay to avoid continuous triggering
// Check if flex sensor 3 value is greater than 800
if (flexValue3 > 800) {
mp3.playTrackNumber(3, 30); // Play track 3 for flex sensor 3
delay(500); // Add a delay to avoid continuous triggering
// Check if flex sensor 4 value is greater than 800
if (flexValue4 > 800) {
mp3.playTrackNumber(4, 30); // Play track 4 for flex sensor 4
delay(500); // Add a delay to avoid continuous triggering
delay(20); // Allow time for the serial monitor to update
7.AI BLIND STICK
a. Water measure and detection sensor
const int waterSensorPin = 2; // Digital pin connected to the water sensor
const int indicatorPin = 9; // Digital pin connected to the LED or buzzer
void setup() {
pinMode(waterSensorPin, INPUT); // Set the water sensor pin as input
pinMode(indicatorPin, OUTPUT); // Set the indicator pin as output
Serial.begin(9600); // Initialize serial communication
void loop() {
int sensorValue = digitalRead(waterSensorPin); // Read the value from the water sensor
if (sensorValue == HIGH) {
// Water detected
digitalWrite(indicatorPin, HIGH); // Turn on LED or buzzer
Serial.println("Water detected!");
} else {
// No water detected
digitalWrite(indicatorPin, LOW); // Turn off LED or buzzer
Serial.println("No water detected.");
delay(500); // Delay for stability
b.ULTRA SONIC
const int trigPin = 9; // Digital pin connected to the TRIG pin of the sensor
const int echoPin = 10; // Digital pin connected to the ECHO pin of the sensor
long duration; // Variable to store the duration of the pulse
int distance; // Variable to store the calculated distance
void setup() {
pinMode(trigPin, OUTPUT); // Set TRIG pin as output
pinMode(echoPin, INPUT); // Set ECHO pin as input
Serial.begin(9600); // Initialize serial communication
void loop() {
// Clear the TRIG pin
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
// Send a 10 microsecond pulse to the TRIG pin
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Read the duration of the pulse from the ECHO pin
duration = pulseIn(echoPin, HIGH);
// Calculate the distance in centimeters
distance = (duration / 2) / 29.1; // Speed of sound is ~343 meters per
second, 29.1 converts to cm
// Print the distance to the Serial Monitor
Serial.print("Distance: ");
Serial.print(distance);
Serial.println(" cm");
delay(500); // Wait for 500 milliseconds before taking the next reading
c.main code
#include <DFRobotDFPlayerMini.h>
#include "mp3tf16p.h"
// Water Level Sensor
const int waterLevelPin = A0; // Define the analog pin
int waterLevelValue = 0; // Variable to store the sensor value
// Define the maximum possible water level in your container
const float maxWaterLevel = 20.0; // Example: 20 cm
const int maxAnalogValue = 1023; // Maximum analog value (10-bit ADC)
// Ultrasonic Sensor (HC-SR04)
const int trigPin = 9; // Trig pin of HC-SR04 connected to Arduino D2
const int echoPin = 8; // Echo pin of HC-SR04 connected to Arduino D3
long duration;
int distance;
// MP3 Player
MP3Player mp3(10, 11); //MP3Player mp3(TX, RX);
void setup() {
Serial.begin(9600);
// Initialize water level sensor
pinMode(waterLevelPin, INPUT);
// Initialize ultrasonic sensor
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
// Initialize MP3 player
mp3.initialize();
void loop() {
// Read water level sensor
waterLevelValue = analogRead(waterLevelPin);
float waterLevelCm = (waterLevelValue * maxWaterLevel) maxAnalogValue;
// Read ultrasonic sensor
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = duration * 0.034 / 2;
// Print sensor values for debugging
Serial.print("Water Level: ");
Serial.print(waterLevelCm);
Serial.println(" cm");
Serial.print("Distance: ");
Serial.print(distance);
Serial.println(" cm");
// Check conditions and trigger MP3 playback
if (distance < 30) {
// Play track if distance is less than 30 cm
mp3.playTrackNumber(1, 30); // Adjust track number and volume as needed
delay(1000); // Delay to avoid rapid triggering
if (waterLevelCm >10) {
// Play second track if water level is exactly 10 cm
mp3.playTrackNumber(2, 30); // Adjust track number and volume as needed
delay(1000); // Delay to avoid rapid triggering
}
// Delay before next iteration
delay(500); // Adjust delay as needed for sensor stability and responsiveness
8. BPM AND TEMPERATURE MEASURE
a. Pluse sensor
#include <PulseSensorPlayground.h> // Include the PulseSensor library
const int pulsePin = A0; // Analog pin connected to the pulse sensor
const int threshold = 550; // Threshold for detecting heartbeat (might need
adjustment)
// Create an instance of the PulseSensorPlayground
PulseSensorPlayground pulseSensor;
void setup() {
Serial.begin(9600); // Initialize serial communication
pulseSensor.analogInput(pulsePin); // Connect pulse sensor to analog pin
pulseSensor.setThreshold(threshold); // Set the heartbeat detection threshold
pulseSensor.begin(); // Start the pulse sensor
void loop() {
int bpm = 0; // Variable to store BPM value
if (pulseSensor.sawStartOfBeat()) {
bpm = pulseSensor.getBeatsPerMinute(); // Get BPM value from the sensor
if (bpm > 0) {
// Map the BPM from the range 80-100 to 0-255
int mappedValue = map(bpm, 80, 100, 0, 255);
// Ensure the mapped value stays within the range 0-255
mappedValue = constrain(mappedValue, 0, 255);
// Print the original BPM and the mapped value to the Serial Monitor
Serial.print("BPM: ");
Serial.print(bpm);
Serial.print(" - Mapped Value: ");
Serial.println(mappedValue);
delay(100); // Delay to reduce the rate of readings
b.Lcd interface
c.LM DIODE (temp) sensor
// Now read the temperature sensor
temp = analogRead(A0); // Read analog input from A0
temp = temp * 0.048820105; // Convert analog reading to voltage (mV)
// Print temperature to the serial monitor
Serial.print("Temperature: ");
Serial.print(temp);
Serial.println(" °C");
d. the main code which is used to detect the person heartrate
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
// Constants for Pulse Sensor
const int pulsePin = A1; // Pulse Sensor analog output connected to analog pin A1
float temp; // Variable to store temperature
// LCD setup
LiquidCrystal_I2C lcd(0x27, 16, 2); // set the LCD address to 0x27 for a 16 chars and
2 line display
void setup() {
// Initialize serial communication at 9600 baud
Serial.begin(9600);
// Initialize LCD
lcd.init();
lcd.backlight();
// Optional: Clear LCD initially
lcd.clear();
void loop() {
// Read the pulse sensor value
int sensorValue = analogRead(pulsePin);
// Map the sensor value to a BPM range (adjust these values based on your
sensor)
int bpmValue = map(sensorValue, 0, 1023, 50, 120); // Adjusted BPM range
based on your requirement
// Print the sensor value and BPM to the Serial Monitor
Serial.print("Pulse Sensor Value: ");
Serial.print(sensorValue);
Serial.print(" -> BPM: ");
Serial.println(bpmValue);
// Now read the temperature sensor
temp = analogRead(A0); // Read analog input from A0
temp = temp * 0.048820105; // Convert analog reading to voltage (mV)
// Print temperature to the serial monitor
Serial.print("Temperature: ");
Serial.print(temp);
Serial.println(" °C");
// Display on LCD
lcd.setCursor(0, 0);
lcd.print("Pulse: ");
lcd.print(bpmValue);
lcd.print(" BPM");
lcd.setCursor(0, 1);
lcd.print("Temp: ");
lcd.print(temp);
lcd.print(" C");
// Delay before updating again
delay(2000); // Adjust delay as needed for your application
// Clear LCD for next update
lcd.clear();
9.AUTO SENSING LIGHT(ultrasonic sensor)
// Define the pins for the ultrasonic sensor and relay
const int trigPin = A1; // Trig pin of the ultrasonic sensor (connected to A1)
const int echoPin = A0; // Echo pin of the ultrasonic sensor (connected to A0)
const int relayPin = 7; // Pin connected to the relay IN pin
// Variables to store the duration of the pulse and calculated distance
long duration;
int distance_cm;
void setup() {
Serial.begin(9600); // Start serial communication at 9600 baud rate
pinMode(trigPin, OUTPUT); // Set trigPin (A1) as output
pinMode(echoPin, INPUT); // Set echoPin (A0) as input
pinMode(relayPin, OUTPUT);// Set relayPin as output
digitalWrite(relayPin, LOW); // Make sure the relay is off at the start
void loop() {
// Clear the trigPin by setting it low for a short moment
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
// Set the trigPin high for 10 microseconds to send the ultrasonic pulse
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Read the echoPin (A0), which returns the duration of the pulse in microseconds
duration = pulseIn(echoPin, HIGH);
// Calculate the distance in centimeters (speed of sound is 343 meters/second)
distance_cm = duration * 0.034 / 2;
// Print the distance to the Serial Monitor
Serial.print("Distance: ");
Serial.print(distance_cm);
Serial.println(" cm");
// Check if the distance is below 30 cm
if (distance_cm > 30) {
// Turn the relay (and bulb) on
digitalWrite(relayPin, HIGH);
} else {
// Turn the relay (and bulb) off
digitalWrite(relayPin, LOW);
// Wait a short delay before taking the next distance measurement
delay(500); // Adjust delay time as needed for your application
10.OBSTACLE AVOIDING CAR
#include <AFMotor.h> // libraray for motor shield
#include <Servo.h> // libraray for servo motor
#include <NewPing.h> // libraray for ultrasonic sensor
const int Trig_Pin =A0;
const int Echo_Pin =A1;
const int Max_Speed =200;
const int Max_Dist = 250;
NewPing ultra_sonic(Trig_Pin, Echo_Pin, Max_Dist);
AF_DCMotor motor1(1,MOTOR12_1KHZ);
AF_DCMotor motor2(2,MOTOR12_1KHZ);
AF_DCMotor motor3(3,MOTOR34_1KHZ);
AF_DCMotor motor4(4,MOTOR34_1KHZ);
Servo myservo;
int distance=250;
int speedSet=0;
boolean goesForward=false;
void setup() {
myservo.attach(10);
myservo.write(90);
delay(2000);
distance=readDistance();
delay(100);
moveForward();
void loop() {
int dist_R = 0;
int dist_L = 0;
delay(100);
if(distance<=25){
moveStop();
delay(100);
moveBackward();
delay(200);
moveStop();
delay(100);
dist_R = right_Distance();
delay(100);
dist_L = left_Distance();
delay(100);
if(dist_R <= dist_L){
turnLeft();
moveStop();
else{
turnRight();
moveStop();
else{
moveForward();
distance = readDistance();
int left_Distance(){
myservo.write(170);
delay(500);
int dist = readDistance();
delay(100);
myservo.write(90);
return dist;
delay(100);
int right_Distance(){
myservo.write(10);
delay(500);
int dist = readDistance();
delay(100);
myservo.write(90);
return dist;
delay(100);
int readDistance(){
int cm = ultra_sonic.ping_cm();
if(cm<=0){ // this is done in case when sensor gives -ve
value which is absurd
cm=250;
return cm;
void moveStop() {
motor1.run(RELEASE);
motor2.run(RELEASE);
motor3.run(RELEASE);
motor4.run(RELEASE);
void moveForward() {
if(!goesForward)
goesForward=true;
motor1.run(FORWARD);
motor2.run(FORWARD);
motor3.run(FORWARD);
motor4.run(FORWARD);
for (speedSet = 0; speedSet < Max_Speed; speedSet +=2) // slowly bring the
speed up to avoid loading down the batteries too quickly
motor1.setSpeed(speedSet);
motor2.setSpeed(speedSet);
motor3.setSpeed(speedSet);
motor4.setSpeed(speedSet);
delay(5);
void moveBackward() {
goesForward=false;
motor1.run(BACKWARD);
motor2.run(BACKWARD);
motor3.run(BACKWARD);
motor4.run(BACKWARD);
for (speedSet = 0; speedSet < Max_Speed; speedSet +=2) // slowly bring the
speed up to avoid loading down the batteries too quickly
motor1.setSpeed(speedSet);
motor2.setSpeed(speedSet);
motor3.setSpeed(speedSet);
motor4.setSpeed(speedSet);
delay(5);
}
void turnRight() {
motor1.run(FORWARD);
motor2.run(FORWARD);
motor3.run(BACKWARD);
motor4.run(BACKWARD);
delay(500);
motor1.run(FORWARD);
motor2.run(FORWARD);
motor3.run(FORWARD);
motor4.run(FORWARD);
void turnLeft() {
motor1.run(BACKWARD);
motor2.run(BACKWARD);
motor3.run(FORWARD);
motor4.run(FORWARD);
delay(500);
motor1.run(FORWARD);
motor2.run(FORWARD);
motor3.run(FORWARD);
motor4.run(FORWARD);
11.ESP NODE MODULE
#include <ESP8266WiFi.h> // For ESP8266
// #include <WiFi.h> // Uncomment this for ESP32
const char* ssid = "your_SSID";
const char* password = "your_PASSWORD";
void setup() {
Serial.begin(115200);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
Serial.println("");
Serial.println("Connected to WiFi");
Serial.println("IP Address: ");
Serial.println(WiFi.localIP());
void loop() {
// Your code here
the mega basic circuit code
#include <DFRobotDFPlayerMini.h>
#include "mp3tf16p.h"
MP3Player mp3(10, 11); // digital signal pin for dfmini player
const int flexPin1 = A0; // Pin for the first flex sensor
const int flexPin2 = A1; // Pin for the second flex sensor
const int flexPin3 = A2; // Pin for the third flex sensor
const int flexPin4 = A3; // Pin for the fourth flex sensor
const int ledPin = 13; // Pin for the LED
void setup() {
Serial.begin(9600);
mp3.initialize();
pinMode(ledPin, OUTPUT); // Set LED pin as output
void loop() {
int flexValue1 = analogRead(flexPin1); // Read value from the first flex sensor
int flexValue2 = analogRead(flexPin2); // Read value from the second flex sensor
int flexValue3 = analogRead(flexPin3); // Read value from the third flex sensor
int flexValue4 = analogRead(flexPin4); // Read value from the fourth flex sensor
Serial.print("Flex Sensor 1 Value: ");
Serial.println(flexValue1);
Serial.print("Flex Sensor 2 Value: ");
Serial.println(flexValue2);
Serial.print("Flex Sensor 3 Value: ");
Serial.println(flexValue3);
Serial.print("Flex Sensor 4 Value: ");
Serial.println(flexValue4);
// Turn on the LED if any two flex sensors are bent
if ((flexValue1 > 800 && flexValue2 > 800) ||
(flexValue1 > 800 && flexValue3 > 800) ||
(flexValue1 > 800 && flexValue4 > 800) ||
(flexValue2 > 800 && flexValue3 > 800) ||
(flexValue2 > 800 && flexValue4 > 800) ||
(flexValue3 > 800 && flexValue4 > 800)) {
digitalWrite(ledPin, HIGH); // Turn on the LED
} else {
digitalWrite(ledPin, LOW); // Turn off the LED
// Sound playback logic (only if a single sensor is bent)
if (flexValue1 > 800 &&
!(flexValue2 > 800 || flexValue3 > 800 || flexValue4 > 800)) {
mp3.playTrackNumber(1, 30); // Play track 1 for flex sensor 1
delay(500); // Add a delay to avoid continuous triggering
if (flexValue2 > 800 &&
!(flexValue1 > 800 || flexValue3 > 800 || flexValue4 > 800)) {
mp3.playTrackNumber(2, 30); // Play track 2 for flex sensor 2
delay(500); // Add a delay to avoid continuous triggering
if (flexValue3 > 800 &&
!(flexValue1 > 800 || flexValue2 > 800 || flexValue4 > 800)) {
mp3.playTrackNumber(3, 30); // Play track 3 for flex sensor 3
delay(500); // Add a delay to avoid continuous triggering
}
if (flexValue4 > 800 &&
!(flexValue1 > 800 || flexValue2 > 800 || flexValue3 > 800)) {
mp3.playTrackNumber(4, 30); // Play track 4 for flex sensor 4
delay(500); // Add a delay to avoid continuous triggering
delay(20); // Allow time for the serial monitor to update
THE MODE SHIFINTG'
#include <DFRobotDFPlayerMini.h>
#include "mp3tf16p.h"
MP3Player mp3(10, 11); // digital signal pin for dfmini player
const int flexPin1 = A0; // Pin for the first flex sensor
const int flexPin2 = A1; // Pin for the second flex sensor
const int flexPin3 = A2; // Pin for the third flex sensor
const int flexPin4 = A3; // Pin for the fourth flex sensor
const int ledPin = 13; // Pin for the LED
bool mode = true; // true for Mode 1 (LED), false for Mode 2 (Sound)
unsigned long lastModeChangeTime = 0; // Store the time of last mode change
const unsigned long debounceDelay = 1000; // 1-second debounce delay
void setup() {
Serial.begin(9600);
mp3.initialize();
pinMode(ledPin, OUTPUT); // Set LED pin as output
void loop() {
int flexValue1 = analogRead(flexPin1);
int flexValue2 = analogRead(flexPin2);
int flexValue3 = analogRead(flexPin3);
int flexValue4 = analogRead(flexPin4);
Serial.print("Flex Sensor 1 Value: ");
Serial.println(flexValue1);
Serial.print("Flex Sensor 2 Value: ");
Serial.println(flexValue2);
Serial.print("Flex Sensor 3 Value: ");
Serial.println(flexValue3);
Serial.print("Flex Sensor 4 Value: ");
Serial.println(flexValue4);
// Check if all four sensors are bent to toggle mode
if (flexValue1 > 800 && flexValue2 > 800 && flexValue3 > 800 && flexValue4 > 800) {
unsigned long currentTime = millis();
if (currentTime - lastModeChangeTime > debounceDelay) {
mode = !mode; // Toggle mode
lastModeChangeTime = currentTime; // Update last mode change time
Serial.print("Mode switched to: ");
Serial.println(mode ? "LED Mode" : "Sound Mode");
// Mode 1: LED turns on if any two sensors are bent
if (mode) {
if ((flexValue1 > 800 && flexValue2 > 800) ||
(flexValue1 > 800 && flexValue3 > 800) ||
(flexValue1 > 800 && flexValue4 > 800) ||
(flexValue2 > 800 && flexValue3 > 800) ||
(flexValue2 > 800 && flexValue4 > 800) ||
(flexValue3 > 800 && flexValue4 > 800)) {
digitalWrite(ledPin, HIGH); // Turn on LED
} else {
digitalWrite(ledPin, LOW); // Turn off LED
// Mode 2: Sound plays when a single sensor is bent
else {
if (flexValue1 > 800 && !(flexValue2 > 800 || flexValue3 > 800 || flexValue4 > 800)) {
mp3.playTrackNumber(1, 30);
delay(500);
}
if (flexValue2 > 800 && !(flexValue1 > 800 || flexValue3 > 800 || flexValue4 > 800)) {
mp3.playTrackNumber(2, 30);
delay(500);
if (flexValue3 > 800 && !(flexValue1 > 800 || flexValue2 > 800 || flexValue4 > 800)) {
mp3.playTrackNumber(3, 30);
delay(500);
if (flexValue4 > 800 && !(flexValue1 > 800 || flexValue2 > 800 || flexValue3 > 800)) {
mp3.playTrackNumber(4, 30);
delay(500);
delay(20); // Allow time for the serial monitor to update
prefect mode change
#include <DFRobotDFPlayerMini.h>
#include "mp3tf16p.h"
MP3Player mp3(10, 11); // Digital signal pin for DFPlayer Mini
int relayPinBulb = 7; // Relay control pin for the bulb
int relayPinMotor = 8; // Relay control pin for the DC motor
int buzzerPin = 9; // Buzzer control pin
int flexSensorPinBulb = A0; // Flex sensor for the bulb
int flexSensorPinMotor = A1; // Flex sensor for the motor
int flexSensorPinBuzzer = A2;// Flex sensor for the buzzer
int flexSensorPinLed = A3; // Flex sensor for the LED
int flexThreshold = 800; // Threshold value for all flex sensors
bool bulbState = false; // Variable to store the bulb state (ON/OFF)
bool motorState = false; // Variable to store the motor state (ON/OFF)
bool buzzerState = false; // Variable to store the buzzer state (ON/OFF)
bool ledState = false; // Variable to store the LED state (ON/OFF)
bool previousFlexStateBulb = false; // Variable for flex sensor for bulb
bool previousFlexStateMotor = false; // Variable for flex sensor for motor
bool previousFlexStateBuzzer = false; // Variable for flex sensor for buzzer
bool previousFlexStateLed = false; // Variable for flex sensor for LED
bool mode = true; // true for Home Automation, false for DFPlayer Mini mode
unsigned long lastModeChangeTime = 0; // Store the time of last mode change
const unsigned long debounceDelay = 1000; // 1-second debounce delay
void setup() {
Serial.begin(9600);
mp3.initialize();
pinMode(relayPinBulb, OUTPUT); // Set relay pin for bulb as output
pinMode(relayPinMotor, OUTPUT); // Set relay pin for motor as output
pinMode(buzzerPin, OUTPUT); // Set buzzer pin as output
pinMode(13, OUTPUT); // Set LED pin as output
digitalWrite(relayPinBulb, LOW); // Ensure the bulb is off initially
digitalWrite(relayPinMotor, LOW); // Ensure the motor is off initially
digitalWrite(buzzerPin, LOW); // Ensure the buzzer is off initially
digitalWrite(13, LOW); // Ensure the LED is off initially
void loop() {
int flexValueBulb = analogRead(flexSensorPinBulb); // Read flex sensor for bulb
int flexValueMotor = analogRead(flexSensorPinMotor); // Read flex sensor for motor
int flexValueBuzzer = analogRead(flexSensorPinBuzzer); // Read flex sensor for buzzer
int flexValueLed = analogRead(flexSensorPinLed); // Read flex sensor for LED
Serial.print("Flex Sensor Bulb Value: ");
Serial.println(flexValueBulb);
Serial.print("Flex Sensor Motor Value: ");
Serial.println(flexValueMotor);
Serial.print("Flex Sensor Buzzer Value: ");
Serial.println(flexValueBuzzer);
Serial.print("Flex Sensor LED Value: ");
Serial.println(flexValueLed);
// Check if all four sensors are bent to toggle mode
if (flexValueBulb > flexThreshold && flexValueMotor > flexThreshold && flexValueBuzzer >
flexThreshold && flexValueLed > flexThreshold) {
unsigned long currentTime = millis();
if (currentTime - lastModeChangeTime > debounceDelay) {
mode = !mode; // Toggle mode
lastModeChangeTime = currentTime; // Update last mode change time
Serial.print("Mode switched to: ");
Serial.println(mode ? "Home Automation Mode" : "DFPlayer Mini Mode");
// Mode 1: Home Automation
if (mode) {
// Bulb control with flex sensor
bool currentFlexStateBulb = flexValueBulb > flexThreshold;
if (currentFlexStateBulb && !previousFlexStateBulb) {
if (!bulbState) {
digitalWrite(relayPinBulb, HIGH); // Turn on the bulb
bulbState = true; // Update bulb state to ON
Serial.println("Bulb ON");
} else {
digitalWrite(relayPinBulb, LOW); // Turn off the bulb
bulbState = false; // Update bulb state to OFF
Serial.println("Bulb OFF");
}
// Motor control with flex sensor
bool currentFlexStateMotor = flexValueMotor > flexThreshold;
if (currentFlexStateMotor && !previousFlexStateMotor) {
if (!motorState) {
digitalWrite(relayPinMotor, HIGH); // Turn on the motor
motorState = true; // Update motor state to ON
Serial.println("Motor ON");
} else {
digitalWrite(relayPinMotor, LOW); // Turn off the motor
motorState = false; // Update motor state to OFF
Serial.println("Motor OFF");
// Buzzer control with flex sensor
bool currentFlexStateBuzzer = flexValueBuzzer > flexThreshold;
if (currentFlexStateBuzzer && !previousFlexStateBuzzer) {
if (!buzzerState) {
digitalWrite(buzzerPin, HIGH); // Turn on the buzzer
buzzerState = true; // Update buzzer state to ON
Serial.println("Buzzer ON");
} else {
digitalWrite(buzzerPin, LOW); // Turn off the buzzer
buzzerState = false; // Update buzzer state to OFF
Serial.println("Buzzer OFF");
}
// LED control with flex sensor
bool currentFlexStateLed = flexValueLed > flexThreshold;
if (currentFlexStateLed && !previousFlexStateLed) {
if (!ledState) {
digitalWrite(13, HIGH); // Turn on the LED
ledState = true; // Update LED state to ON
Serial.println("LED ON");
} else {
digitalWrite(13, LOW); // Turn off the LED
ledState = false; // Update LED state to OFF
Serial.println("LED OFF");
// Mode 2: DFPlayer Mini mode (playing message)
else {
if (flexValueBulb > flexThreshold) {
mp3.playTrackNumber(1, 30); // Play message 1
delay(500);
if (flexValueMotor > flexThreshold) {
mp3.playTrackNumber(2, 30); // Play message 2
delay(500);
if (flexValueBuzzer > flexThreshold) {
mp3.playTrackNumber(3, 30); // Play message 3
delay(500);
if (flexValueLed > flexThreshold) {
mp3.playTrackNumber(4, 30); // Play message 4
delay(500);
// Update previous flex states for the next loop
previousFlexStateBulb = flexValueBulb > flexThreshold;
previousFlexStateMotor = flexValueMotor > flexThreshold;
previousFlexStateBuzzer = flexValueBuzzer > flexThreshold;
previousFlexStateLed = flexValueLed > flexThreshold;
delay(100); // Small delay for stability
the transmiter code
#include <DFRobotDFPlayerMini.h>
#include "mp3tf16p.h"
#include <RH_ASK.h>
#include <SPI.h>
MP3Player mp3(10, 11); // DFPlayer Mini: RX=10, TX=11
RH_ASK driver; // ASK transmitter: Data pin 12
// Flex sensors
const int flexSensorPinBulb = A0;
const int flexSensorPinMotor = A1;
const int flexSensorPinBuzzer = A2;
const int flexSensorPinLed = A3;
const int flexThreshold = 800; // Flex bending threshold
void setup() {
Serial.begin(9600);
if (!driver.init()) {
Serial.println("ASK Transmitter Init Failed");
while (1);
Serial.println("Transmitter Ready");
mp3.begin();
mp3.volume(30); // Set volume level
void loop() {
int flexValueBulb = analogRead(flexSensorPinBulb);
int flexValueMotor = analogRead(flexSensorPinMotor);
int flexValueBuzzer = analogRead(flexSensorPinBuzzer);
int flexValueLed = analogRead(flexSensorPinLed);
// Play sounds based on individual flex sensor bending
if (flexValueBulb > flexThreshold) {
mp3.playTrackNumber(1, 30);
delay(500);
if (flexValueMotor > flexThreshold) {
mp3.playTrackNumber(2, 30);
delay(500);
if (flexValueBuzzer > flexThreshold) {
mp3.playTrackNumber(3, 30);
delay(500);
if (flexValueLed > flexThreshold) {
mp3.playTrackNumber(4, 30);
delay(500);
// Check for mode shift: all sensors bent simultaneously
if (flexValueBulb > flexThreshold && flexValueMotor > flexThreshold &&
flexValueBuzzer > flexThreshold && flexValueLed > flexThreshold) {
const char *msg = "MODE_SHIFT"; // Message to switch to Home Automation mode
driver.send((uint8_t *)msg, strlen(msg));
driver.waitPacketSent();
Serial.println("Mode Shift Signal Sent");
delay(1000); // Debounce delay
the reciver code
#include <RH_ASK.h>
#include <SPI.h>
RH_ASK driver; // ASK Receiver: Data pin 11
// Device control pins
const int relayPinBulb = 7;
const int relayPinMotor = 8;
const int buzzerPin = 9;
const int ledPin = 13;
bool bulbState = false, motorState = false, buzzerState = false, ledState = false;
void setup() {
Serial.begin(9600);
if (!driver.init()) {
Serial.println("ASK Receiver Init Failed");
while (1);
Serial.println("Receiver Ready");
// Set pins as OUTPUT
pinMode(relayPinBulb, OUTPUT);
pinMode(relayPinMotor, OUTPUT);
pinMode(buzzerPin, OUTPUT);
pinMode(ledPin, OUTPUT);
void loop() {
uint8_t buf[RH_ASK_MAX_MESSAGE_LEN];
uint8_t buflen = sizeof(buf);
// Receive message
if (driver.recv(buf, &buflen)) {
buf[buflen] = '\0';
Serial.print("Message Received: ");
Serial.println((char *)buf);
// Check for Mode Shift Command
if (strcmp((char *)buf, "MODE_SHIFT") == 0) {
Serial.println("Home Automation Mode Activated");
homeAutomation();
void homeAutomation() {
while (true) {
int flexValueBulb = analogRead(A0);
int flexValueMotor = analogRead(A1);
int flexValueBuzzer = analogRead(A2);
int flexValueLed = analogRead(A3);
if (flexValueBulb > 800 && !bulbState) {
digitalWrite(relayPinBulb, HIGH);
bulbState = true;
Serial.println("Bulb ON");
delay(500);
if (flexValueMotor > 800 && !motorState) {
digitalWrite(relayPinMotor, HIGH);
motorState = true;
Serial.println("Motor ON");
delay(500);
}
if (flexValueBuzzer > 800 && !buzzerState) {
digitalWrite(buzzerPin, HIGH);
buzzerState = true;
Serial.println("Buzzer ON");
delay(500);
if (flexValueLed > 800 && !ledState) {
digitalWrite(ledPin, HIGH);
ledState = true;
Serial.println("LED ON");
delay(500);