Created
May 10, 2018 18:15
-
-
Save jrheard/fcc04f02854f49b876d6e578d82880fd to your computer and use it in GitHub Desktop.
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
// Welcome to Arduino! | |
// Here's a simple program to get you started driving your robot. | |
// We start by including the Servo library, | |
// which we'll be using to control the robot's wheels. | |
#include <Servo.h> | |
// Next, we declare a couple of Servo objects. | |
// Read on to see how these are actually used. | |
Servo servoLeft; | |
Servo servoRight; | |
// Each Arduino program has two main parts: a setup() function | |
// and a loop() function. | |
// The setup() function gets run _once_, at the start of your | |
// program. It's a good place to put initialization code | |
// (more on this in a moment), as well as any other code that you | |
// want to only run a single time (instead of over and over again). | |
void setup() { | |
// We'll start our setup() function by doing initialization stuff. | |
// These next two lines are really important - they're how we | |
// wire up these servoLeft and servoRight objects so that they | |
// can control our robot. | |
servoLeft.attach(11); | |
servoRight.attach(12); | |
// The `11` and `12` up there tell the Arduino program: | |
// "the servoLeft object should control whatever's attached to pin | |
// 11, and the servoRight object should control what's attached to pin 12". | |
// | |
// Take a look at your robot and make sure that your wheels are attached | |
// to pins 11 and 12. If they're attached to other pins, then change the two lines | |
// of code above so that they tell your program to control the correct pins. | |
// Now that our servoLeft and servoRight objects are initialized, we | |
// can drive our robot! | |
// Let's turn left for .6 seconds. | |
turnLeft(600); | |
// Now let's drive forward for a second. | |
driveForward(1000); | |
// Now let's turn right for .6 seconds. | |
drive(1700, 1700, 600); | |
// I used a few functions there: turnLeft(), driveForward(), and drive(). | |
// I'll explain them in just a minute, keep reading! | |
} | |
// Before I get there, let's take a quick break to look at the | |
// `loop()` function. | |
void loop() { | |
// Any code you put in here will automatically get run over and over, forever, in an | |
// infinite loop. | |
// | |
// For instance, if you want your robot to infinitely drive in figure 8s | |
// until someone turns it off or it runs out of battery, you'd | |
// want to put that code in this here `loop()` function. | |
} | |
// OK, now let's look at those other functions we saw earlier. | |
// | |
// Let's start with drive(). It's a function that takes three inputs: | |
// a `leftWheel` number, a `rightWheel` number, and a `duration` number. | |
// `leftWheel` and `rightWheel` should be between 1300 and 1700. | |
// 1300 means "turn this wheel clockwise" (TODO confirm), 1700 means | |
// "turn this wheel counterclockwise", and 1500 means "make this wheel stop moving". | |
// `drive()` is just a function I wrote, it doesn't come with Arduino by default, | |
// but it was really easy to write. It looks like this: | |
void drive(int leftWheel, int rightWheel, int duration) { | |
servoLeft.writeMicroseconds(leftWheel); | |
servoRight.writeMicroseconds(rightWheel); | |
delay(duration); | |
servoLeft.writeMicroseconds(1500); | |
servoRight.writeMicroseconds(1500); | |
} | |
// So, that's `drive()`. You can use it directly, or you can write some more | |
// well-named functions that sit on top of it! Here are two examples: | |
void driveForward(int duration) { | |
drive(1700, 1300, duration); | |
} | |
void turnLeft(int duration) { | |
drive(1300, 1300, duration); | |
} | |
// Those two functions let us write code like: | |
// driveForward(1000); | |
// which is a lot easier to read than: | |
// drive(1700, 1300, 1000); | |
// That's the main point of writing functions like these. | |
// We don't _need_ driveForward() and turnLeft() - we could just use drive() directly. | |
// And we don't even need drive(), we could just use servoLeft.writeMicroseconds() | |
// and servoRight.writeMicroseconds() directly. | |
// But it's a lot easier to read code that uses simple, clearly-named functions, | |
// and so that's why I wrote the ones you see above. | |
// Try writing functions like driveBackward() and turnRight()! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment