// Define the pins used for the motor control and speed
const int speedPin = 11;
const int dir1 = 9;
const int dir2 = 10;
// Define the pin used for the potentiometer
const int potPin = A0;
void setup() {
// Initialize the serial communication and set pin modes
Serial.begin(9600);
pinMode(speedPin, OUTPUT);
pinMode(dir1, OUTPUT);
pinMode(dir2, OUTPUT);
}
void loop() {
// Read the value of the potentiometer
int potValue = analogRead(potPin);
// Map the potentiometer value to a motor speed between 0 and 255
int motorSpeed = map(potValue, 0, 1023, 0, 255);
// Print the current motor speed to the serial monitor
Serial.print("Current speed: ");
Serial.println(motorSpeed);
// Determine the motor direction based on the potentiometer value
if (potValue < 512) {
// Set the motor direction to forward
digitalWrite(dir1, LOW);
digitalWrite(dir2, HIGH);
Serial.println(" DC motor rotates in FORWARD direction");
} else {
// Set the motor direction to reverse
digitalWrite(dir1, HIGH);
digitalWrite(dir2, LOW);
Serial.println(" DC motor rotates in REVERSE direction");
}
// Apply the PWM signal to the motor speed pin
analogWrite(speedPin, motorSpeed);
// Wait for a short period of time
delay(50);
}