Advertisement
andretafta

NodeMCU Car with L298N

Jun 29th, 2024
558
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #define ENA   D1          // Enable/speed motors Right        GPIO14(D1)
  2. #define IN_1  D2          // L298N in1 motors Rightx          GPIO15(D2)
  3. #define IN_2  D3          // L298N in2 motors Right           GPIO13(D3)
  4. #define IN_3  D4           // L298N in3 motors Left            GPIO2(D4)
  5. #define IN_4  D5           // L298N in4 motors Left            GPIO0(D5)
  6. #define ENB   D6          // Enable/speed motors Left         GPIO12(D6)
  7.  
  8.  
  9. #include <ESP8266WiFi.h>
  10. #include <WiFiClient.h>
  11. #include <ESP8266WebServer.h>
  12.  
  13. String command;             //String to store app command state.
  14. int speedCar = 800;         // 400 - 1023.
  15. int speed_Coeff = 3;
  16.  
  17. const char* ssid = "NodeMCU Car";
  18. ESP8266WebServer server(80);
  19.  
  20. void setup() {
  21.  
  22.  pinMode(ENA, OUTPUT);
  23.  pinMode(ENB, OUTPUT);  
  24.  pinMode(IN_1, OUTPUT);
  25.  pinMode(IN_2, OUTPUT);
  26.  pinMode(IN_3, OUTPUT);
  27.  pinMode(IN_4, OUTPUT);
  28.  
  29.   Serial.begin(115200);
  30.  
  31. // Connecting WiFi
  32.  
  33.   WiFi.mode(WIFI_AP);
  34.   WiFi.softAP(ssid);
  35.  
  36.   IPAddress myIP = WiFi.softAPIP();
  37.   Serial.print("AP IP address: ");
  38.   Serial.println(myIP);
  39.  
  40.  // Starting WEB-server
  41.      server.on ( "/", HTTP_handleRoot );
  42.      server.onNotFound ( HTTP_handleRoot );
  43.      server.begin();    
  44. }
  45.  
  46. void goAhead(){
  47.  
  48.       digitalWrite(IN_1, LOW);
  49.       digitalWrite(IN_2, HIGH);
  50.       analogWrite(ENA, speedCar);
  51.  
  52.       digitalWrite(IN_3, LOW);
  53.       digitalWrite(IN_4, HIGH);
  54.       analogWrite(ENB, speedCar);
  55.   }
  56.  
  57. void goBack(){
  58.  
  59.       digitalWrite(IN_1, HIGH);
  60.       digitalWrite(IN_2, LOW);
  61.       analogWrite(ENA, speedCar);
  62.  
  63.       digitalWrite(IN_3, HIGH);
  64.       digitalWrite(IN_4, LOW);
  65.       analogWrite(ENB, speedCar);
  66.   }
  67.  
  68. void goRight(){
  69.  
  70.       digitalWrite(IN_1, HIGH);
  71.       digitalWrite(IN_2, LOW);
  72.       analogWrite(ENA, speedCar);
  73.  
  74.       digitalWrite(IN_3, LOW);
  75.       digitalWrite(IN_4, HIGH);
  76.       analogWrite(ENB, speedCar);
  77.   }
  78.  
  79. void goLeft(){
  80.  
  81.       digitalWrite(IN_1, LOW);
  82.       digitalWrite(IN_2, HIGH);
  83.       analogWrite(ENA, speedCar);
  84.  
  85.       digitalWrite(IN_3, HIGH);
  86.       digitalWrite(IN_4, LOW);
  87.       analogWrite(ENB, speedCar);
  88.   }
  89.  
  90. void goAheadRight(){
  91.      
  92.       digitalWrite(IN_1, LOW);
  93.       digitalWrite(IN_2, HIGH);
  94.       analogWrite(ENA, speedCar/speed_Coeff);
  95.  
  96.       digitalWrite(IN_3, LOW);
  97.       digitalWrite(IN_4, HIGH);
  98.       analogWrite(ENB, speedCar);
  99.    }
  100.  
  101. void goAheadLeft(){
  102.      
  103.       digitalWrite(IN_1, LOW);
  104.       digitalWrite(IN_2, HIGH);
  105.       analogWrite(ENA, speedCar);
  106.  
  107.       digitalWrite(IN_3, LOW);
  108.       digitalWrite(IN_4, HIGH);
  109.       analogWrite(ENB, speedCar/speed_Coeff);
  110.   }
  111.  
  112. void goBackRight(){
  113.  
  114.       digitalWrite(IN_1, HIGH);
  115.       digitalWrite(IN_2, LOW);
  116.       analogWrite(ENA, speedCar/speed_Coeff);
  117.  
  118.       digitalWrite(IN_3, HIGH);
  119.       digitalWrite(IN_4, LOW);
  120.       analogWrite(ENB, speedCar);
  121.   }
  122.  
  123. void goBackLeft(){
  124.  
  125.       digitalWrite(IN_1, HIGH);
  126.       digitalWrite(IN_2, LOW);
  127.       analogWrite(ENA, speedCar);
  128.  
  129.       digitalWrite(IN_3, HIGH);
  130.       digitalWrite(IN_4, LOW);
  131.       analogWrite(ENB, speedCar/speed_Coeff);
  132.   }
  133.  
  134. void stopRobot(){  
  135.  
  136.       digitalWrite(IN_1, LOW);
  137.       digitalWrite(IN_2, LOW);
  138.       analogWrite(ENA, speedCar);
  139.  
  140.       digitalWrite(IN_3, LOW);
  141.       digitalWrite(IN_4, LOW);
  142.       analogWrite(ENB, speedCar);
  143.  }
  144.  
  145. void loop() {
  146.     server.handleClient();
  147.    
  148.       command = server.arg("State");
  149.       if (command == "F") goAhead();
  150.       else if (command == "B") goBack();
  151.       else if (command == "L") goLeft();
  152.       else if (command == "R") goRight();
  153.       else if (command == "I") goAheadRight();
  154.       else if (command == "G") goAheadLeft();
  155.       else if (command == "J") goBackRight();
  156.       else if (command == "H") goBackLeft();
  157.       else if (command == "0") speedCar = 400;
  158.       else if (command == "1") speedCar = 470;
  159.       else if (command == "2") speedCar = 540;
  160.       else if (command == "3") speedCar = 610;
  161.       else if (command == "4") speedCar = 680;
  162.       else if (command == "5") speedCar = 750;
  163.       else if (command == "6") speedCar = 820;
  164.       else if (command == "7") speedCar = 890;
  165.       else if (command == "8") speedCar = 960;
  166.       else if (command == "9") speedCar = 1023;
  167.       else if (command == "S") stopRobot();
  168. }
  169.  
  170. void HTTP_handleRoot(void) {
  171.  
  172. if( server.hasArg("State") ){
  173.        Serial.println(server.arg("State"));
  174.   }
  175.   server.send ( 200, "text/html", "" );
  176.   delay(1);
  177. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement