Advertisement
Daveoh

Gertboard microcontroller code for controlling servos

Nov 13th, 2012
353
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.28 KB | None | 0 0
  1. /*
  2.   Servo control test. Code by Daveoh. http://youtu.be/EhP3enceEx4
  3.   Spins a standard servo from left to right.
  4.   Configure the wait_MIN and wait_MAX variables to the microseconds required by your servo.
  5.  */
  6.  
  7. // Specify the pin used for the pulse. I've foolishly left the name as led, from the base code I started with.
  8. int led = 13; // Pin 13 of the microcontroller translates to PB5 on the Gertboard. See the user manual for this table.
  9. unsigned int wait = 1500; // The pulse duration, in microseconds.
  10. unsigned int wait_MIN = 1000; // Minimum pulse duration, in microseconds.
  11. unsigned int wait_MAX = 3900; // Maximum pulse duration, in microseconds.
  12. int servo_dir = 1; // Add or subtract from pulse duration? 1 = add, -1 = subtract
  13.  
  14. // Servo speed
  15. int servoSpeed = 100; // Speed of the servo's turn. For each run of loop(), the pulse is modified by this amount.
  16. int servoSpeedMax = 100; // Max varying speed.
  17. int servoSpeedMin = 1; // Min varying speed.
  18. int servoSpeedDir = 1; // Current direction of change. 1 = faster, -1 = slower.
  19. int servoSpeedStep = 1; // How much to change the speed by each time it is changed.
  20. int servoSpeedTicksMax = 5; // How many executions of loop() to run before changing.
  21. int servoSpeedTicks = 5; // How many executions of loop() are remaining before it is changed. Leave this as servoSpeedTicksMax.
  22.  
  23. int waitTicks = 10; // How many executions of loop() to idle for after the servo reaches its maximum turn.
  24. int waitAtEndForTicks = 0; // Keeps count of ticks.
  25.  
  26. // the setup routine runs once after the code has been uploaded to the Gertboard:
  27. void setup() {
  28.   // initialize the digital pin as an output.
  29.   pinMode(led, OUTPUT);
  30. }
  31.  
  32. // the loop routine runs over and over again forever:
  33. void loop() {
  34.   // Ensure the pulse duration is within an acceptable range.
  35.   if (wait > wait_MAX) { wait = wait_MAX; }
  36.   else if (wait < wait_MIN) { wait = wait_MIN; }
  37.  
  38.   // Pulse the servo.
  39.   digitalWrite(led, LOW);   // Ensure we are currently at the LOW voltage
  40.   delay(20);                // Wait 20 milliseconds between pulses. This might be different for your servo, but the value is typically around 20ms.
  41.   digitalWrite(led, HIGH);  // Send the HIGH signal.
  42.   delayMicroseconds(wait);  // Wait for the specified pulse duration.
  43.   digitalWrite(led, LOW);   // Return back to LOW.
  44.  
  45.   if (waitAtEndForTicks-- > 0) { return; } // Skip the rest of loop() if we are currently paused at the end of a turn.
  46.  
  47.   if (servoSpeedTicks == 0) {
  48.     // Do we need to start going faster or slower?
  49.     if (servoSpeed == servoSpeedMax) { servoSpeedDir = -1; }
  50.     else if (servoSpeed == servoSpeedMin) { servoSpeedDir = 1; }
  51.    
  52.     // Change the speed variable by the specified step.
  53.     if (servoSpeedDir == 1) { servoSpeed += servoSpeedStep; }
  54.     else { servoSpeed -= servoSpeedStep; }
  55.    
  56.     // Reset the tick count.
  57.     servoSpeedTicks = servoSpeedTicksMax;
  58.    
  59.   } else { servoSpeedTicks--; } // Decrement the tick count.
  60.  
  61.   // Change the pulse duration. Increase or decrease based on direction.
  62.   if (servo_dir == 1) { wait += servoSpeed; }
  63.   else { wait -= servoSpeed; }
  64.   if (wait >= wait_MAX) {
  65.     servo_dir = -1;
  66.     waitAtEndForTicks = waitTicks;
  67.   }
  68.   else if (wait <= wait_MIN) {
  69.     servo_dir = 1;
  70.     waitAtEndForTicks = waitTicks;
  71.   }
  72.  
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement