Hello, i am having troubles with my DC motor. I was trying to make it so it can vary speed, the arduino starter book suggested me to try and do so. Here is my problem : No matter what, my arduino keeps spinning the same MAX speed.
const int switchPin = 2;
const int motorPin = 9;
const int potPin = A0;
int switchState = 0;
int potVal = 0; //Potentiometer value
int motorOut = 0; //The to be speed mapped from 0-1023 to 0-255
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
pinMode(switchPin, INPUT);
pinMode(motorPin, OUTPUT);
}
void loop() {
// put your main code here, to run repeatedly:
switchState = digitalRead(switchPin);
int potVal = analogRead(potPin);
Serial.print("Etat du switch = ");
Serial.println(switchState);
Serial.print("potVal = ");
Serial.println(potVal);
motorOut = map(potVal, 0, 1023, 0, 255);
Serial.print("Valeur de MotorOut = ");
Serial.println(motorOut); //it actually works : printing value from 0-255
float motorOut2 = motorOut/255*5; // Unused troubleshoot tries (overall useless)
if (switchState == 1) {
digitalWrite(motorPin, motorOut); //Same motor speed no matter WHAT !
delay(50);
}
else {
digitalWrite(motorPin, LOW); //thankfully, it stops
delay(50);
}
delay(5);
}


