Advertisement
safwan092

Untitled

Aug 7th, 2022 (edited)
45
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.70 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 = 400; // interval at which to blink (milliseconds)
  25.  
  26. bool frontFlag = 0;
  27. bool rightFlag = 0;
  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(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. // 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. // If [RIGHT] sensor is seeing [Black] and [LEFT] sensor is seeing [White] -> [move Back and to the Right]
  114. else if (sensor1State == 1 && sensor2State == 0) {
  115. back();
  116. delay(400);
  117. right();
  118. delay(400);
  119. frontFlag = 0;
  120. rightFlag = 0;
  121. }
  122.  
  123. // If [RIGHT] sensor is seeing [White] and [LEFT] sensor is seeing [Black] -> [move Back and to the Left]
  124. else if (sensor1State == 0 && sensor2State == 1) {
  125.  
  126. back();
  127. delay(400);
  128. left();
  129. delay(400);
  130. frontFlag = 0;
  131. rightFlag = 0;
  132. }
  133.  
  134. // If [RIGHT] sensor is seeing [White] and [LEFT] sensor is seeing [White] -> [move Back and to the Right]
  135. else if (sensor1State == 0 && sensor2State == 0) {
  136. back();
  137. delay(400);
  138. left();
  139. delay(400);
  140. frontFlag = 0;
  141. rightFlag = 0;
  142. }
  143. //--------------------------------------------------------------------------------------------------------
  144. }//end of loop
  145.  
  146. //---------------------------------------------------------------------------------------------------------/End of [ Loop Function ]
  147.  
  148. //**************************************************\\
  149.  
  150. //--------------------------------------------------------------------------------------------------------[ Car Directions Functions ]
  151.  
  152. // Stopping [RIGH] motors and [LEFT] motors
  153. void stopM() {
  154. analogWrite(ENA, 0); // Setting speed of Motors on the LEFT
  155. analogWrite(ENB, 0); // Setting speed of Motors on the RIGH
  156. }
  157.  
  158. // Moving [RIGH] motors [forward] and [LEFT] motors [forward]
  159. void front(int s) {
  160. analogWrite(ENA, s); // Setting speed of Motors on the LEFT
  161. analogWrite(ENB, s); // Setting speed of Motors on the RIGH
  162. digitalWrite(IN1, 0);
  163. digitalWrite(IN2, 1);
  164. digitalWrite(IN3, 0);
  165. digitalWrite(IN4, 1);
  166. }
  167.  
  168. // Moving [RIGH] motors [backwards] and [LEFT] motors [backwards]
  169. void back() {
  170. digitalWrite(IN1, 1);
  171. digitalWrite(IN2, 0);
  172. digitalWrite(IN3, 1);
  173. digitalWrite(IN4, 0);
  174. analogWrite(ENA, 250); // Setting speed of Motors on the LEFT
  175. analogWrite(ENB, 250); // Setting speed of Motors on the RIGH
  176. }
  177.  
  178. // Moving [LEFT] motors [forward] and [RIGHT] motors [backwards]
  179. void right() {
  180. digitalWrite(IN1, 1);
  181. digitalWrite(IN2, 0);
  182. digitalWrite(IN3, 0);
  183. digitalWrite(IN4, 1);
  184. analogWrite(ENA, 250); // Setting speed of Motors on the LEFT
  185. analogWrite(ENB, 250); // Setting speed of Motors on the RIGH
  186. }
  187.  
  188. // Moving [RIGH] motors [forward] and [LEFT] motors [backwards]
  189. void left() {
  190. digitalWrite(IN1, 0);
  191. digitalWrite(IN2, 1);
  192. digitalWrite(IN3, 1);
  193. digitalWrite(IN4, 0);
  194. analogWrite(ENA, 250); // Setting speed of Motors on the LEFT
  195. analogWrite(ENB, 250); // Setting speed of Motors on the RIGH
  196. }
  197. void FRONTattack() {
  198.  
  199. if (millis() - previousMillis1 >= 500) {
  200. previousMillis1 = millis();
  201. front(250);
  202. }
  203. }
  204. void Seaching()
  205. {
  206. if (millis() - previousMillis >= interval && frontFlag == 0 && rightFlag == 0) {
  207. Serial.println(millis() - previousMillis);
  208. previousMillis = millis();
  209. front(200);
  210. frontFlag = 1;
  211. Serial.println("front ------ frontFlag = 1");
  212.  
  213. }
  214. else if (millis() - previousMillis >= interval && frontFlag == 1 && rightFlag == 0) {
  215. Serial.println(millis() - previousMillis);
  216. previousMillis = millis();
  217. right();
  218. rightFlag = 1;
  219. Serial.println("right ------ rightFlag = 1");
  220.  
  221. }
  222. else if (millis() - previousMillis >= interval && frontFlag == 1 && rightFlag == 1) {
  223. Serial.println(millis() - previousMillis);
  224. previousMillis = millis();
  225. right();
  226. frontFlag = 0;
  227. rightFlag = 0;
  228. Serial.println("left ------ Flags = 00");
  229.  
  230. }
  231. }
  232.  
  233. void fight() {
  234. if (distance <= maximumRange) {
  235. FRONTattack();
  236. frontFlag = 0;
  237. rightFlag = 0;
  238. //delay(500);
  239. }
  240.  
  241. // If object is not in range of attack start Searching:
  242. else {
  243. Seaching();
  244. }
  245.  
  246. }//end of fight function
  247.  
  248.  
  249. //**************************************************\\
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement