// Arduino UNO code /* * This Arduino Sketch receives commands from the ESP8266 ESP-01 module via * UART and controls the H-bridge and motors accordingly. */ // Pin definitions const int trigPin = 6; const int echoPin = 7; const int motorA1 = 9; const int motorA2 = 8; const int motorB1 = 5; const int motorB2 = 4; const int enableA = 10; const int enableB = 3; void setup() { // Initialize serial communication Serial.begin(115200); // Initialize pins pinMode(trigPin, OUTPUT); pinMode(echoPin, INPUT); pinMode(motorA1, OUTPUT); pinMode(motorA2, OUTPUT); pinMode(motorB1, OUTPUT); pinMode(motorB2, OUTPUT); pinMode(enableA, OUTPUT); pinMode(enableB, OUTPUT); // Enable motors analogWrite(enableA, 255); analogWrite(enableB, 255); } void loop() { if (Serial.available() > 0) { String command = Serial.readStringUntil('\n'); command.trim(); if (command == "forward") { moveForward(); } else if (command == "backward") { moveBackward(); } else if (command == "left") { turnLeft(); } else if (command == "right") { turnRight(); } else if (command == "stop") { stopMotors(); } } } // Motor control functions void moveForward() { digitalWrite(motorA1, HIGH); digitalWrite(motorA2, LOW); digitalWrite(motorB1, HIGH); digitalWrite(motorB2, LOW); } void moveBackward() { digitalWrite(motorA1, LOW); digitalWrite(motorA2, HIGH); digitalWrite(motorB1, LOW); digitalWrite(motorB2, HIGH); } void turnRight() { digitalWrite(motorA1, LOW); digitalWrite(motorA2, HIGH); digitalWrite(motorB1, HIGH); digitalWrite(motorB2, LOW); } void turnLeft() { digitalWrite(motorA1, HIGH); digitalWrite(motorA2, LOW); digitalWrite(motorB1, LOW); digitalWrite(motorB2, HIGH); } void stopMotors() { digitalWrite(motorA1, LOW); digitalWrite(motorA2, LOW); digitalWrite(motorB1, LOW); digitalWrite(motorB2, LOW); }