PROGRAMA AUDUINO Auto Con Grua
PROGRAMA AUDUINO Auto Con Grua
PROGRAMA AUDUINO Auto Con Grua
int sensor = 2;
//
//#include <PS4Controller.h>
#include <ESP8266WiFi.h> //
#include <WiFiClient.h> //
#include <ESP8266WebServer.h> //
void setup() {
/*Serial.begin(115200);
PS4.begin();
const uint8_t* address = esp_bt_dev_get_address();
char str[100];
sprintf(str, "ESP32's Bluetooth MAC address is - %02x:%02x:%02x:%02x:%02x:%02x",
address[0],address[1],address[2],address[3],address[4],address[5]);
Serial.println(str);*/
pinMode(sensor, INPUT); // declare sensor as input
pinMode(Status, OUTPUT); // declare LED as output
pinMode(EnableA, OUTPUT);
pinMode(EnableB, OUTPUT);
pinMode(IN1, OUTPUT);
pinMode(IN2, OUTPUT);
pinMode(IN3, OUTPUT);
pinMode(IN4, OUTPUT);
pinMode(ledP,OUTPUT);
Serial.begin(115200);
// Connecting WiFi
WiFi.mode(WIFI_AP);
WiFi.softAP(ssid);
// Starting WEB-server
server.on ( "/", HTTP_handleRoot );
server.onNotFound ( HTTP_handleRoot );
server.begin();
}
void goAhead(){
digitalWrite(IN1, LOW);
digitalWrite(IN2, HIGH);
analogWrite(EnableA, speedCar);
digitalWrite(IN3, LOW);
digitalWrite(IN4, HIGH);
analogWrite(EnableB, speedCar);
}
void goBack(){
digitalWrite(IN1, HIGH);
digitalWrite(IN2, LOW);
analogWrite(EnableA, speedCar);
digitalWrite(IN3, HIGH);
digitalWrite(IN4, LOW);
analogWrite(EnableB, speedCar);
}
void goRight(){
digitalWrite(IN1, HIGH);
digitalWrite(IN2, LOW);
analogWrite(EnableA, speedCar);
digitalWrite(IN3, LOW);
digitalWrite(IN4, HIGH);
analogWrite(EnableB, speedCar);
}
void goLeft(){
digitalWrite(IN1, LOW);
digitalWrite(IN2, HIGH);
analogWrite(EnableA, speedCar);
digitalWrite(IN3, HIGH);
digitalWrite(IN4, LOW);
analogWrite(EnableB, speedCar);
}
void goAheadRight(){
digitalWrite(IN1, LOW);
digitalWrite(IN2, HIGH);
analogWrite(EnableA, speedCar/speed_Coeff);
digitalWrite(IN3, LOW);
digitalWrite(IN4, HIGH);
analogWrite(EnableB, speedCar);
}
void goAheadLeft(){
digitalWrite(IN1, LOW);
digitalWrite(IN2, HIGH);
analogWrite(EnableA, speedCar);
digitalWrite(IN3, LOW);
digitalWrite(IN4, HIGH);
analogWrite(EnableB, speedCar/speed_Coeff);
}
void goBackRight(){
digitalWrite(IN1, HIGH);
digitalWrite(IN2, LOW);
analogWrite(EnableA, speedCar/speed_Coeff);
digitalWrite(IN3, HIGH);
digitalWrite(IN4, LOW);
analogWrite(EnableB, speedCar);
}
void goBackLeft(){
digitalWrite(IN1, HIGH);
digitalWrite(IN2, LOW);
analogWrite(EnableA, speedCar);
digitalWrite(IN3, HIGH);
digitalWrite(IN4, LOW);
analogWrite(EnableB, speedCar/speed_Coeff);
}
void stopRobot(){
digitalWrite(IN1, LOW);
digitalWrite(IN2, LOW);
analogWrite(EnableA, speedCar);
digitalWrite(IN3, LOW);
digitalWrite(IN4, LOW);
analogWrite(EnableB, speedCar);
}
void loop() {
server.handleClient();
long state = digitalRead(sensor);//esta parte es del sensor todo esto comentado
si lo descomentamos da el sensor pero hay mucha mas latencia al controlar el auto
haber si se arregla con las tres pilas si dejar comentado
/*if(state == HIGH) {
digitalWrite (Status, HIGH);
Serial.println("Motion detected!");
delay(1000);
}
else {
digitalWrite (Status, LOW);
Serial.println("Motion absent!");
delay(1000);
}*/
command = server.arg("State");
if (command == "F") goAhead();
else if (command == "B") goBack();
else if (command == "L") goLeft();
else if (command == "R") goRight();
else if (command == "I") goAheadRight();
else if (command == "G") goAheadLeft();
else if (command == "J") digitalWrite(ledP,HIGH);//goBackRight();
else if (command == "H") digitalWrite(ledP,LOW);//goBackLeft();
else if (command == "0") speedCar = 400;
else if (command == "1") speedCar = 470;
else if (command == "2") speedCar = 540;
else if (command == "3") speedCar = 610;
else if (command == "4") speedCar = 680;
else if (command == "5") speedCar = 750;
else if (command == "6") speedCar = 820;
else if (command == "7") speedCar = 890;
else if (command == "8") speedCar = 960;
else if (command == "9") speedCar = 1023;
else if (command == "S") stopRobot();
}
void HTTP_handleRoot(void) {
if( server.hasArg("State") ){
Serial.println(server.arg("State"));
}
server.send ( 200, "text/html", "" );
delay(1);
}