Advertisement
safwan092

Sumo_Final_White

Sep 20th, 2022 (edited)
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.78 KB | None | 0 0
  1. /*
  2. Connections:
  3. --------------------------------------
  4. sensor1Pin ---> Pin 3 on Arduino Uno
  5. sensor2Pin ---> Pin 2 on Arduino Uno
  6. echoPin ---> Pin A0 on Arduino Uno
  7. trigPin ---> Pin A1 on Arduino Uno
  8. LEDPin ---> Pin 13 on Arduino Uno
  9. IN1 ---> Pin 4 on Arduino Uno
  10. IN2 ---> Pin 7 on Arduino Uno
  11. IN3 ---> Pin 8 on Arduino Uno
  12. IN4 ---> Pin 9 on Arduino Uno
  13. ENA ---> Pin 5 on Arduino Uno
  14. ENB ---> Pin 6 on Arduino Uno
  15. ---------------------------------------
  16. */
  17.  
  18. //**************************************************\\
  19.  
  20. //----------------------------------------------------------[ Pins and Global Variables ]
  21. unsigned long previousMillis = 0; // will store last time was updated
  22. unsigned long previousMillis1 = 0;
  23. // constants won't change:
  24. const long interval = 400; // interval at which to blink (milliseconds)
  25.  
  26. bool frontFlag = 0;
  27. bool rightFlag = 0;
  28.  
  29. #define sensor1Pin 3 // 1st Infra-Red sensor pin on the (RIGHT) <---- Very Important
  30. int sensor1State = 0; // variable for reading sensor 1 status
  31.  
  32. #define sensor2Pin 2 // 2nd Infra-Red sensor pin on the (LEFT) <---- Very Important
  33. int sensor2State = 0; // variable for reading sensor 2 status
  34.  
  35. // Motor Driver Board Pins
  36. int IN1 = 4;
  37. int IN2 = 7;
  38. int IN3 = 8;
  39. int IN4 = 9;
  40. int ENA = 5;
  41. int ENB = 6;
  42.  
  43. #define echoPin A0 // Echo Pin
  44. #define trigPin A1 // Trigger Pin
  45.  
  46. int maximumRange = 80; // Maximum range to start attacking
  47.  
  48. long duration, distance; // Variables to calculate distance
  49.  
  50.  
  51. //-------------------------------------------------------/End of [ Pins and Global Variables ]
  52.  
  53. //**************************************************\\
  54.  
  55. //-------------------------------------------------------------[ Setup Function ]
  56.  
  57. void setup() {
  58. Serial.begin(9600);
  59. // Delay of 5000 milli-seconds = 5 seconds
  60. delay(5000);
  61. // Declare motor driver pins as OUTPUT
  62. for (int i = 6; i < 11; i ++)
  63. {
  64. pinMode(i, OUTPUT);
  65. }
  66. // Declare Ultrasonic sensor pins as OUTPUT and INPUT
  67. pinMode(trigPin, OUTPUT);
  68. pinMode(echoPin, INPUT);
  69. // Declare Infra-Red Sensor pins as INPUT
  70. pinMode(sensor1Pin, INPUT);
  71. pinMode(sensor2Pin, INPUT);
  72.  
  73. // Moving car forward to initiate attack
  74. front(120);
  75. // This delay controls the speed of the car when going in any direction above this delay
  76. delay(300);
  77.  
  78. }
  79.  
  80. //---------------------------------------------------------------------------------------------------------/End of [ Setup Function ]
  81. //**************************************************\\
  82.  
  83. //--------------------------------------------------------------------------------------------------------[ Loop Function ]
  84.  
  85. void loop() {
  86.  
  87. // Reading Infra-Red Sensors and saving their status to corresponding variables
  88. sensor1State = digitalRead(sensor1Pin);
  89. sensor2State = digitalRead(sensor2Pin);
  90. Serial.print(sensor2State);
  91. Serial.println(sensor1State);
  92. // Triggering the Ultrasonic Wave :
  93. digitalWrite(trigPin, LOW);// |
  94. delayMicroseconds(2);// |
  95. digitalWrite(trigPin, HIGH);// |
  96. delayMicroseconds(10);// |
  97. digitalWrite(trigPin, LOW);// |
  98. //---------------------------------
  99.  
  100. //Sensing Ultrasonic Wave on echo Pin and callculating the duration
  101. duration = pulseIn(echoPin, HIGH);
  102.  
  103. //Calculate the distance (in cm) based on the speed of sound.
  104. distance = duration / 58.2;
  105. Serial.println(distance);
  106. // If object in range of attack start Attacking:
  107. // Conrolling car direction based on Infra-Red Sensors:
  108. //--------------------------------------------------------------------------------------------------------
  109.  
  110. // If [RIGHT] sensor is seeing [Black] and [LEFT] sensor is seeing [Black] -> [FIGHT]
  111. if (sensor1State == 0 && sensor2State == 0) {
  112. fight();
  113. }
  114.  
  115. // If [RIGHT] sensor is seeing [Black] and [LEFT] sensor is seeing [White] -> [move Back and to the Right]
  116. else if (sensor1State == 1 && sensor2State == 0) {
  117. back();
  118. delay(400);
  119. left();
  120. delay(400);
  121. frontFlag = 0;
  122. rightFlag = 0;
  123. }
  124.  
  125. // If [RIGHT] sensor is seeing [White] and [LEFT] sensor is seeing [Black] -> [move Back and to the Left]
  126. else if (sensor1State == 0 && sensor2State == 1) {
  127.  
  128. back();
  129. delay(400);
  130. right();
  131. delay(400);
  132. frontFlag = 0;
  133. rightFlag = 0;
  134. }
  135.  
  136. // If [RIGHT] sensor is seeing [White] and [LEFT] sensor is seeing [White] -> [move Back and to the Right]
  137. else if (sensor1State == 1 && sensor2State == 1) {
  138. back();
  139. delay(400);
  140. left();
  141. delay(400);
  142. frontFlag = 0;
  143. rightFlag = 0;
  144. }
  145. //--------------------------------------------------------------------------------------------------------
  146. }//end of loop
  147.  
  148. //---------------------------------------------------------------------------------------------------------/End of [ Loop Function ]
  149.  
  150. //**************************************************\\
  151.  
  152. //--------------------------------------------------------------------------------------------------------[ Car Directions Functions ]
  153.  
  154. // Stopping [RIGH] motors and [LEFT] motors
  155. void stopM() {
  156. analogWrite(ENA, 0); // Setting speed of Motors on the LEFT
  157. analogWrite(ENB, 0); // Setting speed of Motors on the RIGH
  158. }
  159.  
  160. // Moving [RIGH] motors [forward] and [LEFT] motors [forward]
  161. void front(int s) {
  162. analogWrite(ENA, s); // Setting speed of Motors on the LEFT
  163. analogWrite(ENB, s); // Setting speed of Motors on the RIGH
  164. digitalWrite(IN1, 0);
  165. digitalWrite(IN2, 1);
  166. digitalWrite(IN3, 0);
  167. digitalWrite(IN4, 1);
  168. }
  169.  
  170. // Moving [RIGH] motors [backwards] and [LEFT] motors [backwards]
  171. void back() {
  172. digitalWrite(IN1, 1);
  173. digitalWrite(IN2, 0);
  174. digitalWrite(IN3, 1);
  175. digitalWrite(IN4, 0);
  176. analogWrite(ENA, 80); // Setting speed of Motors on the LEFT
  177. analogWrite(ENB, 80); // Setting speed of Motors on the RIGH
  178. }
  179.  
  180. // Moving [LEFT] motors [forward] and [RIGHT] motors [backwards]
  181. void right() {
  182. digitalWrite(IN1, 1);
  183. digitalWrite(IN2, 0);
  184. digitalWrite(IN3, 0);
  185. digitalWrite(IN4, 1);
  186. analogWrite(ENA, 80); // Setting speed of Motors on the LEFT
  187. analogWrite(ENB, 80); // Setting speed of Motors on the RIGH
  188. }
  189.  
  190. // Moving [RIGH] motors [forward] and [LEFT] motors [backwards]
  191. void left() {
  192. digitalWrite(IN1, 0);
  193. digitalWrite(IN2, 1);
  194. digitalWrite(IN3, 1);
  195. digitalWrite(IN4, 0);
  196. analogWrite(ENA, 80); // Setting speed of Motors on the LEFT
  197. analogWrite(ENB, 80); // Setting speed of Motors on the RIGH
  198. }
  199. void FRONTattack() {
  200.  
  201. if (millis() - previousMillis1 >= 500) {
  202. previousMillis1 = millis();
  203. front(80);
  204. //delay(400);
  205. }
  206. }
  207. void Seaching()
  208. {
  209. if (millis() - previousMillis >= interval+300 && frontFlag == 0 && rightFlag == 0) {
  210. Serial.println(millis() - previousMillis);
  211. previousMillis = millis();
  212. front(80);
  213. //delay(400);
  214. frontFlag = 1;
  215. Serial.println("front ------ frontFlag = 1");
  216.  
  217. }
  218. else if (millis() - previousMillis >= interval && frontFlag == 1 && rightFlag == 0) {
  219. Serial.println(millis() - previousMillis);
  220. previousMillis = millis();
  221. right();
  222. //delay(400);
  223. rightFlag = 1;
  224. Serial.println("right ------ rightFlag = 1");
  225.  
  226. }
  227. else if (millis() - previousMillis >= interval && frontFlag == 1 && rightFlag == 1) {
  228. // Serial.println(millis() - previousMillis);
  229. // previousMillis = millis();
  230. // left();
  231. frontFlag = 0;
  232. rightFlag = 0;
  233. // Serial.println("left ------ Flags = 00");
  234.  
  235. }
  236. }
  237. void fight() {
  238. if (distance <= maximumRange) {
  239. FRONTattack();
  240. frontFlag = 0;
  241. rightFlag = 0;
  242. //delay(500);
  243. }
  244.  
  245. // If object is not in range of attack start Searching:
  246. else {
  247. Seaching();
  248. }
  249. }//end of fight function
  250.  
  251. //**************************************************\\
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement