Guest User

Untitled

a guest
Jun 18th, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.09 KB | None | 0 0
  1. const int speedMotorA = 9;
  2. const int speedMotorB = 3;
  3. const int motorPin1 = 4;
  4. const int motorPin2 = 7;
  5. const int motorPin3 = 12;
  6. const int motorPin4 = 13;
  7.  
  8. void setup() {
  9. Serial.begin(9600);
  10. pinMode(speedMotorA, OUTPUT);
  11. pinMode(speedMotorB, OUTPUT);
  12. pinMode(motorPin1, OUTPUT);
  13. pinMode(motorPin2, OUTPUT);
  14. pinMode(motorPin3, OUTPUT);
  15. pinMode(motorPin4, OUTPUT);
  16. }
  17. void loop(){
  18. TurnMotorA();
  19. TurnMotorB();
  20. }
  21.  
  22.  
  23. void TurnMotorA() {
  24. int Speed = analogRead(A2);
  25. Speed = Speed*0.24926686;
  26. Serial.println(Speed);
  27. analogWrite(speedMotorA,Speed);
  28. digitalWrite(motorPin1, HIGH);
  29. digitalWrite(motorPin2, LOW);
  30. }
  31.  
  32. void TurnMotorB() {
  33. int Speed = analogRead(A2);
  34. Speed = Speed*0.24926686;
  35. analogWrite(speedMotorB,Speed);
  36. digitalWrite(motorPin3, HIGH);
  37. digitalWrite(motorPin4, LOW);
  38. }
  39.  
  40. #include <NewPing.h>
  41. #include <Servo.h>
  42.  
  43. #define TRIG_PIN A4
  44. #define ECHO_PIN A5
  45. #define MAX_DISTANCE 200
  46. NewPing sonar(TRIG_PIN, ECHO_PIN, MAX_DISTANCE);
  47. Servo myservo;
  48.  
  49. const int laser = 8;
  50. int distance = 0;
  51. const int speedMotorA = 9;
  52. const int speedMotorB = 3;
  53. const int motorPin1 = 4;
  54. const int motorPin2 = 7;
  55. const int motorPin3 = 12;
  56. const int motorPin4 = 13;
  57.  
  58. int hitlaser(){
  59. digitalWrite(laser, HIGH);
  60. delay(200);
  61. digitalWrite(laser, LOW);
  62. }
  63.  
  64. void setup() {
  65. pinMode(laser, OUTPUT);
  66. pinMode(speedMotorA, OUTPUT);
  67. pinMode(speedMotorB, OUTPUT);
  68. pinMode(motorPin1, OUTPUT);
  69. pinMode(motorPin2, OUTPUT);
  70. pinMode(motorPin3, OUTPUT);
  71. pinMode(motorPin4, OUTPUT);
  72. myservo.attach(5);
  73. myservo.write(65);
  74. delay(2000);
  75. distance = readPing();
  76. delay(100);
  77. distance = readPing();
  78. delay(100);
  79. distance = readPing();
  80. delay(100);
  81. distance = readPing();
  82. delay(100);
  83. }
  84.  
  85. void loop() {
  86. int distanceR = 0;
  87. int distanceL = 0;
  88. delay(40);
  89. digitalWrite(laser, HIGH);
  90. if(distance<=50)
  91. {
  92. moveStop();
  93. delay(100);
  94. moveBackward();
  95. delay(300);
  96. moveStop();
  97. delay(200);
  98. distanceR = lookRight();
  99. delay(200);
  100. distanceL = lookLeft();
  101. delay(200);
  102.  
  103. if(distanceR>=distanceL)
  104. {
  105. turnRight();
  106. moveStop();
  107. }else
  108. {
  109. turnLeft();
  110. moveStop();
  111. }
  112. }else
  113. {
  114. moveForward();
  115. }
  116. distance = readPing();
  117. }
  118.  
  119. int lookRight()
  120. {
  121. myservo.write(5);
  122. delay(500);
  123. int distance = readPing();
  124. delay(100);
  125. myservo.write(65);
  126. return distance;
  127. }
  128.  
  129. int lookLeft()
  130. {
  131. myservo.write(150);
  132. delay(500);
  133. int distance = readPing();
  134. delay(100);
  135. myservo.write(65);
  136. return distance;
  137. delay(100);
  138. }
  139.  
  140. int readPing() {
  141. delay(70);
  142. int cm = sonar.ping_cm();
  143. if(cm==0)
  144. {
  145. cm = 250;
  146. }
  147. return cm;
  148. }
  149.  
  150. void moveStop() {
  151. analogWrite(speedMotorA, 0);
  152. analogWrite(speedMotorB, 0);
  153. digitalWrite(motorPin1, LOW);
  154. digitalWrite(motorPin2, LOW);
  155. digitalWrite(motorPin3, LOW);
  156. digitalWrite(motorPin4, LOW);
  157. }
  158.  
  159. void moveForward() {
  160. int Speed = analogRead(A2);
  161. Speed = Speed*0.24926686;
  162. Serial.println(Speed);
  163. analogWrite(speedMotorA,Speed);
  164. analogWrite(speedMotorB,Speed);
  165. digitalWrite(motorPin1, HIGH);
  166. digitalWrite(motorPin2, LOW);
  167. digitalWrite(motorPin3, HIGH);
  168. digitalWrite(motorPin4, LOW);
  169. }
  170.  
  171. void moveBackward() {
  172. int Speed = analogRead(A2);
  173. Speed = Speed*0.24926686;
  174. Serial.println(Speed);
  175. analogWrite(speedMotorA,Speed);
  176. analogWrite(speedMotorB,Speed);
  177. digitalWrite(motorPin1, LOW);
  178. digitalWrite(motorPin2, HIGH);
  179. digitalWrite(motorPin3, LOW);
  180. digitalWrite(motorPin4, HIGH);
  181. }
  182.  
  183. void turnRight() {
  184. int Speed = analogRead(A2);
  185. Speed = Speed*0.24926686;
  186. Serial.println(Speed);
  187. analogWrite(speedMotorA,Speed);
  188. analogWrite(speedMotorB,Speed);
  189. digitalWrite(motorPin1, HIGH);
  190. digitalWrite(motorPin2, LOW);
  191. digitalWrite(motorPin3, LOW);
  192. digitalWrite(motorPin4, HIGH);
  193. delay(300);
  194. moveForward();
  195. }
  196.  
  197. void turnLeft() {
  198. int Speed = analogRead(A2);
  199. Speed = Speed*0.24926686;
  200. Serial.println(Speed);
  201. analogWrite(speedMotorA,Speed);
  202. analogWrite(speedMotorB,Speed);
  203. digitalWrite(motorPin1, LOW);
  204. digitalWrite(motorPin2, HIGH);
  205. digitalWrite(motorPin3, HIGH);
  206. digitalWrite(motorPin4, LOW);
  207. delay(300);
  208. moveForward();
  209. }
Add Comment
Please, Sign In to add comment