Guest User

Untitled

a guest
Jan 19th, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.33 KB | None | 0 0
  1. /* Motion Following Pet Robot aka MollowBot
  2. * Using Grove kit & sensors
  3. * Conttributed by: William Hooi / OneMaker Group 2018
  4. * Inspired and based on motion follow robot by Calvin Kielas-Jensen
  5. */
  6.  
  7. #include <Servo.h>
  8. #include <Ultrasonic.h>
  9.  
  10. Servo fServo;
  11. Ultrasonic lSensor (6); //create left sensor object and attach to pin D6
  12. Ultrasonic rSensor (7); //create right sensor object and attach to pin D7
  13.  
  14. int threshold = 25; //in cm
  15. int angle = 80; //initial servo angle
  16.  
  17. long leftRanger;
  18. long rightRanger;
  19.  
  20. void setup() {
  21.  
  22. Serial.begin (9600);
  23. fServo.attach(3); //servo connected to pin D3
  24. fServo.write(angle);
  25.  
  26. }
  27.  
  28. void loop() {
  29. // put your main code here, to run repeatedly:
  30.  
  31. leftRanger = lSensor.MeasureInCentimeters(); //obtain range in cm for left sensor
  32. rightRanger = rSensor.MeasureInCentimeters(); //obtain range in cm for right sensor
  33. Serial.print("Left:");
  34. Serial.print(leftRanger);
  35. Serial.print("Right:");
  36. Serial.println(rightRanger);
  37.  
  38. follow(); //run a 'follow' function
  39.  
  40. }
  41.  
  42. void follow ()
  43. {
  44. if (leftRanger <= threshold || rightRanger <=threshold)
  45. {
  46. if (leftRanger/rightRanger < 1)
  47. angle = angle-2; //move left
  48.  
  49. if (leftRanger/rightRanger > 1)
  50. angle = angle+2; //move right
  51.  
  52. }
  53. if (angle > 160)
  54. {
  55. angle = 160;
  56. }
  57. if (angle < 0)
  58. {
  59. angle =0;
  60. }
  61. fServo.write(angle);
  62. delay (15);
  63. }
Add Comment
Please, Sign In to add comment