/*void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
Serial.println("Hello, ESP32!");
}
void loop() {
// put your main code here, to run repeatedly:
delay(10); // this speeds up the simulation
}*/
/*
#include <Arduino.h>
#include <ESP32Servo.h>
#include <Keypad.h>
// Q1-P1
/*
//Define the number of LEDs
const int numLEDs = 3;
// Define the LED pins (adjusted for ESP32)
const int ledPins[numLEDs] = {4, 2, 15};
// Function to initialize LEDs
void setup() {
for (int i = 0; i < numLEDs; i++) {
pinMode(ledPins[i], OUTPUT);
digitalWrite(ledPins[i], LOW); // Ensure all LEDs are initially off
}
}
// Function to turn on LEDs in sequence
void lightUpLEDsSequentially() {
for (int i = 0; i < numLEDs; i++) {
digitalWrite(ledPins[i], HIGH); // Turn LED on
delay(500); // Delay to keep the LED on for a while
}
}
// Function to turn off LEDs in sequence
void turnOffLEDsSequentially() {
for (int i = numLEDs - 1; i >= 0; i--) {
digitalWrite(ledPins[i], LOW); // Turn LED off
delay(500); // Delay to keep the LED off for a while
}
}
void loop() {
lightUpLEDsSequentially(); // Light up LEDs in sequence
delay(500); // Wait after all LEDs are lit
turnOffLEDsSequentially(); // Turn off LEDs in sequence
delay(500); // Wait after all LEDs are turned off
}*/
/*
//push button
#include <Arduino.h>
#define LED_PIN 18 // Define the LED pin
#define BUTTON_PIN 35 // Define the button pin
void setup() {
Serial.begin(9600); // Start serial communication
pinMode(LED_PIN, OUTPUT); // Set the LED pin as an output
pinMode(BUTTON_PIN, INPUT_PULLUP);// Set the button pin as an input with internal pull-up resistor
}
void loop() {
int buttonValue = digitalRead(BUTTON_PIN); // Read the state of the button
if (buttonValue == LOW) { // Button is pressed (internal pull-up resistor makes pin LOW when pressed)
Serial.println("Button is pressed");
digitalWrite(LED_PIN, HIGH); // Turn on the LED
} else {
Serial.println("Button is not pressed");
digitalWrite(LED_PIN, LOW); // Turn off the LED
}
delay(500); // Delay in milliseconds
}*/
/*
//Q1-P3
int LDRInput=35; //Set Analog Input A0 for LDR.
int LED=18;
int Buzzer=26;
void setup() {
Serial.begin(9600);
pinMode(LDRInput,INPUT);
pinMode(LED,OUTPUT);
pinMode(Buzzer,OUTPUT);
}
void loop() {
int value=analogRead(LDRInput);//Reads the Value of LDR(light).
Serial.println("LDR value is :");//Prints the value of LDR to Serial Monitor.
Serial.println(value);
if(value<100)
{
tone(Buzzer,100);
digitalWrite(LED,HIGH);//The LED turns ON in Dark.
delay(100);
noTone(Buzzer);
delay(100);
}
else
{
digitalWrite(LED,LOW);//The LED turns OFF in Light.
delay(100);
noTone(Buzzer);
delay(100);
}
}
*/
/*
#include <Arduino.h>
#include <ESP32Servo.h>
#include <Keypad.h>
// Define the pin connected to the servo motor signal line
#define SERVO_PIN 4
// Define the keypad size
const byte ROWS = 4;
const byte COLS = 4;
// Define the key map for the keypad
char hexaKeys[ROWS][COLS] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
// Define the row and column pins used to connect to the keypad
byte rowPins[ROWS] = {13, 12, 14, 27};
byte colPins[COLS] = {26, 25, 33, 32};
// Create the keypad object
Keypad customKeypad = Keypad(makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);
// Servo object
Servo myservo;
// Define the predefined angles
const int servoAngles[] = {30, 45, 60, 90, 120, 180};
// Variable to store the current angle of the servo
int currentAngle = 0;
// Flag to indicate custom angle mode
bool customAngleMode = false;
void setup() {
Serial.begin(115200);
myservo.attach(SERVO_PIN);
myservo.write(currentAngle); // Initialize the servo at 0 degrees
Serial.println("System Ready");
}
void loop() {
char key = customKeypad.getKey();
if (key) {
Serial.print("Key pressed: ");
Serial.println(key);
// Act based on the key pressed
switch (key) {
case '1':
currentAngle = servoAngles[0]; // Set to 30°
break;
case '2':
currentAngle = servoAngles[1]; // Set to 45°
break;
case '3':
currentAngle = servoAngles[2]; // Set to 60°
break;
case '4':
currentAngle = servoAngles[3]; // Set to 90°
break;
case '5':
currentAngle = servoAngles[4]; // Set to 120°
break;
case '6':
currentAngle = servoAngles[5]; // Set to 180°
break;
case '#':
// Stop button logic (command the servo to the current angle)
myservo.write(currentAngle);
Serial.println("Stop button pressed - Servo moved to current position");
break;
case '*':
// Reset button logic (return to 0°)
currentAngle = 0;
myservo.write(currentAngle);
Serial.println("Reset button pressed - Servo returning to 0°");
break;
case 'A':
// Enter custom angle mode
customAngleMode = true;
Serial.println("Enter custom angle in Serial Monitor (0-180):");
break;
default:
Serial.println("Invalid key pressed");
break;
}
// Update the servo position based on the key pressed
if (key != 'A') { // Prevent updating the servo position if custom angle mode is active
myservo.write(currentAngle);
}
}
// Handle custom angle input if in custom angle mode
if (customAngleMode) {
if (Serial.available()) {
int customAngle = Serial.parseInt(); // Read custom angle
if (customAngle >= 0 && customAngle <= 180) {
currentAngle = customAngle;
myservo.write(currentAngle); // Move servo to custom angle
Serial.print("Servo moved to custom angle: ");
Serial.println(customAngle);
} else {
Serial.println("Invalid angle. Please enter an angle between 0° and 180°.");
}
customAngleMode = false; // Exit custom angle mode after processing
}
}
}
*/
//problem 3
/*
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <HardwareSerial.h>
#include <FS.h>
#include <SPIFFS.h>
#include <WiFi.h>
#include <SPI.h>
// Initialize the LCD (I2C address, LCD columns, LCD rows)
LiquidCrystal_I2C lcd(0x27, 16, 2); // Replace 0x27 with your LCD's I2C address if different
const int irSensorPin = 4; // ADC pin connected to IR sensor
const int sensorThreshold = 10; // Threshold for detecting poor connection
// Function prototype for calculateDistance
float calculateDistance(int sensorValue);
void setup() {
// Initialize the LCD
lcd.init(); // Use init() if begin() does not work
lcd.backlight();
// Set up the IR sensor pin
pinMode(irSensorPin, INPUT);
// Start Serial Monitor for debugging
Serial.begin(115200);
// Initial message on LCD
lcd.setCursor(0, 0);
lcd.print("IR Sensor Test");
delay(2000);
lcd.clear();
// Initialize SPIFFS
if(!SPIFFS.begin(true)){
Serial.println("An Error has occurred while mounting SPIFFS");
return;
}
}
void loop() {
// Read the analog value from the IR sensor
int sensorValue = analogRead(irSensorPin);
// Print the sensor value to the Serial Monitor for debugging
Serial.print("Sensor Value: ");
Serial.println(sensorValue);
// Check if the sensor value is below the threshold (indicating a poor connection)
if (sensorValue < sensorThreshold) {
// Display a warning message
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("No input detected");
lcd.setCursor(0, 1);
lcd.print("Check connection");
} else {
// Calculate the distance from the sensor value
float distance = calculateDistance(sensorValue);
// Display the sensor reading and the calculated distance on the LCD
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Reading: ");
lcd.print(sensorValue);
lcd.setCursor(0, 1);
lcd.print("Distance: ");
lcd.print(distance);
lcd.print(" mm");
}
delay(500); // Update the display every 500ms
}
// Function to calculate the distance based on the sensor value
// Adjust the constants based on calibration
float calculateDistance(int sensorValue) {
// Ensure the sensor value is not zero to avoid division by zero
if (sensorValue <= 0) {
return 0.0;
}
// Example formula for distance calculation (adjust as needed)
float distance = 10000.0 / (sensorValue - 800.0);
// Ensure the distance is within the desired range (1mm to 25mm)
if (distance < 1.0) distance = 1.0;
if (distance > 25.0) distance = 25.0;
return distance;
}
*/
//problem 1
/*
#include <ESP32Servo.h>
#define SERVO_PIN 18 // Pin connected to the servo motor signal line
#define IR_SENSOR_PIN 35 // Pin connected to the analog IR sensor (e.g., A0)
#define OPEN_ANGLE 180 // Angle to open the door
#define CLOSE_ANGLE 0 // Angle to close the door
#define DELAY_TIME 2000 // Time (in milliseconds) the door stays open
Servo myServo;
int previousIrValue = 0;
void setup() {
Serial.begin(9600);
myServo.attach(SERVO_PIN);
myServo.write(CLOSE_ANGLE); // Initialize the door to the closed position
previousIrValue = analogRead(IR_SENSOR_PIN); // Initialize with the first IR sensor value
}
void loop() {
int currentIrValue = analogRead(IR_SENSOR_PIN);
Serial.print("IR Sensor Value: ");
Serial.println(currentIrValue);
// Check if the person is detected
if (currentIrValue > 400) { // Adjust the threshold based on your IR sensor
Serial.println("Person detected! Opening door...");
myServo.write(OPEN_ANGLE);
delay(DELAY_TIME); // Keep the door open for a specified time
} else {
Serial.println("No person detected. Closing door...");
myServo.write(CLOSE_ANGLE); // Close the door
}
delay(500); // Small delay for stability
}
*/
//quick bouns one (oled)
/*
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
// OLED display width and height, adjust if necessary
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
// Declaration for SSD1306 display connected using I2C (SDA, SCL)
#define OLED_RESET -1
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
// IR sensor pin
#define IR_SENSOR_PIN 35
void setup() {
// Initialize serial communication
Serial.begin(9600);
// Initialize the OLED display with the I2C address
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // 0x3C is the I2C address for the display
Serial.println(F("SSD1306 allocation failed"));
for (;;); // Don't proceed, loop forever
}
// Clear the buffer
display.clearDisplay();
// Display initialization message
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
display.setCursor(0, 0);
display.println(F("IR Sensor Readings:"));
display.display();
delay(2000); // Pause for 2 seconds
}
void loop() {
// Read the analog value from the IR sensor
int sensorValue = analogRead(IR_SENSOR_PIN);
// Print the sensor value to the Serial Monitor
Serial.print("IR Sensor Value: ");
Serial.println(sensorValue);
// Clear the display buffer
display.clearDisplay();
// Set text properties
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
display.setCursor(0, 0);
// Display the sensor value
display.println(F("IR Sensor Readings:"));
display.setTextSize(2);
display.setCursor(0, 20);
display.print("Value: ");
display.println(sensorValue);
// Update the display with the new data
display.display();
// Wait for a short period before taking another reading
delay(500);
}
*/
// quick bouns ir & bazzer
/*
#include <Arduino.h>
#include <Wire.h>
// Define pin connections
const int irSensorPin = 35; // Analog pin for IR sensor
const int ledPin = 32; // Digital pin for LED
const int buzzerPin = 33; // Digital pin for Buzzer
// Threshold value for proximity detection
const int threshold = 500;
void setup() {
// Initialize serial communication
Serial.begin(115200);
// Initialize pin modes
pinMode(irSensorPin, INPUT);
pinMode(ledPin, OUTPUT);
pinMode(buzzerPin, OUTPUT);
// Start with LED and Buzzer off
digitalWrite(ledPin, LOW);
digitalWrite(buzzerPin, LOW);
}
void loop() {
// Read the analog value from the IR sensor
int sensorValue = analogRead(irSensorPin);
// Print the sensor value to the serial monitor
Serial.println(sensorValue);
// Check if the sensor value is below the threshold
if (sensorValue < threshold) {
// Turn on the LED and Buzzer
digitalWrite(ledPin, LOW);
digitalWrite(buzzerPin, LOW);
} else {
// Turn off the LED and Buzzer
digitalWrite(ledPin, HIGH);
digitalWrite(buzzerPin, HIGH);
}
// Small delay to avoid flooding the serial monitor
delay(100);
}
*/
// quick bouns name in screen
/*
#include <LiquidCrystal_I2C.h>
// Create an LCD object
LiquidCrystal_I2C lcd(0x27, 16, 2);
void setup() {
// Initialize the LCD
lcd.init();
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print("welcome, ");
lcd.setCursor(0, 1);
lcd.print("Adham");
}
void loop() {
// Do nothing
}
*/
// ultra sonic
/*
#include <Arduino.h>
const int trigPin = 5; // Trig pin of the ultrasonic sensor
const int echoPin = 18; // Echo pin of the ultrasonic sensor
const int ledPin = 26; // LED pin
const int buzzerPin = 25; // Buzzer pin
long duration;
float distance;
float safetyDistance = 10.0; // Threshold distance in centimeters
void setup() {
Serial.begin(115200); // Start serial communication at 115200 baud rate
pinMode(trigPin, OUTPUT); // Set trigPin as output
pinMode(echoPin, INPUT); // Set echoPin as input
pinMode(ledPin, OUTPUT); // Set ledPin as output
pinMode(buzzerPin, OUTPUT); // Set buzzerPin as output
Serial.println("Ultrasonic Sensor Initialized.");
}
void loop() {
// Clear the trigPin
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
// Trigger the sensor by setting the trigPin HIGH for 10 microseconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Read the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echoPin, HIGH);
// Calculate the distance
distance = duration * 0.034 / 2; // Speed of sound in air is 0.034 cm/us
// Print the distance on the Serial Monitor
Serial.print("Distance: ");
Serial.print(distance);
Serial.println(" cm");
// Check if the measured distance is less than the safetyDistance
if (distance < safetyDistance) {
digitalWrite(ledPin, HIGH); // Turn on the LED
tone(buzzerPin, 1000); // Turn on the buzzer at 1kHz frequency
} else {
digitalWrite(ledPin, LOW); // Turn off the LED
noTone(buzzerPin); // Turn off the buzzer
}
delay(500); // Wait for half a second before the next loop
}
*/
//Quick bouns(Ultra sonic & PIR & buzzer)
/*
#include <Arduino.h>
// Define pins
const int pirPin = 27; // PIR sensor pin
const int trigPin = 5; // Trig pin of the ultrasonic sensor
const int echoPin = 18; // Echo pin of the ultrasonic sensor
const int ledPin = 26; // LED pin
const int buzzerPin = 25; // Buzzer pin
// Variables
long duration;
float distance;
float safetyDistance = 10.0; // Threshold distance in centimeters
int pirState = LOW; // PIR motion state
void setup() {
Serial.begin(115200); // Start serial communication at 115200 baud rate
// Set pin modes
pinMode(pirPin, INPUT); // Set pirPin as input
pinMode(trigPin, OUTPUT); // Set trigPin as output
pinMode(echoPin, INPUT); // Set echoPin as input
pinMode(ledPin, OUTPUT); // Set ledPin as output
pinMode(buzzerPin, OUTPUT); // Set buzzerPin as output
Serial.println("System Initialized.");
}
void loop() {
pirState = digitalRead(pirPin); // Read PIR sensor state
if (pirState == HIGH) {
// Motion detected
Serial.println("Motion detected!");
// Clear the trigPin
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
// Trigger the sensor by setting the trigPin HIGH for 10 microseconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Read the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echoPin, HIGH);
// Calculate the distance
distance = duration * 0.034 / 2; // Speed of sound in air is 0.034 cm/us
// Print the distance on the Serial Monitor
Serial.print("Distance: ");
Serial.print(distance);
Serial.println(" cm");
// Check if the measured distance is less than the safetyDistance
if (distance < safetyDistance) {
digitalWrite(ledPin, HIGH); // Turn on the LED
tone(buzzerPin, 1000); // Turn on the buzzer at 1kHz frequency
} else {
digitalWrite(ledPin, LOW); // Turn off the LED
noTone(buzzerPin); // Turn off the buzzer
}
} else {
// No motion detected
digitalWrite(ledPin, LOW); // Ensure LED is off
noTone(buzzerPin); // Ensure buzzer is off
}
delay(500); // Wait for half a second before the next loop
}
// */
// #include <Arduino.h>
// #include <ESP32Servo.h>
// // Pin definitions
// const int ldrPin = 35; // Analog pin for LDR
// const int irPin = 33; // Digital pin for IR sensor
// const int buzzerPin = 26; // Digital pin for buzzer
// const int potPin = 34; // Analog pin for potentiometer
// const int servoPin = 13; // PWM pin for servo
// const int ledPin = 25; // Digital pin for LED
// const int buttonPin = 14; // Digital pin for reset button
// // Variables
// Servo servo;
// int ldrValue;
// int irValue;
// int potValue;
// bool personDetected;
// bool darknessDetected;
// int servoPosition;
// void setup() {
// Serial.begin(115200);
// pinMode(irPin, INPUT);
// pinMode(buzzerPin, OUTPUT);
// pinMode(ledPin, OUTPUT);
// pinMode(buttonPin, INPUT_PULLUP);
// servo.attach(servoPin);
// Serial.println("Smart Home Automation System Initialized.");
// }
// void loop() {
// // Read LDR value
// ldrValue = analogRead(ldrPin);
// darknessDetected = (ldrValue < 500); // Adjust threshold as needed
// // Read IR sensor value
// irValue = digitalRead(irPin);
// personDetected = (irValue == HIGH);
// // Read potentiometer value
// potValue = analogRead(potPin);
// // Check reset button
// if (digitalRead(buttonPin) == LOW) {
// resetSystem();
// } else {
// // Handle LDR and IR sensor logic
// if (personDetected) {
// digitalWrite(ledPin, HIGH);
// tone(buzzerPin, 1000);
// servo.write(180);
// } else if (darknessDetected) {
// digitalWrite(ledPin, LOW);
// noTone(buzzerPin);
// servo.write(180);
// } else {
// digitalWrite(ledPin, LOW);
// noTone(buzzerPin);
// servo.write(0);
// }
// // Handle potentiometer control only if neither condition is met
// if (!personDetected && !darknessDetected) {
// servoPosition = map(potValue, 0, 4095, 0, 180);
// servo.write(servoPosition);
// }
// }
// // Print values to Serial Monitor
// Serial.print("LDR Value: ");
// Serial.print(ldrValue);
// Serial.print("\t IR Value: ");
// Serial.print(irValue);
// Serial.print("\t Potentiometer Value: ");
// Serial.print(potValue);
// Serial.print("\t Servo Position: ");
// Serial.println(servo.read()); // Read the actual position of the servo
// delay(100); // Small delay for stability
// }
// void resetSystem() {
// digitalWrite(ledPin, LOW);
// noTone(buzzerPin);
// servo.write(0);
// Serial.println("System Reset");
// }
//smart home
/*
#include <Arduino.h>
#include <ESP32Servo.h>
// Pin definitions
const int ldrPin = 35; // Analog pin for LDR
const int irPin = 33; // Digital pin for IR sensor
const int buzzerPin = 26; // Digital pin for buzzer
const int potPin = 34; // Analog pin for potentiometer
const int servoPin = 13; // PWM pin for servo
const int ledPin = 4; // Digital pin for LED
const int buttonPin = 14; // Digital pin for reset button
// Variables
Servo servo;
int ldrValue;
int irValue;
int potValue;
bool personDetected;
bool darknessDetected;
int servoPosition;
void setup() {
Serial.begin(115200);
pinMode(irPin, INPUT);
pinMode(buzzerPin, OUTPUT);
pinMode(ledPin, OUTPUT);
pinMode(buttonPin, INPUT_PULLUP);
servo.attach(servoPin);
Serial.println("Smart Home Automation System Initialized.");
}
void loop() {
// Read LDR value
ldrValue = analogRead(ldrPin);
darknessDetected = (ldrValue < 500); // Adjust threshold as needed
// Read IR sensor value
irValue = digitalRead(irPin);
personDetected = (irValue == HIGH);
// Read potentiometer value
potValue = analogRead(potPin);
// Check reset button
if (digitalRead(buttonPin) == LOW) {
resetSystem();
} else {
// Handle LDR and IR sensor logic
if (personDetected) {
Serial.println("Person detected");
digitalWrite(ledPin, HIGH);
tone(buzzerPin, 1000);
servo.write(180);
} else if (darknessDetected) {
Serial.println("Darkness detected");
digitalWrite(ledPin, LOW);
noTone(buzzerPin);
servo.write(180);
} else {
Serial.println("No person detected and no darkness");
digitalWrite(ledPin, LOW);
noTone(buzzerPin);
servo.write(0);
}
// Handle potentiometer control only if neither condition is met
if (!personDetected && !darknessDetected) {
servoPosition = map(potValue, 0, 4095, 0, 180);
servo.write(servoPosition);
Serial.print("Servo Position: ");
Serial.println(servoPosition);
}
}
// Print values to Serial Monitor
Serial.print("LDR Value: ");
Serial.print(ldrValue);
Serial.print("\t IR Value: ");
Serial.print(irValue);
Serial.print("\t Potentiometer Value: ");
Serial.print(potValue);
Serial.print("\t Servo Position: ");
Serial.println(servo.read()); // Read the actual position of the servo
delay(100); // Small delay for stability
}
void resetSystem() {
digitalWrite(ledPin, LOW);
noTone(buzzerPin);
servo.write(0);
Serial.println("System Reset");
}
*/