Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Servo control test. Code by Daveoh. http://youtu.be/EhP3enceEx4
- Spins a standard servo from left to right.
- Configure the wait_MIN and wait_MAX variables to the microseconds required by your servo.
- */
- // Specify the pin used for the pulse. I've foolishly left the name as led, from the base code I started with.
- int led = 13; // Pin 13 of the microcontroller translates to PB5 on the Gertboard. See the user manual for this table.
- unsigned int wait = 1500; // The pulse duration, in microseconds.
- unsigned int wait_MIN = 1000; // Minimum pulse duration, in microseconds.
- unsigned int wait_MAX = 3900; // Maximum pulse duration, in microseconds.
- int servo_dir = 1; // Add or subtract from pulse duration? 1 = add, -1 = subtract
- // Servo speed
- int servoSpeed = 100; // Speed of the servo's turn. For each run of loop(), the pulse is modified by this amount.
- int servoSpeedMax = 100; // Max varying speed.
- int servoSpeedMin = 1; // Min varying speed.
- int servoSpeedDir = 1; // Current direction of change. 1 = faster, -1 = slower.
- int servoSpeedStep = 1; // How much to change the speed by each time it is changed.
- int servoSpeedTicksMax = 5; // How many executions of loop() to run before changing.
- int servoSpeedTicks = 5; // How many executions of loop() are remaining before it is changed. Leave this as servoSpeedTicksMax.
- int waitTicks = 10; // How many executions of loop() to idle for after the servo reaches its maximum turn.
- int waitAtEndForTicks = 0; // Keeps count of ticks.
- // the setup routine runs once after the code has been uploaded to the Gertboard:
- void setup() {
- // initialize the digital pin as an output.
- pinMode(led, OUTPUT);
- }
- // the loop routine runs over and over again forever:
- void loop() {
- // Ensure the pulse duration is within an acceptable range.
- if (wait > wait_MAX) { wait = wait_MAX; }
- else if (wait < wait_MIN) { wait = wait_MIN; }
- // Pulse the servo.
- digitalWrite(led, LOW); // Ensure we are currently at the LOW voltage
- delay(20); // Wait 20 milliseconds between pulses. This might be different for your servo, but the value is typically around 20ms.
- digitalWrite(led, HIGH); // Send the HIGH signal.
- delayMicroseconds(wait); // Wait for the specified pulse duration.
- digitalWrite(led, LOW); // Return back to LOW.
- if (waitAtEndForTicks-- > 0) { return; } // Skip the rest of loop() if we are currently paused at the end of a turn.
- if (servoSpeedTicks == 0) {
- // Do we need to start going faster or slower?
- if (servoSpeed == servoSpeedMax) { servoSpeedDir = -1; }
- else if (servoSpeed == servoSpeedMin) { servoSpeedDir = 1; }
- // Change the speed variable by the specified step.
- if (servoSpeedDir == 1) { servoSpeed += servoSpeedStep; }
- else { servoSpeed -= servoSpeedStep; }
- // Reset the tick count.
- servoSpeedTicks = servoSpeedTicksMax;
- } else { servoSpeedTicks--; } // Decrement the tick count.
- // Change the pulse duration. Increase or decrease based on direction.
- if (servo_dir == 1) { wait += servoSpeed; }
- else { wait -= servoSpeed; }
- if (wait >= wait_MAX) {
- servo_dir = -1;
- waitAtEndForTicks = waitTicks;
- }
- else if (wait <= wait_MIN) {
- servo_dir = 1;
- waitAtEndForTicks = waitTicks;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement