Skip to content

Instantly share code, notes, and snippets.

@jonathanmv
Created July 19, 2019 20:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jonathanmv/5c8a01eeb72788972d95c703a3367f45 to your computer and use it in GitHub Desktop.
Save jonathanmv/5c8a01eeb72788972d95c703a3367f45 to your computer and use it in GitHub Desktop.
#include <AFMotor.h>
AF_DCMotor left(3); // This is M3
AF_DCMotor right(4); // This is M4
// Stops both wheels
void stop() {
left.run(RELEASE);
right.run(RELEASE);
}
void setup()
{
left.setSpeed(120);
right.setSpeed(120);
}
void loop() {
delay(1000);
rotateLeft(90);
rotateRight(180);
rotateLeft(90);
}
/**
* speed 120 at 9v for 2.5s seems to make a reliable full circle
*/
void xDegrees(float x) {
int fullCircle = 2500;
delay(fullCircle * (x/360.0));
}
void rotateLeft(int degrees) {
// Stop left wheel
left.run(RELEASE);
// Start right wheel forward
right.run(FORWARD);
// Wait until the car has reached some degrees
xDegrees(degrees);
stop();
}
void rotateRight(int degrees) {
// Stop left wheel
left.run(RELEASE);
// Start right wheel backwards
right.run(BACKWARD);
xDegrees(degrees);
stop();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment