const int potPin = A0;
const int motorPin = 9;
int potValue;
int motorValue;
int minSpeedThreshold = 0; // Stall point
void setup() {
Serial.begin(9600);
pinMode(motorPin, OUTPUT);
}
void loop() {
potValue = analogRead(potPin); // to read potentiometer from 0 to 1023
motorValue = map(potValue, 0, 1023, 0, 255); // Mapping potentiometer value to motor speed (0 to 255)
Serial.print("potentiometer = ");
Serial.print(potValue);
Serial.print(" motor = ");
Serial.println(motorValue);
// Controlling motor speed based on potentiometer value
if (potValue > minSpeedThreshold) {
analogWrite(motorPin, motorValue); // Setting motor speed
} else {
analogWrite(motorPin, 0); // stall motor
}
delay(500);
}