Advertisement
mjd1990

Untitled

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