safwan092

test_code

Dec 3rd, 2024
44
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.71 KB | None | 0 0
  1. #include<AFMotor.h> // Add Adafruit Motor Shield for Arduino kit library.
  2. #include<Servo.h> // Add Servo Motor library.
  3. #define BuzzPIN A0 // Assign PIN A0 as BuzzPIN (Connect Arduino UNO "A0" PIN with Buzzer "+" PIN).
  4. #define TrigPIN A1 // Assign PIN A1 as TrigPIN (Connect Arduino UNO "A1" PIN with Ultrasonic Sonar Sensor "Trig" PIN).
  5. #define EchoPIN A2 // Assign PIN A2 as EchoPIN (Connect Arduino UNO "A2" PIN with Ultrasonic Sonar Sensor "Trig" PIN).
  6. #define LEDBPIN A3 // Assign PIN A3 as LEDBPIN (Connect Arduino UNO "A3" PIN with RGB Diffused Common Cathode "LEDB" PIN).
  7. #define LEDGPIN A4 // Assign PIN A4 as LEDGPIN (Connect Arduino UNO "A4" PIN with RGB Diffused Common Cathode "LEDG" PIN).
  8. #define LEDRPIN A5 // Assign PIN A5 as LEDRPIN (Connect Arduino UNO "A5" PIN with RGB Diffused Common Cathode "LEDR" PIN).
  9. #define DCMROFF 25 // This sets Offset to allow differences between the two DC traction Motors.
  10.  
  11. AF_DCMotor M1 (1, MOTOR12_64KHZ); // Create DCMotor #1 using M1 output, Set to 64kHz PWM frequency.
  12. AF_DCMotor M2 (2, MOTOR12_64KHZ); // Create DCMotor #2 using M2 output, Set to 64kHz PWM frequency.
  13. AF_DCMotor M3 (3, MOTOR12_64KHZ); // Create DCMotor #1 using M1 output, Set to 64kHz PWM frequency.
  14. AF_DCMotor M4 (4, MOTOR12_64KHZ); // Create DCMotor #2 using M2 output, Set to 64kHz PWM frequency.
  15.  
  16. Servo SER1; // Create Servo object to control Servo.
  17. int Search (void) { // Integer type variable declaration.
  18. float Duration = 0.0; // Float type variable declaration.
  19. float CM = 0.0; // Float type variable declaration.
  20. digitalWrite (TrigPIN, LOW); // TrigPIN output as 0V (Logic low level).
  21. delayMicroseconds (2); // Delay for 2us, Send 10 us high pulse to Ultrasonic Sonar Sensor "TrigPIN".
  22. digitalWrite (TrigPIN, HIGH); // TrigPIN output as 5V (Logic high level).
  23. delayMicroseconds (10); // Delay for 10us.
  24. digitalWrite (TrigPIN, LOW); // TrigPIN output as 0V (Logic low level).
  25. Duration = pulseIn (EchoPIN, HIGH); // Start counting time, Upto again EchoPIN back to logic "High Level" and puting the "Time" into variable called "Duration".
  26. CM = (Duration / 58.8); // Convert Distance into CM.
  27. return CM; // Return to CM.
  28. }
  29. int RightDistance, LeftDistance; // Distances on either side.
  30. float Distance = 0.00; // Float type variable declaration.
  31.  
  32.  
  33. void setup () { // Setup loop.
  34. pinMode (BuzzPIN, OUTPUT); // Declare BuzzPIN as "Output PIN".
  35. pinMode (TrigPIN, OUTPUT); // Declare TrigPIN as "Output PIN".
  36. pinMode (EchoPIN, INPUT); // Declare EchoPIN as "Output PIN".
  37. pinMode (LEDBPIN, OUTPUT); // Declare LEDBPIN as "Output PIN".
  38. pinMode (LEDGPIN, OUTPUT); // Declare LEDGPIN as "Output PIN".
  39. pinMode (LEDRPIN, OUTPUT); // Declare LEDRPIN as "Output PIN".
  40. SER1.attach (10); // Attaches the Servo on pin 10 (SER1 on the Adafruit Motor Shield for Arduino kit to the Servo object).
  41. }
  42.  
  43.  
  44. void loop () { // Main loop.
  45. SER1.write (80); // Tells the Servo to position at 80 degrees (Facing forward).
  46. delay (100); // Delay for 0.1s.
  47. Distance = Search (); // Measuring the Distance in CM.
  48. if (Distance < 30) { // If obstacle found in 30cm.
  49. digitalWrite (BuzzPIN, HIGH); // BuzzPIN output as 5V (Logic high level).
  50. digitalWrite (LEDBPIN, LOW); // LEDBPIN output as 0V (Logic low level).
  51. digitalWrite (LEDGPIN, LOW); // LEDGPIN output as 0V (Logic low level).
  52. digitalWrite (LEDRPIN, HIGH); // LEDRPIN output as 5V (Logic high level).
  53. M1.setSpeed (150); // Speed down.
  54. M2.setSpeed (150); // Speed down.
  55. M3.setSpeed (150); // Speed down.
  56. M4.setSpeed (150); // Speed down.
  57. ChangePath (); // If forward is blocked Change direction.
  58. }
  59. else if ((Distance >= 30) && (Distance < 60)) { // If obstacle found between 30cm to 60cm.
  60. digitalWrite (BuzzPIN, LOW); // BuzzPIN output as 0V (Logic low level).
  61. digitalWrite (LEDBPIN, HIGH); // LEDBPIN output as 5V (Logic high level).
  62. digitalWrite (LEDGPIN, LOW); // LEDGPIN output as 0V (Logic low level).
  63. digitalWrite (LEDRPIN, LOW); // LEDRPIN output as 0V (Logic low level).
  64. M1.setSpeed (150); // Speed increase slightly.
  65. M2.setSpeed (150); // Speed increase slightly.
  66. M3.setSpeed (150); // Speed increase slightly.
  67. M4.setSpeed (150); // Speed increase slightly.
  68. Forward (); // Robot move to Forward direction.
  69. }
  70. else if ((Distance >= 60) && (Distance < 90)) { // If obstacle found between 60cm to 90cm.
  71. digitalWrite (BuzzPIN, LOW); // BuzzPIN output as 0V (Logic low level).
  72. digitalWrite (LEDBPIN, LOW); // LEDBPIN output as 0V (Logic low level).
  73. digitalWrite (LEDGPIN, HIGH); // LEDGPIN output as 5V (Logic high level).
  74. digitalWrite (LEDRPIN, LOW); // LEDRPIN output as 0V (Logic low level).
  75. M1.setSpeed (200); // Speed up.
  76. M2.setSpeed (200); // Speed up.
  77. M3.setSpeed (200); // Speed up.
  78. M4.setSpeed (200); // Speed up.
  79. Forward (); // Robot move to Forward direction.
  80. }
  81. else { // If obstacle cannot be found in 90cm.
  82. digitalWrite (BuzzPIN, LOW); // BuzzPIN output as 0V (Logic low level).
  83. digitalWrite (LEDBPIN, HIGH); // LEDBPIN output as 5V (Logic high level).
  84. digitalWrite (LEDGPIN, HIGH); // LEDGPIN output as 5V (Logic high level).
  85. digitalWrite (LEDRPIN, HIGH); // LEDRPIN output as 5V (Logic high level).
  86. M1.setSpeed (250); // Speed increase fully.
  87. M2.setSpeed (250); // Speed increase fully.
  88. M3.setSpeed (250); // Speed increase fully.
  89. M4.setSpeed (250); // Speed increase fully.
  90. Forward (); // Robot move to Forward direction.
  91. }
  92. }
  93.  
  94. void ChangePath () { // Path Change loop.
  95. Stop (); // Robot Stop.
  96. Backward (); // Robot run Backward direction.
  97. Stop (); // Robot Stop.
  98. SER1.write (12); // Check Distance to the Right.
  99. delay (500); // Delay for 0.5s.
  100. RightDistance = Search (); // Set Right Distance.
  101. delay (500); // Delay for 0.5s.
  102. SER1.write (160); // Check Distance to the Left.
  103. delay (1000); // Delay for 1s.
  104. LeftDistance = Search (); // Set Left Distance.
  105. delay (500); // Delay for 0.5s.
  106. SER1.write (80); // Return to center.
  107. delay (500); // Delay for 0.5s.
  108. CompareDistance(); // Find the longest distance.
  109. }
  110.  
  111.  
  112. void CompareDistance () { // Distance Compare loop.
  113. if (RightDistance > LeftDistance) { // If Left is less obstructed.
  114. // Robot Turn into Left direction.
  115. TurnLeft ();
  116. }
  117. else if (LeftDistance > RightDistance) { // If Right is less obstructed.
  118. TurnRight (); // Robot Turn into Right direction.
  119. }
  120. else { // If both are equally obstructed.
  121. TurnAround (); // Robot Turn Around.
  122. }
  123. }
  124. void Forward () { // Forward loop.
  125. M1.setSpeed (255);
  126. M2.setSpeed (255);
  127. M3.setSpeed (255);
  128. M4.setSpeed (255);
  129. M1.run(FORWARD); // Turn DCMotor #1 to Forward.
  130. M2.run(FORWARD); // Turn DCMotor #1 to Forward.
  131. M3.run(FORWARD); // Turn DCMotor #1 to Forward.
  132. M4.run(FORWARD); // Turn DCMotor #1 to Forward.
  133. }
  134. void Backward () { // Backward loop.
  135. M1.setSpeed (255);
  136. M2.setSpeed (255);
  137. M3.setSpeed (255);
  138. M4.setSpeed (255);
  139. M1.run (BACKWARD); // Turn DCMotor #1 to Backward.
  140. M2.run (BACKWARD); // Turn DCMotor #2 to Backward.
  141. M3.run (BACKWARD); // Turn DCMotor #3 to Forward.
  142. M4.run (BACKWARD); // Turn DCMotor #4 to Forward.
  143. delay (500); // Delay for 1s.
  144. }
  145. void TurnRight () { // Right Turn loop.
  146. M1.setSpeed (255);
  147. M2.setSpeed (255);
  148. M3.setSpeed (255);
  149. M4.setSpeed (255);
  150. M1.run (BACKWARD); // Turn DCMotor #1 to Backward.
  151. M2.run (BACKWARD); // Turn DCMotor #2 to Forward.
  152. M3.run (FORWARD); // Turn DCMotor #3 to Forward.
  153. M4.run (FORWARD); // Turn DCMotor #4 to Forward.
  154. M1.setSpeed (100 + DCMROFF); // Calibrate the Speed of DCMotor #1.
  155. M3.setSpeed (100 + DCMROFF); // Calibrate the Speed of DCMotor #2.
  156. delay (300); // Delay for 0.7s.
  157. }
  158. void TurnLeft () { // Left Turn loop.
  159. M1.setSpeed (255);
  160. M2.setSpeed (255);
  161. M3.setSpeed (255);
  162. M4.setSpeed (255);
  163. M1.run (FORWARD); // Turn DCMotor #1 to Forward.
  164. M2.run (FORWARD); // Turn DCMotor #2 to Backward.
  165. M3.run (BACKWARD); // Turn DCMotor #3 to Forward.
  166. M4.run (BACKWARD); // Turn DCMotor #4 to Forward.
  167.  
  168. M2.setSpeed (100 + DCMROFF); // Calibrate the Speed of DCMotor #2.
  169. M4.setSpeed (100 + DCMROFF); // Calibrate the Speed of DCMotor #2.
  170. delay (300); // Delay for 0.7s.
  171. }
  172. void TurnAround () { // Trun Around loop.
  173. M1.run (FORWARD); // Turn DCMotor #1 to Forward.
  174. M2.run (BACKWARD); // Turn DCMotor #2 to Backward.
  175. M3.run (FORWARD); // Turn DCMotor #1 to Forward.
  176. M4.run (BACKWARD); // Turn DCMotor #2 to Backward.
  177. M2.setSpeed (100 + DCMROFF); // Calibrate the Speed of DCMotor #2.
  178. M4.setSpeed (100 + DCMROFF); // Calibrate the Speed of DCMotor #2.
  179. delay (700); // Delay for 2.1s.
  180. }
  181. void Stop () { // Stop loop.
  182. M1.run (RELEASE); // Release DCMotor #1.
  183. M2.run (RELEASE); // Release DCMotor #2.
  184. M3.run (RELEASE); // Turn DCMotor #1 to Forward.
  185. M4.run (RELEASE); // Turn DCMotor #2 to Backward.
  186. delay (100); // Delay for 0.1s.
  187. }
Advertisement
Add Comment
Please, Sign In to add comment