Skip to content

Instantly share code, notes, and snippets.

@zr0n
Created March 30, 2018 23:30
Show Gist options
  • Save zr0n/2d4fb984f32087f468acce381d3613c4 to your computer and use it in GitHub Desktop.
Save zr0n/2d4fb984f32087f468acce381d3613c4 to your computer and use it in GitHub Desktop.
#include <AFMotor.h>
#define SPEED 255
AF_DCMotor leftWheels(1);
AF_DCMotor rightWheels(2);
unsigned char serialData = 0x00;
void setup(){
Serial.begin(9600);
setSpeed();
}
void loop(){
if(Serial.available() > 0){
serialData = Serial.read();
switch(serialData){
case 48:
stop();
break;
case 49:
goForward();
break;
case 50:
goBackward();
break;
case 51:
turnLeft();
break;
case 52:
turnRight();
break;
}
}
}
void setSpeed(){
leftWheels.setSpeed(SPEED);
rightWheels.setSpeed(SPEED);
}
void turnLeft(){
rightWheels.run(FORWARD);
leftWheels.run(RELEASE);
}
void turnRight(){
leftWheels.run(FORWARD);
rightWheels.run(RELEASE);
}
void goBackward(){
leftWheels.run(BACKWARD);
rightWheels.run(BACKWARD);
}
void goForward(){
leftWheels.run(FORWARD);
rightWheels.run(FORWARD);
}
void stop(){
leftWheels.run(RELEASE);
rightWheels.run(RELEASE);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment