Created
September 3, 2012 17:01
-
-
Save mstriemer/3610855 to your computer and use it in GitHub Desktop.
Running the servo with GPS
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Update the lat/lon or altitude code like this, there are some adjustments | |
// to the servo code but the setup will all be the same. | |
// 1. Add a boolean that will be true when we are in our target | |
bool cheat = false; // This line already exists but add: | |
bool inTargetArea = false; | |
// 2. Update the target check to set the boolean | |
// Change this: | |
if (altitude > 300) { | |
digitalWrite(13, HIGH); | |
} else { | |
digitalWrite(13, LOW); | |
} | |
// To this: | |
if (altitude > 300) { | |
inTargetArea = true; | |
} else { | |
inTargetArea = false; | |
} | |
// 3. Update `loop` to turn on the LED | |
// This is just a sanity check step, the board should work exactly as | |
// before with these updates, we'll add the servo later | |
void loop() { | |
if (inTargetArea()) { | |
digitalWrite(13, HIGH); | |
} else { | |
digitalWrite(13, LOW); | |
} | |
// Leave the rest of `loop` here | |
} | |
// You might want to enable the cheat to check this or just walk to the | |
// end of the street if you have target coordinates. (remember to make sure | |
// that the cheat is not commented out where you check for lat/lon) | |
// If everything is working as before: | |
// 4. Add the servo code | |
// Include the variables that are setup globally (pos, myServo and the #include) | |
// Also add a variable 'direction' to keep track of if we are turning left or right | |
#include <Servo.h> | |
Servo myservo; | |
int pos = 0; | |
int direction = 1; | |
// Copy the setup function | |
void setup() { | |
myservo.attach(9); | |
// Include the old setup code here | |
} | |
// 5. Create a function with the servo code. | |
// We're going to have to modify it a bit because we only want to do | |
// one partial rotation in this function (the sample code will do one | |
// full sweep from left to right if you run it once but we want to do one | |
// step of the sweep so that when this gets called over and over again | |
// in `loop` it will perform an entire sweep) | |
void runServo() { | |
// Calculate the next position for the servo | |
if (pos >= 180) { | |
direction = -1; // If we are all-the-way right, start going left | |
else if (pos <= 0) { | |
direction = 1; // If we are all-the-way left, start going right | |
} | |
pos = pos + direction; // Set the next position | |
myservo.write(pos); // Rotate the servo to the next position | |
delay(15); // Wait 15 ms | |
} | |
// 6. Update `loop` to run the servo | |
void loop() { | |
if (inTargetArea) { | |
// Let's run the LED too so we know we're in the target area even if | |
// there is a bug with the servo | |
digitalWrite(13, HIGH); | |
runServo(); | |
} else { | |
// Turn off the LED, we are not in the target area | |
digitalWrite(13, LOW); | |
// No need to do anything with the servo, we just won't move it | |
// if we aren't in the target area | |
} | |
// Leave the rest of `loop` untouched | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment