safwan092

Untitled

Oct 19th, 2018
154
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /****************************
  2. Connections:
  3. -----------------------------
  4. [Arduino Uno] --> [L298N Red Board]:
  5. 11 --> ENA (Black)/(أسود)
  6. 9 --> IN1 (White)/(أبيض)
  7. 8 --> IN2 (Gray)/(رمادي)
  8. 7 --> IN3 (Purple)/(أرجواني)
  9. 6 --> IN4 (Blue)/(أزرق)
  10. 10 --> ENB (Green)/(أخضر)
  11. [Arduino Uno] --> [Ultrasonic Sensor]:
  12. 5V --> Vcc (Purple)/(أرجواني)
  13. A1 --> Trig (Gray)/(رمادي)
  14. A0 --> Echo (White)/(أبيض)
  15. GND --> GND (Black)/(أسود)
  16. [Arduino Uno] --> [IR Sensor (Left)]:
  17. GND --> GND (Brown)/(بني)
  18. 5V --> VCC (Red)/(أحمر)
  19. 2 --> OUT (Orange)/(برتقالي)
  20. [Arduino Uno] --> [IR Sensor (Center)]:
  21. GND --> GND (Gray)/(رمادي)
  22. 5V --> VCC (White)/(أبيض)
  23. 3 --> OUT (Black)/(أسود)
  24. [Arduino Uno] --> [IR Sensor (Right)]:
  25. GND --> GND (Brown)/(بني)
  26. 5V --> VCC (Red)/(أحمر)
  27. 4 --> OUT (Orange)/(برتقالي)
  28. [Arduino Uno] --> [Buzzer]:
  29. GND --> - (Green)/(أخضر)
  30. 13 --> + (Yellow)/(أصفر)
  31. -----------------------------
  32. *****************************/
  33. const int trigPin = A1;
  34. const int echoPin = A0;
  35. // defines variables
  36. long duration;
  37. int distance;
  38.  
  39. #define SensorLeft 2 //input pin of left sensor
  40. #define SensorMiddle 3 //input pin of middle sensor
  41. #define SensorRight 4 //input pin of right sensor
  42. unsigned char SL; //state of left sensor
  43. unsigned char SM; //state of middle sensor
  44. unsigned char SR; //state of right sensor
  45.  
  46.  
  47.  
  48. #define Lpwm_pin 10 //ENB
  49. #define Rpwm_pin 11 //ENA
  50. int pinLB = 6; //IN4
  51. int pinLF = 7; //IN3
  52. int pinRB = 8; //IN2
  53. int pinRF = 9; //IN1
  54. unsigned char Lpwm_val = 100;//255 = max
  55. unsigned char Rpwm_val = 100;//255 = max
  56.  
  57. void M_Control_IO_config(void)
  58. {
  59. pinMode(trigPin, OUTPUT);
  60. pinMode(echoPin, INPUT);
  61. pinMode(SensorLeft, INPUT);
  62. pinMode(SensorMiddle, INPUT);
  63. pinMode(SensorRight, INPUT);
  64. pinMode(pinLB, OUTPUT);
  65. pinMode(pinLF, OUTPUT);
  66. pinMode(pinRB, OUTPUT);
  67. pinMode(pinRF, OUTPUT);
  68. pinMode(Lpwm_pin, OUTPUT); // pin 11 (PWM)
  69. pinMode(Rpwm_pin, OUTPUT); // pin 10 (PWM)
  70. }
  71. void Set_Speed(unsigned char Left, unsigned char Right)
  72. {
  73. analogWrite(Lpwm_pin, Left);
  74. analogWrite(Rpwm_pin, Right);
  75. }
  76.  
  77. void turnR()
  78. {
  79. digitalWrite(pinRB, LOW);
  80. digitalWrite(pinRF, HIGH);
  81. digitalWrite(pinLB, LOW);
  82. digitalWrite(pinLF, HIGH);
  83. }
  84. void back()
  85. {
  86. digitalWrite(pinRB, LOW);
  87. digitalWrite(pinRF, HIGH);
  88. digitalWrite(pinLB, HIGH);
  89. digitalWrite(pinLF, LOW);
  90. }
  91. void advance()
  92. {
  93. digitalWrite(pinRB, HIGH);
  94. digitalWrite(pinRF, LOW );
  95. digitalWrite(pinLB, LOW);
  96. digitalWrite(pinLF, HIGH);
  97. }
  98. void stopp()
  99. {
  100. digitalWrite(pinRB, HIGH);
  101. digitalWrite(pinRF, HIGH);
  102. digitalWrite(pinLB, HIGH);
  103. digitalWrite(pinLF, HIGH);
  104. }
  105. void turnL()
  106. {
  107. digitalWrite(pinRB, HIGH);
  108. digitalWrite(pinRF, LOW);
  109. digitalWrite(pinLB, HIGH);
  110. digitalWrite(pinLF, LOW);
  111. }
  112.  
  113. void hornON() {
  114. digitalWrite(13, 1);
  115. }
  116. void hornOFF() {
  117. digitalWrite(13, 0);
  118. }
  119.  
  120. void setup()
  121. {
  122. pinMode(13, OUTPUT);
  123. M_Control_IO_config();
  124. Set_Speed(Lpwm_val, Rpwm_val);
  125. stopp();
  126. delay(15000);
  127.  
  128. }
  129.  
  130. void Sensor_Scan(void)
  131. {
  132. digitalWrite(trigPin, LOW);
  133. delayMicroseconds(2);
  134. digitalWrite(trigPin, HIGH);
  135. delayMicroseconds(10);
  136. digitalWrite(trigPin, LOW);
  137. duration = pulseIn(echoPin, HIGH);
  138.  
  139. distance = duration * 0.034 / 2;
  140. SL = digitalRead(SensorLeft);
  141. SM = digitalRead(SensorMiddle);
  142. SR = digitalRead(SensorRight);
  143. }
  144.  
  145. void loop()
  146. {
  147. Sensor_Scan();
  148.  
  149. if (distance > 10) {
  150. hornOFF();
  151. ///////////////////////////////////////////////////////////////////////////////
  152. if (SM == HIGH)// middle sensor in black area
  153. {
  154. if (SL == LOW & SR == HIGH) // black on left, white on right, turn left
  155. {
  156. turnR();
  157. }
  158. else if (SR == LOW & SL == HIGH) // white on left, black on right, turn right
  159. {
  160. turnL();
  161. }
  162. else // white on both sides, going forward
  163. {
  164. advance();
  165.  
  166. }
  167. }
  168. ///////////////////////////////////////////////////////////////////////////////
  169. else // middle sensor on white area
  170. {
  171. if (SL == LOW & SR == HIGH) // black on left, white on right, turn left
  172. {
  173. turnR();
  174.  
  175. }
  176. else if (SR == LOW & SL == HIGH) // white on left, black on right, turn right
  177. {
  178. turnL();
  179.  
  180.  
  181. }
  182. else // all white, stop
  183. {
  184. // back();
  185. delay(100);
  186. stopp() ;
  187. }
  188. }
  189. ///////////////////////////////////////////////////////////////////////////////
  190. }
  191.  
  192. else {
  193. stopp();
  194. hornON();
  195. delay(500);
  196. hornOFF();
  197. delay(500);
  198. }
  199. }//end of loop
RAW Paste Data