Guest User

Untitled

a guest
Jul 18th, 2018
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.97 KB | None | 0 0
  1. #include <Servo.h> // For Servo motors
  2. #include <Wire.h>
  3. #include <Adafruit_MotorShield.h> // For the DC hobby motors
  4. // The speed can also be varied at 0.5% increments
  5.  
  6. // A Servo has a potentiometer inside it that counts the number of
  7. // degrees that the shaft moves in. It does not stop until it
  8. // reaches the specified number, recieved form the PWM signal.
  9.  
  10. // The Servo does not send a signal back to the micro-controller
  11. // (Closed-loop). Instead, the Servo is expected to make the turn
  12. // before the next signal is recieved.
  13. // Which is why a delay is necessary.
  14.  
  15. // The direction the Servo rotates in, depends on the length of
  16. // this pulse. 1ms will move the shaft anticlockwise at -90°,
  17. // 1.5ms will move the shaft to 0°, and a pulse of 2ms will move
  18. // the shaft clockwise at +90°. Varying these pulse widths between
  19. // the 1ms and 2ms mark, moves the shaft to a specific degree
  20. // within this maximum range of 180°.
  21.  
  22. // As there is no limit to the length of the pulse,
  23. // a Continuous Servo can rotate pass a full 360°.
  24. // But the delay will need to be increased, since it cannot be
  25. // less than the pulse width (With about 5ms to spare).
  26.  
  27. // These pulses are repeated every
  28. // 20 milliseconds (ms) so the motor holds this position.
  29.  
  30. int servoNo1Speed;
  31. int servoNo2Speed;
  32. int servoNo3Speed;
  33. int servoNo4Speed;
  34. // Setting the Position or Speed
  35. // The Parallax servo's are neutral at 94
  36.  
  37. int servoDelay=25; // 25 milliseconds
  38. // The Servo needs to be given enough time to change its position
  39.  
  40. int servoNo1comPin=9;
  41. int servoNo2comPin=10;
  42. int servoNo3comPin=11;
  43. int servoNo4comPin=6;
  44. // The Communications or 'Signal' wires are attached to the PWM pins.
  45.  
  46. Servo Servo1;
  47. Servo Servo2;
  48. Servo Servo3;
  49. Servo Servo4;
  50. // The Servos are given names, making them into objects
  51.  
  52. Adafruit_MotorShield AFMS = Adafruit_MotorShield();
  53. Adafruit_DCMotor *ServoV3 = AFMS.getMotor(1);
  54. Adafruit_DCMotor *ServoV4 = AFMS.getMotor(2);
  55.  
  56. // Declare the Adafruit_MotorShield and the separate Power supply
  57. // to each Servo a name. This also section also describes which
  58. // M port the Voltage will come from. The (1); and (2); refers
  59. // to the M1 and M2 screw-in block terminals.
  60.  
  61. // The other two servos are powered by the Motor Shield,
  62. // but controled through the PWM pins on the Arduino.
  63.  
  64. // The empty pair of brackets () on Adafruit_MotorShield()
  65. // is the default I2C address of the shield, which is 0x060
  66. // If another MotorShield is stacked ontop, a separate declaration
  67. // is needed and a 1 is placed between the brackets. This sets up
  68. // a communication line between the Arduino and the new MotorShield object
  69. // by referencing its unique I2C address of 0x061 (Manually soldered in).
  70.  
  71. void setup() {
  72. Serial.begin(9600);
  73.  
  74. // Attaching the Servo's Signal wires to their respective pins
  75. pinMode(servoNo1comPin,OUTPUT);
  76. Servo1.attach(servoNo1comPin); // Arduino Header No.9
  77.  
  78. pinMode(servoNo2comPin,OUTPUT);
  79. Servo2.attach(servoNo2comPin); // Arduino Header No.9
  80.  
  81. pinMode(servoNo3comPin,OUTPUT);
  82. Servo3.attach(servoNo3comPin); // Arduino Header No.9
  83.  
  84. pinMode(servoNo4comPin,OUTPUT);
  85. Servo4.attach(servoNo4comPin); // Arduino Header No.9
  86.  
  87. AFMS.begin();
  88. // Similar to the Serial.begin() function, but for the Shield
  89. // The empty brackets have a default frequency of 1.6KHz
  90. }
  91.  
  92. void loop() {
  93.  
  94. ServoV3->run(FORWARD); // Turns on the motor
  95. ServoV3->setSpeed(113); // 113 should be 6V
  96. // The setSpeed() parameter accepts a value between 0 and 255.
  97. // It controls the power provided to the DC motor using PWM.
  98. // while the function is called 'setSpeed', this only controls
  99. // the voltage to the Servo, which would normally set the speed
  100. // of a DC Hobby Motor.
  101.  
  102. servoNo1Speed=0;
  103. servoNo2Speed=0;
  104. servoNo3Speed=-20;
  105. servoNo4Speed=0;
  106.  
  107. servoNo1Speed+=94;
  108. servoNo2Speed+=94;
  109. servoNo3Speed+=94;
  110. servoNo4Speed+=94;
  111. // Because Parallax servo's are neutral at 94, any number the user enters
  112. // needs to less or more. So instead of requiring them to add or subtract
  113. // from 94, the Arduino does it for them.
  114.  
  115. Servo3.write(servoNo3Pos);
  116. delay(servoDelay);
  117.  
  118. }
Add Comment
Please, Sign In to add comment