Advertisement
Sajib_Ahmed

Car project

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