0% found this document useful (0 votes)
40 views8 pages

IRCode

Uploaded by

kareem
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views8 pages

IRCode

Uploaded by

kareem
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

#include <WiFi.

h>
#include <WebServer.h>
#include <IRremote.h>
#include <EEPROM.h>

#define OPENSTEPS 4300; //4300


#define CLOSESTEPS 4300; //4300
#define EEPROM_SIZE 1

IRrecv IR(15);
const int stepPin = 2;
const int dirPin=4;
const int sensorPin=13;
const int feedopen=14;
int isopen=0;
int autoMode=0; // if the automatic mode of opening the door when an object is
detected activated, the value of this variable would be 1;

int SpeedCounter=0;
unsigned int deltime=100;

const char* ssid = "Kareem";


const char* password = "11112222";

int StepsTaken=0;

WebServer server(80);

void handleRoot() {
//The Local Web Page Structure:
String htmlContent = "<!DOCTYPE html><html><head><style>";
htmlContent += "body { text-align: center; }";
htmlContent += ".button { width: 100px; height: 100px; font-size: 20px; margin:
10px; }";
htmlContent += "</style></head><body>";
htmlContent += "<h1>ESP32 Control</h1>";
htmlContent += "<button class='button' id='upButton'>Up</button>";
htmlContent += "<button class='button' id='downButton'>Down</button>";
htmlContent += "<button class='button' id='leftButton'>Left</button>";
htmlContent += "<button class='button' id='rightButton'>Right</button>";
htmlContent += "<script>";
htmlContent += "document.getElementById('upButton').addEventListener('click',
function () { sendCommand('up'); });";
htmlContent += "document.getElementById('downButton').addEventListener('click',
function () { sendCommand('down'); });";
htmlContent += "document.getElementById('leftButton').addEventListener('click',
function () { sendCommand('left'); });";
htmlContent +=
"document.getElementById('rightButton').addEventListener('click', function () {
sendCommand('right'); });";
htmlContent += "function sendCommand(command) {";
htmlContent += "var xhr = new XMLHttpRequest();";
htmlContent += "xhr.open('GET', '/control?cmd=' + command, true);";
htmlContent += "xhr.send();";
htmlContent += "}";
htmlContent += "</script>";
htmlContent += "</body></html>";

server.send(200, "text/html", htmlContent);


}

void ccwRotation(){

//Closing the door


if(!isopen){
//Rotating the stepper motor clockwise
digitalWrite(dirPin, HIGH); //Setting the direction to clockwise
digitalWrite(stepPin, LOW); //Stop Rotating the motor ( in case the motor was
rotating in the oppsite directrion it ensures that it is stationary )
delayMicroseconds(100);// delay to make time for the shaft of the motor to
respond to the stopping command
StepsTaken=0;

for (int x = 0; x < 4300; x++) {


if (IR.decodedIRData.decodedRawData == 0xA55AFF00) {//right button
Serial2.println("Right");
if(deltime>=50)
deltime-=10;
else deltime=50;
}
else if (IR.decodedIRData.decodedRawData == 0xF708FF00) {//left button
Serial2.println("Left");
if(deltime<=200)
deltime +=10;
else deltime=200;}

digitalWrite(stepPin, HIGH);
delayMicroseconds(deltime); //time of rotation
digitalWrite(stepPin, LOW); //stopping the rotaion (the step)
delayMicroseconds(deltime); // time before starting the next step
StepsTaken=+1; //the steps counter counts negative values when the door
is closing
//so that if something happens and the closing got
interrupted, the system
// knows how much steps to take in closing or opening the
door
}
isopen=1;
EEPROM.write(0,isopen);
EEPROM.commit();

}
}

void cwRotation(){ //Opening the door


if (isopen){

//Rotating the stepper motor counterclockwise


digitalWrite(dirPin, LOW);
digitalWrite(stepPin, LOW);
delayMicroseconds(100);

if (IR.decodedIRData.decodedRawData == 0xA55AFF00) {//right button


Serial2.println("Right");
if(deltime>=50)
deltime-=10;
else deltime=50;
}
else if (IR.decodedIRData.decodedRawData == 0xF708FF00) {//left button
Serial2.println("Left");
if(deltime<=200)
deltime +=10;
else deltime=200;}
// Code to move up
for (int x = 0; x < 4300; x++) {
if (digitalRead(feedopen)) break;
digitalWrite(stepPin, HIGH);
delayMicroseconds(deltime);
digitalWrite(stepPin, LOW);
delayMicroseconds(deltime);
StepsTaken=+1; //the steps counter counts positve values when the door is
opening
//so that if something happens and the opening got
interrupted, the system
// knows how much steps to take in closing or opening the
door
}
isopen=0;
EEPROM.write(0,isopen);
EEPROM.commit();
}}

//This Function is responsible for opening the door when the ir sensor detects an
object
void openSensor(){
if (autoMode){ // if the automatic mode is activated
//if the sensor detects an object, it waits, if the object is still detected
it opens the door
while(!digitalRead(sensorPin)){ //while you detect something, wait, open the
door, wait longer, close
if (isopen){ //If this function opens the door, it closes it. Otherwise it has
no control over the system whatsoever.
delay(1000);
if(!digitalRead(sensorPin)){
cwRotation();
Serial.println("Sensor detects an object, Opening");
delay(1000);

}
if (digitalRead(sensorPin)){
delay(1000);
delay(1000);
delay(1000);
delay(1000);
ccwRotation();
break;
}

// After Openning the door, if the sensor does not detect anything, the door is
closed
}
}
}
}

void handleCommand() {
//Handling the input from the buttons on the local web page
if (server.hasArg("cmd")) {
String command = server.arg("cmd");
// Handle the command received from the web interface here
// You can perform actions based on the command, e.g., controlling your ESP32

if (server.hasArg("cmd")) {
String command = server.arg("cmd");

if (command == "up") {
Serial.println("Up");

ccwRotation();
}
else if (command == "down") {

Serial.println("Down");
cwRotation();
}
else if (command == "left") {

Serial.println("Left");

}
else if (command == "right") {

Serial.println("Right");

server.send(200, "text/plain", "Command received: " + command);


}
}
void setup() {
EEPROM.begin(EEPROM_SIZE);
isopen= EEPROM.read(0);
IR.enableIRIn();
pinMode(4, OUTPUT);
pinMode(2, OUTPUT);
pinMode(13, INPUT);
pinMode(feedopen, INPUT);

Serial.begin(9600);

//Serial2.begin(115200);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
Serial.println("Connected to WiFi");

server.on("/", HTTP_GET, handleRoot);


server.on("/control", HTTP_GET, handleCommand);
server.begin();
}

void loop() {
server.handleClient();

if (IR.decode()) {
Serial.println(IR.decodedIRData.decodedRawData, HEX);
if (IR.decodedIRData.decodedRawData == 0xA55AFF00) {//right button
Serial2.println("Right");
if(deltime>=50)
deltime-=10;
else deltime=50;
Serial.println(deltime);
} else if (IR.decodedIRData.decodedRawData == 0xF708FF00) {//left button
Serial2.println("Left");
if(deltime<=300)
deltime +=10;
else deltime=300;
Serial.println(deltime);
} else if (IR.decodedIRData.decodedRawData == 0xE718FF00) { //up
ccwRotation();
} else if (IR.decodedIRData.decodedRawData == 0xAD52FF00) {//down
digitalWrite(stepPin, LOW);
cwRotation();
}
else if (IR.decodedIRData.decodedRawData == 0xE31CFF00) {//OK Button
autoMode=1; //Activating the Automatic Opening Mode
}
delay(10);
IR.resume();
}

// Handling the inputs to the serial port from matlab GUI:


if (Serial.available() > 0) {
String command2 = Serial.readStringUntil('\n');
if (command2 == "Up") {
Serial.println("Up");
Serial.println("Closing Door");
ccwRotation();

} else if (command2 == "Down") {


Serial.println("Opening Door");
cwRotation();

} else if (command2 == "Left") {


Serial.println("Left");

} else if (command2 == "Right") {

Serial.println("Right");

}
}

openSensor(); //this function checks if the sensor detects an object waiting by


the door, if it does it opens the door, then checks if the sensor
// does not detect anything and the door is still open, it closes
the door. This function is called repetetivly in the program's loop
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy