Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
#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