Advertisement
Guest User

Untitled

a guest
Jul 23rd, 2019
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.98 KB | None | 0 0
  1. #include <Servo.h>
  2. #include <IRremote.h>
  3.  
  4.  
  5. const int pingPin = 5; // Trigger Pin of Ultrasonic Sensor
  6. const int echoPin = 4; // Echo Pin of Ultrasonic Sensor
  7. int back_left_one = 13;
  8. int back_left_second = 12;
  9. int back_right_one = 10;
  10. int back_right_second = 11;
  11. int front_left_one = 7;
  12. int front_left_second = 6;
  13. int front_right_one = 8;
  14. int front_right_second = 9;
  15.  
  16. //Servo
  17. int servoPin = 3;
  18. Servo servo;
  19. // Infrared Sensor
  20. int ir_sensor = 1;
  21. decode_results results;
  22. IRrecv irrecv(ir_sensor);
  23.  
  24.  
  25. // Autonomous driving on/off
  26. bool elon_driving = false;
  27. int pins[] = {back_left_one,back_left_second,back_right_one,back_right_second,front_left_one,front_left_second,front_right_one,front_right_second};
  28. void setup()
  29. {
  30. pinMode(pingPin, OUTPUT);
  31. pinMode(echoPin, INPUT);
  32. Serial.begin(9600);
  33. for(int i = 0; i < sizeof(pins)/sizeof(pins[0]);i++){
  34. pinMode(pins[i],OUTPUT);
  35. }
  36. servo.attach(servoPin);
  37. pinMode(ir_sensor,INPUT);
  38. irrecv.enableIRIn();
  39. Serial.println(results.value);
  40. start();
  41. }
  42.  
  43. void start(){
  44. //set all pins to full speed
  45. setMotor(pins[0],pins[1],HIGH,false);
  46. setMotor(pins[2],pins[3],HIGH,false);
  47. setMotor(pins[4],pins[5],HIGH,false);
  48. setMotor(pins[6],pins[7],HIGH,false);
  49. }
  50.  
  51. void stop(){
  52. //set all pins to low speed
  53. setMotor(pins[0],pins[1],LOW,false);
  54. setMotor(pins[2],pins[3],LOW,false);
  55. setMotor(pins[4],pins[5],LOW,false);
  56. setMotor(pins[6],pins[7],LOW,false);
  57. }
  58.  
  59. void reverse(){
  60. // reverse the speed on the second pin of the h bridge
  61. setMotor(pins[0],pins[1],HIGH,true);
  62. setMotor(pins[2],pins[3],HIGH,true);
  63. setMotor(pins[4],pins[5],HIGH,true);
  64. setMotor(pins[6],pins[7],HIGH,true);
  65. }
  66.  
  67. void driveLeft(){
  68. //left
  69. setMotor(pins[0],pins[1],HIGH,true);
  70. setMotor(pins[4],pins[5],HIGH,true);
  71. //Right
  72. setMotor(pins[2],pins[3],HIGH,false);
  73. setMotor(pins[6],pins[7],HIGH,false);
  74. }
  75.  
  76. void driveRight(){
  77. //left
  78. setMotor(pins[0],pins[1],HIGH,false);
  79. setMotor(pins[4],pins[5],HIGH,false);
  80. //Right
  81. setMotor(pins[2],pins[3],HIGH,true);
  82. setMotor(pins[6],pins[7],HIGH,true);
  83. }
  84.  
  85. long sensor_reading = 0;
  86.  
  87. void loop()
  88. {
  89.  
  90. if (irrecv.decode(&results))
  91. {
  92. switch(results.value){
  93. case 0xFF6897: elon_driving = true; break;
  94. case 0xFF9867: elon_driving = false; break;
  95. default: break;
  96. }
  97. if(elon_driving == true){
  98. autoDrive();
  99. }else{
  100. switch(results.value){
  101. case 0xFF629D: start(); break;
  102. case 0xFF22DD: driveLeft(); break;
  103. case 0xFF02FD: stop(); break;
  104. case 0xFFC23D: driveRight(); break;
  105. case 0xFFA857: reverse(); break;
  106. default: break;
  107. }
  108. }
  109. irrecv.resume(); // Receive the next value
  110. }
  111. /*
  112. sensor_reading = readDistance();
  113.  
  114. turnServo();
  115. */
  116. }
  117.  
  118. void autoDrive(){
  119. bool found_obstical = false;
  120. for (int pos = 0; pos <= 180; pos += 2) {
  121. servo.write(pos);
  122. if (readDistance() >= 20){
  123. found_obstical = true;
  124. break;
  125. }
  126. }
  127. delay(500);
  128. for (int pos = 180; pos >= 0; pos -= 2) {
  129. servo.write(pos);
  130. if (readDistance() >= 20){
  131. found_obstical = true;
  132. break;
  133. }
  134. }
  135. if(found_obstical == false){
  136. elon_driving = false;
  137. }else{
  138. reverse();
  139. delay(500);
  140. stop();
  141. autoDrive();
  142. }
  143. }
  144.  
  145. void setMotor(int first_in,int second_in,int speed, boolean reverse)
  146. {
  147. if(reverse == true){
  148. digitalWrite(first_in, LOW);
  149. digitalWrite(second_in,speed);
  150. }else{
  151. digitalWrite(first_in, speed);
  152. digitalWrite(second_in,LOW);
  153. }
  154. }
  155.  
  156. long duration;
  157. long cm;
  158. long readDistance(){
  159. digitalWrite(pingPin, LOW);
  160. delayMicroseconds(2);
  161. digitalWrite(pingPin, HIGH);
  162. delayMicroseconds(10);
  163. digitalWrite(pingPin, LOW);
  164. duration = pulseIn(echoPin, HIGH);
  165. // the ultrasonic reader is not really accuarte, so multipling by 1.2 seems to get
  166. // the sensor a lot better when it comes to accuracy.
  167. cm = microsecondsToCentimeters(duration) * 1.2;
  168. return cm;
  169. }
  170.  
  171. long microsecondsToCentimeters(long microseconds) {
  172. return microseconds / 29 / 2;
  173. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement