Advertisement
safwan092

Untitled

Aug 6th, 2022
38
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.11 KB | None | 0 0
  1. /*
  2. Connections:
  3. --------------------------------------
  4. sensor1Pin ---> Pin 3 on Arduino Uno
  5. sensor2Pin ---> Pin 4 on Arduino Uno
  6. echoPin ---> Pin 11 on Arduino Uno
  7. trigPin ---> Pin 12 on Arduino Uno
  8. LEDPin ---> Pin 13 on Arduino Uno
  9. IN1 ---> Pin 5 on Arduino Uno
  10. IN2 ---> Pin 6 on Arduino Uno
  11. IN3 ---> Pin 7 on Arduino Uno
  12. IN4 ---> Pin 8 on Arduino Uno
  13. ENA ---> Pin 9 on Arduino Uno
  14. ENB ---> Pin 10 on Arduino Uno
  15. ---------------------------------------
  16. */
  17.  
  18. //**************************************************\\
  19.  
  20. //----------------------------------------------------------[ Pins and Global Variables ]
  21. unsigned long previousMillis = 0; // will store last time LED was updated
  22. unsigned long previousMillis1 = 0;
  23. // constants won't change:
  24. const long interval = 200; // interval at which to blink (milliseconds)
  25. unsigned long currentMillis;
  26. unsigned long currentMillis1;
  27.  
  28.  
  29. #define sensor1Pin 2 // 1st Infra-Red sensor pin on the (RIGHT) <---- Very Important
  30. int sensor1State = 0; // variable for reading sensor 1 status
  31.  
  32. #define sensor2Pin 3 // 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 = 6;
  37. int IN2 = 7;
  38. int IN3 = 8;
  39. int IN4 = 9;
  40. int ENA = 11;
  41. int ENB = 10;
  42.  
  43. #define echoPin A1 // Echo Pin
  44. #define trigPin A0 // Trigger Pin
  45.  
  46. int maximumRange = 25; // 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();
  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. // Triggering the Ultrasonic Wave :
  91. digitalWrite(trigPin, LOW);// |
  92. delayMicroseconds(2);// |
  93. digitalWrite(trigPin, HIGH);// |
  94. delayMicroseconds(10);// |
  95. digitalWrite(trigPin, LOW);// |
  96. //---------------------------------
  97.  
  98. //Sensing Ultrasonic Wave on echo Pin and callculating the duration
  99. duration = pulseIn(echoPin, HIGH);
  100.  
  101. //Calculate the distance (in cm) based on the speed of sound.
  102. distance = duration / 58.2;
  103. Serial.println(distance);
  104. // If object in range of attack start Attacking:
  105. // Conrolling car direction based on Infra-Red Sensors:
  106. //--------------------------------------------------------------------------------------------------------
  107.  
  108. // If [RIGHT] sensor is seeing [Black] and [LEFT] sensor is seeing [Black] -> [FIGHT]
  109. if (sensor1State == 1 && sensor2State == 1) {
  110. fight();
  111.  
  112. }
  113.  
  114. // If [RIGHT] sensor is seeing [Black] and [LEFT] sensor is seeing [White] -> [move Back and to the Right]
  115. else if (sensor1State == 1 && sensor2State == 0) {
  116. back();
  117. delay(200);
  118. right();
  119. delay(200);
  120. }
  121.  
  122. // If [RIGHT] sensor is seeing [White] and [LEFT] sensor is seeing [Black] -> [move Back and to the Left]
  123. else if (sensor1State == 0 && sensor2State == 1) {
  124.  
  125. back();
  126. delay(200);
  127. left();
  128. delay(200);
  129. }
  130.  
  131. // If [RIGHT] sensor is seeing [White] and [LEFT] sensor is seeing [White] -> [move Back and to the Right]
  132. else if (sensor1State == 0 && sensor2State == 0) {
  133. back();
  134. delay(200);
  135. left();
  136. delay(200);
  137. }
  138. //--------------------------------------------------------------------------------------------------------
  139. }//end of loop
  140.  
  141. //---------------------------------------------------------------------------------------------------------/End of [ Loop Function ]
  142.  
  143. //**************************************************\\
  144.  
  145. //--------------------------------------------------------------------------------------------------------[ Car Directions Functions ]
  146.  
  147. // Stopping [RIGH] motors and [LEFT] motors
  148. void stopM() {
  149. analogWrite(ENA, 0); // Setting speed of Motors on the LEFT
  150. analogWrite(ENB, 0); // Setting speed of Motors on the RIGH
  151. }
  152.  
  153. // Moving [RIGH] motors [forward] and [LEFT] motors [forward]
  154. void front() {
  155. digitalWrite(IN1, 0);
  156. digitalWrite(IN2, 1);
  157. digitalWrite(IN3, 0);
  158. digitalWrite(IN4, 1);
  159. analogWrite(ENA, 130); // Setting speed of Motors on the LEFT
  160. analogWrite(ENB, 130); // Setting speed of Motors on the RIGH
  161. }
  162.  
  163. // Moving [RIGH] motors [backwards] and [LEFT] motors [backwards]
  164. void back() {
  165. digitalWrite(IN1, 1);
  166. digitalWrite(IN2, 0);
  167. digitalWrite(IN3, 1);
  168. digitalWrite(IN4, 0);
  169. analogWrite(ENA, 150); // Setting speed of Motors on the LEFT
  170. analogWrite(ENB, 150); // Setting speed of Motors on the RIGH
  171. }
  172.  
  173. // Moving [LEFT] motors [forward] and [RIGHT] motors [backwards]
  174. void right() {
  175. digitalWrite(IN1, 1);
  176. digitalWrite(IN2, 0);
  177. digitalWrite(IN3, 0);
  178. digitalWrite(IN4, 1);
  179. analogWrite(ENA, 130); // Setting speed of Motors on the LEFT
  180. analogWrite(ENB, 130); // Setting speed of Motors on the RIGH
  181. }
  182.  
  183. // Moving [RIGH] motors [forward] and [LEFT] motors [backwards]
  184. void left() {
  185. digitalWrite(IN1, 0);
  186. digitalWrite(IN2, 1);
  187. digitalWrite(IN3, 1);
  188. digitalWrite(IN4, 0);
  189. analogWrite(ENA, 200); // Setting speed of Motors on the LEFT
  190. analogWrite(ENB, 200); // Setting speed of Motors on the RIGH
  191. }
  192. void fight() {
  193.  
  194.  
  195. if (distance <= maximumRange) {
  196. FRONTattack();
  197. //delay(500);
  198. }
  199.  
  200. // If object is not in range of attack start Searching:
  201. else {
  202. Seaching();
  203. }
  204.  
  205. }//end of fight function
  206. void FRONTattack() {
  207. currentMillis1 = millis();
  208. if (currentMillis1 - previousMillis1 >= interval) {
  209. previousMillis1 = currentMillis1;
  210. analogWrite(ENA, 220);
  211. analogWrite(ENB, 220);
  212. digitalWrite(IN1, 0);
  213. digitalWrite(IN2, 1);
  214. digitalWrite(IN3, 0);
  215. digitalWrite(IN4, 1);
  216. }
  217. }
  218. void Seaching()
  219. {
  220. currentMillis = millis();
  221. if (currentMillis - previousMillis >= interval) {
  222. previousMillis = currentMillis;
  223. right();
  224. }
  225.  
  226. }
  227.  
  228. //**************************************************\\
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement