const int motorPin1 = D0; // Pin 0 connected to IN1 on the L298N const int motorPin2 = D1; // Pin 1 connected to IN2 on the L298N const int duration = 400; // Duration to keep the pin HIGH in milliseconds void setup() { // Initialize the digital pins as outputs pinMode(motorPin1, OUTPUT); pinMode(motorPin2, OUTPUT); // Initialize serial communication at 9600 bits per second Serial.begin(9600); } void loop() { // Check if data is available to read if (Serial.available() > 0) { // Read the incoming byte char incomingByte = Serial.read(); // Check if the incoming byte is '1' if (incomingByte == '1') { // Set motorPin1 HIGH digitalWrite(motorPin1, HIGH); digitalWrite(motorPin2, LOW); // Ensure the other pin is LOW to drive the motor in one direction // Wait for the specified duration delay(duration); // Set motorPin1 LOW digitalWrite(motorPin1, LOW); } // Check if the incoming byte is '0' if (incomingByte == '0') { // Set motorPin2 HIGH digitalWrite(motorPin2, HIGH); digitalWrite(motorPin1, LOW); // Ensure the other pin is LOW to drive the motor in the opposite direction // Wait for the specified duration delay(duration); // Set motorPin2 LOW digitalWrite(motorPin2, LOW); } } }