Advertisement
safwan092

Untitled

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