#include <Arduino.h>
const uint8_t ENABLE_PIN = 5;  // PWM enable controls the L293D, never a motor winding directly.
const uint8_t DIRECTION_A = 3;
const uint8_t DIRECTION_B = 4;
void drive(bool forward, uint8_t duty) {
  digitalWrite(ENABLE_PIN, LOW);  // Disable before changing direction; do not reverse under drive.
  delay(100);  // Brief coast interval added to the original; it is not measured zero-speed feedback.
  digitalWrite(DIRECTION_A, forward ? HIGH : LOW);
  digitalWrite(DIRECTION_B, forward ? LOW : HIGH);
  analogWrite(ENABLE_PIN, duty);  // Duty is 0..255; it is not a measured shaft speed.
}
void setup() {
  pinMode(ENABLE_PIN, OUTPUT); pinMode(DIRECTION_A, OUTPUT); pinMode(DIRECTION_B, OUTPUT);
  Serial.begin(9600);
}
void loop() {
  Serial.println("One way, then reverse");
  for (uint8_t i = 0; i < 5; ++i) {
    drive(true, 255); delay(500); drive(false, 255); delay(500);
  }
  digitalWrite(ENABLE_PIN, LOW); delay(2000);  // Disabled outputs let the motor coast.
  Serial.println("fast Slow example");
  drive(true, 255); delay(3000);
  digitalWrite(ENABLE_PIN, LOW); delay(1000);
  drive(false, 255); delay(3000);
  // Both inputs LOW with enable HIGH brakes the motor. The source forgot to lower DIRB.
  digitalWrite(DIRECTION_A, LOW); digitalWrite(DIRECTION_B, LOW); delay(2000);
  Serial.println("PWM full then slow");
  drive(true, 255);
  const uint8_t duties[] = {255, 180, 128, 50, 128, 180, 255};
  for (uint8_t duty : duties) { analogWrite(ENABLE_PIN, duty); delay(2000); }
  digitalWrite(ENABLE_PIN, LOW); delay(10000);
}
