Humans are really bad at repetition; computers are really good at it.
void forward()
{
moveMotor (0, 0x7F);
moveMotor (2, 0x7E);
}
void rightWall(int duration)
{
moveMotor (0, 0x40);
moveMotor (2, 0x7D);
}
void leftWall(int duration)
{
moveMotor (0, 0x7F);
moveMotor (2, 0x7D);
}
void reverse(int duration)
{
moveMotor (1, 0x45);
moveMotor (3, 0x45);
}
void leftTurn(int duration)
{
moveMotor (0, 0x45);
moveMotor (2, 0x7D);
}
void rightTurn(int duration)
{
moveMotor (0, 0x7F);
moveMotor (2, 0x45);
}
void moveMotor (unsigned char motor, unsigned char speed)
{
unsigned char buff[4];
//left motor
buff[0]=0x80;
buff[1]=0x00;
buff[2]=motor;
buff[3]=speed;
for(int i=0; i<4; i++) {
Serial.print(buff[i], BYTE);
}
}
Reducing source size gives bugs fewer dusty corners to hide in.