Advertisement
Guest User

Modified SIK Circuit 5C Autonomous Robot

a guest
Jul 1st, 2018
244
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.31 KB | None | 0 0
  1. /*
  2. Slightly modified version of: modifications: PWM frequency is set to not interfere with the 40kHz of the sensor. MAX_SPEED reduced, Timings of movements modified
  3.  
  4. SparkFun Inventor’s Kit
  5. Circuit 5C - Autonomous Robot
  6.  
  7. This robot will drive around on its own and react to obstacles by backing up and turning to a new direction.
  8. This sketch was adapted from one of the activities in the SparkFun Guide to Arduino.
  9. Check out the rest of the book at
  10. https://www.sparkfun.com/products/14326
  11.  
  12. This sketch was written by SparkFun Electronics, with lots of help from the Arduino community.
  13. This code is completely free for any use.
  14.  
  15. View circuit diagram and instructions at: https://learn.sparkfun.com/tutorials/sparkfun-inventors-kit-experiment-guide---v40
  16. Download drawings and code at: https://github.com/sparkfun/SIK-Guide-Code
  17. */
  18.  
  19. #define MAX_SPEED 200
  20. //distance variables
  21.  
  22. const int trigPin = 6;
  23. const int echoPin = 5;
  24.  
  25.  
  26. //the left motor will be controlled by the motor B pins on the motor driver
  27. const int PWMB = 3; //speed control pin on the motor driver for the left motor
  28. const int BIN2 = 8; //control pin 2 on the motor driver for the left motor
  29. const int BIN1 = 10; //control pin 1 on the motor driver for the left motor
  30.  
  31. //the right motor will be controlled by the motor A pins on the motor driver
  32. const int AIN1 = 13; //control pin 1 on the motor driver for the right motor
  33. const int AIN2 = 12; //control pin 2 on the motor driver for the right motor
  34. const int PWMA = 11; //speed control pin on the motor driver for the right motor
  35.  
  36.  
  37.  
  38. int switchPin = 7; //switch to turn the robot on and off
  39.  
  40. float distance = 0; //variable to store the distance measured by the distance sensor
  41.  
  42.  
  43.  
  44. /********************************************************************************/
  45. void setup()
  46. {
  47. pinMode(trigPin, OUTPUT); //this pin will send ultrasonic pulses out from the distance sensor
  48. pinMode(echoPin, INPUT); //this pin will sense when the pulses reflect back to the distance sensor
  49.  
  50. pinMode(switchPin, INPUT_PULLUP); //set this as a pullup to sense whether the switch is flipped
  51.  
  52.  
  53. //set the motor contro pins as outputs
  54. pinMode(AIN1, OUTPUT);
  55. pinMode(AIN2, OUTPUT);
  56. pinMode(PWMA, OUTPUT);
  57.  
  58. pinMode(BIN1, OUTPUT);
  59. pinMode(BIN2, OUTPUT);
  60. pinMode(PWMB, OUTPUT);
  61.  
  62. Serial.begin(9600); //begin serial communication with the computer
  63. Serial.print("To infinity and beyond!"); //test the serial connection
  64. }
  65.  
  66. /********************************************************************************/
  67. void loop()
  68. {
  69. //DETECT THE DISTANCE READ BY THE DISTANCE SENSOR
  70. distance = getDistance();
  71. // Set timer2 period to 31,74 kHz - this is the period of the motor drivers - not in interference with 40kHz of the untrasound sensor
  72. TCCR2B = TCCR2B & 0b11111000 | 0b00000001;
  73.  
  74. Serial.print("Distance: ");
  75. Serial.print(distance);
  76. Serial.println(" in"); // print the units
  77.  
  78. if(digitalRead(switchPin) == LOW){ //if the on switch is flipped
  79.  
  80. if(distance < 20){ //if an object is detected
  81. //back up and turn
  82. Serial.print(" ");
  83. Serial.print("BACK!");
  84.  
  85. //stop for a moment
  86. rightMotor(0);
  87. leftMotor(0);
  88. delay(1000);
  89.  
  90. //back up
  91. rightMotor(-MAX_SPEED);
  92. leftMotor(-MAX_SPEED);
  93. delay(300l*255l/MAX_SPEED); //amount of time that the robot will back up when it senses an object - depends on MAX_SPEED
  94.  
  95. //stop for a moment
  96. rightMotor(0);
  97. leftMotor(0);
  98. delay(100);
  99.  
  100. //turn to the right
  101. rightMotor(-MAX_SPEED);
  102. leftMotor(MAX_SPEED);
  103. delay(300l*255l/MAX_SPEED); //amount that the robot will turn once it has backed up - depends on MAX_SPEED
  104.  
  105. }else{ //if no obstacle is detected drive forward
  106. Serial.print(" ");
  107. Serial.print("Moving...");
  108.  
  109.  
  110. rightMotor(MAX_SPEED);
  111. leftMotor(MAX_SPEED);
  112. }
  113. } else{ //if the switch is off then stop
  114.  
  115. //stop the motors
  116. rightMotor(0);
  117. leftMotor(0);
  118. }
  119.  
  120. delay(50); //wait 50 milliseconds between readings
  121. }
  122.  
  123. /********************************************************************************/
  124. void rightMotor(int motorSpeed) //function for driving the right motor
  125. {
  126. if (motorSpeed > 0) //if the motor should drive forward (positive speed)
  127. {
  128. digitalWrite(AIN1, HIGH); //set pin 1 to high
  129. digitalWrite(AIN2, LOW); //set pin 2 to low
  130. }
  131. else if (motorSpeed < 0) //if the motor should drive backwar (negative speed)
  132. {
  133. digitalWrite(AIN1, LOW); //set pin 1 to low
  134. digitalWrite(AIN2, HIGH); //set pin 2 to high
  135. }
  136. else //if the motor should stop
  137. {
  138. digitalWrite(AIN1, LOW); //set pin 1 to low
  139. digitalWrite(AIN2, LOW); //set pin 2 to low
  140. }
  141. analogWrite(PWMA, abs(motorSpeed)); //now that the motor direction is set, drive it at the entered speed
  142. }
  143.  
  144. /********************************************************************************/
  145. void leftMotor(int motorSpeed) //function for driving the left motor
  146. {
  147. if (motorSpeed > 0) //if the motor should drive forward (positive speed)
  148. {
  149. digitalWrite(BIN1, HIGH); //set pin 1 to high
  150. digitalWrite(BIN2, LOW); //set pin 2 to low
  151. }
  152. else if (motorSpeed < 0) //if the motor should drive backwar (negative speed)
  153. {
  154. digitalWrite(BIN1, LOW); //set pin 1 to low
  155. digitalWrite(BIN2, HIGH); //set pin 2 to high
  156. }
  157. else //if the motor should stop
  158. {
  159. digitalWrite(BIN1, LOW); //set pin 1 to low
  160. digitalWrite(BIN2, LOW); //set pin 2 to low
  161. }
  162. analogWrite(PWMB, abs(motorSpeed)); //now that the motor direction is set, drive it at the entered speed
  163. }
  164.  
  165. /********************************************************************************/
  166. //RETURNS THE DISTANCE MEASURED BY THE HC-SR04 DISTANCE SENSOR
  167. float getDistance()
  168. {
  169. float echoTime; //variable to store the time it takes for a ping to bounce off an object
  170. float calcualtedDistance; //variable to store the distance calculated from the echo time
  171.  
  172. //send out an ultrasonic pulse that's 10ms long
  173. digitalWrite(trigPin, HIGH);
  174. delayMicroseconds(10);
  175. digitalWrite(trigPin, LOW);
  176.  
  177. echoTime = pulseIn(echoPin, HIGH); //use the pulsein command to see how long it takes for the
  178. //pulse to bounce back to the sensor
  179.  
  180. calcualtedDistance = echoTime / 148.0; //calculate the distance of the object that reflected the pulse (half the bounce time multiplied by the speed of sound)
  181.  
  182. return calcualtedDistance; //send back the distance that was calculated
  183. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement