Skip to content

Instantly share code, notes, and snippets.

@lionello
Created May 5, 2017
Embed
What would you like to do?
A simple wall-following robot in Arduino (from Xinchejian roborace)
#define MOTOR_L_F 6
#define MOTOR_L_B 5
#define MOTOR_R_B 9
#define MOTOR_R_F 10
#define SENSOR_L 11
#define SENSOR_F 12
void left(int speed)
{
if (speed > 0) {
analogWrite(MOTOR_L_F, speed);
analogWrite(MOTOR_L_B, 0);
}
else {
analogWrite(MOTOR_L_F, 0);
analogWrite(MOTOR_L_B, -speed);
}
}
void right(int speed)
{
if (speed > 0) {
analogWrite(MOTOR_R_F, speed);
analogWrite(MOTOR_R_B, 0);
}
else {
analogWrite(MOTOR_R_F, 0);
analogWrite(MOTOR_R_B, -speed);
}
}
void right_forward()
{
right(200);
left(255);
}
void left_forward()
{
right(255);
left(200);
}
void right90()
{
right(-255);
left(255);
}
void setup()
{
pinMode(MOTOR_L_F, OUTPUT);
pinMode(MOTOR_L_B, OUTPUT);
pinMode(MOTOR_R_F, OUTPUT);
pinMode(MOTOR_R_B, OUTPUT);
pinMode(SENSOR_L, INPUT);
pinMode(SENSOR_F, INPUT);
pinMode(13, OUTPUT);
}
void loop()
{
int left = digitalRead(SENSOR_L);
int front = digitalRead(SENSOR_F);
if (front) {
if (left)
left_forward();
else
right_forward();
}
else {
right90();
}
delay(100);
static int l;
digitalWrite(13, l=!l);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment