Advertisement
BimoSora

Control

May 23rd, 2024
705
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.27 KB | None | 0 0
  1. // Import required libraries
  2. #include <Arduino.h>
  3. #ifdef ESP32
  4. #include <WiFi.h>
  5. #include <AsyncTCP.h>
  6. #elif defined(ESP8266)
  7. #include <ESP8266WiFi.h>
  8. #include <ESPAsyncTCP.h>
  9. #endif
  10. #include <ESPAsyncWebServer.h>
  11. #include <FS.h>
  12. #include <Servo.h>
  13.  
  14. /* Put your SSID & Password */
  15. const char* ssid = "Car-ESP";  // Enter SSID here
  16. const char* password = "12345678";  // Enter Password here
  17.  
  18. /* Put IP Address details */
  19. IPAddress local_ip(192,168,1,1);
  20. IPAddress gateway(192,168,1,1);
  21. IPAddress subnet(255,255,255,0);
  22.  
  23. // Create AsyncWebServer object on port 80
  24. AsyncWebServer server(80);
  25.  
  26. int in1 = D4;
  27. int in2 = D3;
  28. int enA = D2;  // Enable pin for speed control
  29. const int servoPin = D1;
  30. Servo servo;
  31. int currentServoAngle = 90;  // Starting position for servo
  32.  
  33. void smoothServoMove(int targetAngle) {
  34.   while (currentServoAngle != targetAngle) {
  35.     if (currentServoAngle < targetAngle) {
  36.       currentServoAngle++;
  37.     } else if (currentServoAngle > targetAngle) {
  38.       currentServoAngle--;
  39.     }
  40.     servo.write(currentServoAngle);
  41.     delay(15); // Adjust delay for smoothness
  42.   }
  43. }
  44.  
  45. void setup() {
  46.   // Serial port for debugging purposes
  47.   Serial.begin(115200);
  48.   Serial.println();
  49.  
  50.   servo.attach(servoPin);
  51.   servo.write(currentServoAngle);
  52.  
  53.   if(SPIFFS.begin()) {
  54.     Serial.println("SPIFFS initialised OK");
  55.   }
  56.  
  57.   pinMode(in1, OUTPUT);
  58.   pinMode(in2, OUTPUT);
  59.   pinMode(enA, OUTPUT);
  60.  
  61.   WiFi.softAP(ssid, password);
  62.   WiFi.softAPConfig(local_ip, gateway, subnet);
  63.   delay(100);
  64.  
  65.   // Route for root / web page
  66.   server.on("/", HTTP_GET, [](AsyncWebServerRequest *request) {
  67.     request->send(SPIFFS, "/index.html", "text/html");
  68.   });
  69.   server.on("/assets/js/jquery.min.js", HTTP_GET, [](AsyncWebServerRequest *request) {
  70.     request->send(SPIFFS, "/assets/js/jquery.min.js", "text/javascript");
  71.   });
  72.  
  73.   server.on("/joystick", HTTP_POST, [](AsyncWebServerRequest *request) {
  74.     if (request->hasArg("x") && request->hasArg("y")) {
  75.       String xValue = request->arg("x");
  76.       String yValue = request->arg("y");
  77.       int x = xValue.toInt();
  78.       int y = yValue.toInt();
  79.  
  80.       // Calculate speed based on joystick position
  81.       int speed = map(abs(y - 512), 0, 512, 0, 255);
  82.  
  83.       // Handle motor control
  84.       if (y > 600) { // Forward
  85.         digitalWrite(in1, HIGH);
  86.         digitalWrite(in2, LOW);
  87.         analogWrite(enA, speed);
  88.       } else if (y < 400) { // Reverse
  89.         digitalWrite(in1, LOW);
  90.         digitalWrite(in2, HIGH);
  91.         analogWrite(enA, speed);
  92.       } else { // Stop
  93.         digitalWrite(in1, LOW);
  94.         digitalWrite(in2, LOW);
  95.         analogWrite(enA, 0);
  96.       }
  97.  
  98.       // Handle servo control
  99.       int targetAngle = map(x, 0, 1023, 0, 180);
  100.       smoothServoMove(targetAngle);
  101.  
  102.       request->send(200, "text/plain", "ok");
  103.     } else {
  104.       // Invalid request, return error
  105.       request->send(400, "text/plain", "Bad Request");
  106.     }
  107.   });
  108.  
  109.   server.onNotFound([](AsyncWebServerRequest *request){
  110.     // Reset motor and servo when joystick is released
  111.     digitalWrite(in1, LOW);
  112.     digitalWrite(in2, LOW);
  113.     analogWrite(enA, 0);
  114.     smoothServoMove(90);
  115.     request->send(404, "text/plain", "Not Found");
  116.   });
  117.  
  118.   // Start server
  119.   server.begin();
  120. }
  121.  
  122. void loop() {}
  123.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement