When I turn on DC motor with a button I get a a fixed PWM value that goes into a loop till button state changes.
What should I change that I could change X value with potentiometer in a loop?
int X = A0;
int SW = 2;
int Xval=5;
int SWval;
int buttonNew;
int buttonOld = 1;
int buttonState= 1;
int engine = 6;
void setup() { // This executes once
Serial.begin(9600);
pinMode(X, INPUT);
pinMode(SW, INPUT_PULLUP);
}
void loop() { // This loops continuously
Xval = analogRead(X);
buttonNew = digitalRead(SW);
if (buttonOld == 0 && buttonNew == 1) {
if (buttonState == 0) {
analogWrite(engine, Xval);
buttonState = 1;
}
else {
digitalWrite(engine, LOW);
buttonState = 0;
}
}
buttonOld = buttonNew;
delay(1);
Serial.print("button");
Serial.print(SWval);
Serial.print(" X=");
Serial.println(Xval);
}