refactor(server): ♻️ simplify motor control and remove code duplication

This commit is contained in:
Julien Oculi 2024-06-25 17:20:53 +02:00
parent 05e3b607a2
commit c1cdd646e2

View file

@ -196,175 +196,67 @@ void chooseCommand()
} }
} }
void writeMotor(uint8_t left, uint8_t right)
{
digitalWrite(IN4a, left & 0b1000);
digitalWrite(IN3a, left & 0b0100);
digitalWrite(IN2a, left & 0b0010);
digitalWrite(IN1a, left & 0b0001);
digitalWrite(IN4b, right & 0b1000);
digitalWrite(IN3b, right & 0b0100);
digitalWrite(IN2b, right & 0b0010);
digitalWrite(IN1b, right & 0b0001);
}
void forward() void forward()
{ {
digitalWrite(IN4a, HIGH); writeMotor(0b1000, 0b0001);
digitalWrite(IN3a, LOW);
digitalWrite(IN2a, LOW);
digitalWrite(IN1a, LOW);
digitalWrite(IN4b, LOW);
digitalWrite(IN3b, LOW);
digitalWrite(IN2b, LOW);
digitalWrite(IN1b, HIGH);
delay(delayTime); delay(delayTime);
digitalWrite(IN4a, LOW); writeMotor(0b0100, 0b0010);
digitalWrite(IN3a, HIGH);
digitalWrite(IN2a, LOW);
digitalWrite(IN1a, LOW);
digitalWrite(IN4b, LOW);
digitalWrite(IN3b, LOW);
digitalWrite(IN2b, HIGH);
digitalWrite(IN1b, LOW);
delay(delayTime); delay(delayTime);
digitalWrite(IN4a, LOW); writeMotor(0b0010, 0b0100);
digitalWrite(IN3a, LOW);
digitalWrite(IN2a, HIGH);
digitalWrite(IN1a, LOW);
digitalWrite(IN4b, LOW);
digitalWrite(IN3b, HIGH);
digitalWrite(IN2b, LOW);
digitalWrite(IN1b, LOW);
delay(delayTime); delay(delayTime);
digitalWrite(IN4a, LOW); writeMotor(0b0001, 0b1000);
digitalWrite(IN3a, LOW);
digitalWrite(IN2a, LOW);
digitalWrite(IN1a, HIGH);
digitalWrite(IN4b, HIGH);
digitalWrite(IN3b, LOW);
digitalWrite(IN2b, LOW);
digitalWrite(IN1b, LOW);
delay(delayTime); delay(delayTime);
} }
void backward() void backward()
{ {
digitalWrite(IN4a, LOW); writeMotor(0b0001, 0b1000);
digitalWrite(IN3a, LOW);
digitalWrite(IN2a, LOW);
digitalWrite(IN1a, HIGH);
digitalWrite(IN4b, HIGH);
digitalWrite(IN3b, LOW);
digitalWrite(IN2b, LOW);
digitalWrite(IN1b, LOW);
delay(delayTime); delay(delayTime);
digitalWrite(IN4a, LOW); writeMotor(0b0010, 0b0100);
digitalWrite(IN3a, LOW);
digitalWrite(IN2a, HIGH);
digitalWrite(IN1a, LOW);
digitalWrite(IN4b, LOW);
digitalWrite(IN3b, HIGH);
digitalWrite(IN2b, LOW);
digitalWrite(IN1b, LOW);
delay(delayTime); delay(delayTime);
digitalWrite(IN4a, LOW); writeMotor(0b0100, 0b0010);
digitalWrite(IN3a, HIGH);
digitalWrite(IN2a, LOW);
digitalWrite(IN1a, LOW);
digitalWrite(IN4b, LOW);
digitalWrite(IN3b, LOW);
digitalWrite(IN2b, HIGH);
digitalWrite(IN1b, LOW);
delay(delayTime); delay(delayTime);
digitalWrite(IN4a, HIGH); writeMotor(0b1000, 0b0001);
digitalWrite(IN3a, LOW);
digitalWrite(IN2a, LOW);
digitalWrite(IN1a, LOW);
digitalWrite(IN4b, LOW);
digitalWrite(IN3b, LOW);
digitalWrite(IN2b, LOW);
digitalWrite(IN1b, HIGH);
delay(delayTime); delay(delayTime);
} }
void right() void right()
{ {
digitalWrite(IN4a, HIGH); writeMotor(0b1000, 0b1000);
digitalWrite(IN3a, LOW);
digitalWrite(IN2a, LOW);
digitalWrite(IN1a, LOW);
digitalWrite(IN4b, HIGH);
digitalWrite(IN3b, LOW);
digitalWrite(IN2b, LOW);
digitalWrite(IN1b, LOW);
delay(delayTime); delay(delayTime);
digitalWrite(IN4a, LOW); writeMotor(0b0100, 0b0100);
digitalWrite(IN3a, HIGH);
digitalWrite(IN2a, LOW);
digitalWrite(IN1a, LOW);
digitalWrite(IN4b, LOW);
digitalWrite(IN3b, HIGH);
digitalWrite(IN2b, LOW);
digitalWrite(IN1b, LOW);
delay(delayTime); delay(delayTime);
digitalWrite(IN4a, LOW); writeMotor(0b0010, 0b0010);
digitalWrite(IN3a, LOW);
digitalWrite(IN2a, HIGH);
digitalWrite(IN1a, LOW);
digitalWrite(IN4b, LOW);
digitalWrite(IN3b, LOW);
digitalWrite(IN2b, HIGH);
digitalWrite(IN1b, LOW);
delay(delayTime); delay(delayTime);
digitalWrite(IN4a, LOW); writeMotor(0b0001, 0b0001);
digitalWrite(IN3a, LOW);
digitalWrite(IN2a, LOW);
digitalWrite(IN1a, HIGH);
digitalWrite(IN4b, LOW);
digitalWrite(IN3b, LOW);
digitalWrite(IN2b, LOW);
digitalWrite(IN1b, HIGH);
delay(delayTime); delay(delayTime);
} }
void left() void left()
{ {
digitalWrite(IN4a, LOW); writeMotor(0b0001, 0b0001);
digitalWrite(IN3a, LOW);
digitalWrite(IN2a, LOW);
digitalWrite(IN1a, HIGH);
digitalWrite(IN4b, LOW);
digitalWrite(IN3b, LOW);
digitalWrite(IN2b, LOW);
digitalWrite(IN1b, HIGH);
delay(delayTime); delay(delayTime);
digitalWrite(IN4a, LOW); writeMotor(0b0010, 0b0010);
digitalWrite(IN3a, LOW);
digitalWrite(IN2a, HIGH);
digitalWrite(IN1a, LOW);
digitalWrite(IN4b, LOW);
digitalWrite(IN3b, LOW);
digitalWrite(IN2b, HIGH);
digitalWrite(IN1b, LOW);
delay(delayTime); delay(delayTime);
digitalWrite(IN4a, LOW); writeMotor(0b0100, 0b0100);
digitalWrite(IN3a, HIGH);
digitalWrite(IN2a, LOW);
digitalWrite(IN1a, LOW);
digitalWrite(IN4b, LOW);
digitalWrite(IN3b, HIGH);
digitalWrite(IN2b, LOW);
digitalWrite(IN1b, LOW);
delay(delayTime); delay(delayTime);
digitalWrite(IN4a, HIGH); writeMotor(0b1000, 0b1000);
digitalWrite(IN3a, LOW);
digitalWrite(IN2a, LOW);
digitalWrite(IN1a, LOW);
digitalWrite(IN4b, HIGH);
digitalWrite(IN3b, LOW);
digitalWrite(IN2b, LOW);
digitalWrite(IN1b, LOW);
delay(delayTime); delay(delayTime);
} }
void stopMotors(void) void stopMotors(void)
{ {
digitalWrite(IN4a, LOW); writeMotor(0b0000, 0b0000);
digitalWrite(IN3a, LOW);
digitalWrite(IN2a, LOW);
digitalWrite(IN1a, LOW);
digitalWrite(IN4b, LOW);
digitalWrite(IN3b, LOW);
digitalWrite(IN2b, LOW);
digitalWrite(IN1b, LOW);
} }