Advertisement
Guest User

Untitled

a guest
Feb 6th, 2016
41
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.80 KB | None | 0 0
  1. /**
  2. * @file: servo control example
  3. *
  4. * @description
  5. * drive forward, drive backward
  6. * 1 servo, LiPo battery
  7. *
  8. * The minimum (backward) and maximum (forward) values
  9. * will be different depending on your specific servo motor.
  10. * Ideally, it should be between 1 and 2 milliseconds, but in practice,
  11. * 0.5 - 2.5 milliseconds works well.
  12. * Try different values to see what numbers are best for you.
  13. *
  14. */
  15.  
  16. // include additional headers
  17. #include <Servo.h>
  18.  
  19. //global declarations
  20. #define SERVO 4 // define a pin for servo
  21. #define BUTTON 2 // define pin for button
  22. #define LED 13
  23. Servo myservo; // initialize servo
  24.  
  25. // played around with values that sets the servos to each position
  26. // these values need to be set for each servo!!!
  27. const int neutral = 1500;
  28. const int forward = 2000;
  29. const int backward = 900;
  30. int buttonState = 0;
  31. int servoState = 0;
  32.  
  33. //--- Function: Setup ()
  34. void setup() {
  35. pinMode(BUTTON, INPUT);
  36. pinMode(SERVO, OUTPUT); // want servo pin to be an output
  37. pinMode(LED, OUTPUT);
  38. myservo.attach(SERVO); // attach pin to the servo
  39. myservo.writeMicroseconds(neutral); // set servo to mid-point
  40. digitalWrite(LED, LOW);
  41. }
  42.  
  43. //--- Function: loop ()
  44. void loop() {
  45. buttonState = digitalRead(BUTTON);
  46.  
  47. if ((buttonState==1) && (servoState==0)) {
  48. digitalWrite(LED, HIGH);
  49. myservo.writeMicroseconds(backward);
  50. delay(200);
  51. //myservo.writeMicroseconds(neutral);
  52. servoState = 1;
  53. }
  54. else if ((buttonState==0) && (servoState==1)) {
  55. //myservo.writeMicroseconds(forward);
  56. //delay(200);
  57. myservo.writeMicroseconds(neutral);
  58. delay(200);
  59. servoState = 0;
  60. digitalWrite(LED, LOW);
  61. }
  62. else {
  63. digitalWrite(LED, servoState ? HIGH : LOW);
  64. myservo.writeMicroseconds(servoState ? backward : neutral);
  65. }
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement