Maintain the speed of the dc motor

Good day,

Im using a potentiometer to increase and decrese the speed of a dc motor. Now, i want to make it while the speed increases and as i press a button, the speed remains at the speed it was..

for an example, if the motor's ability is from 0 rpm to 100rpm, as we use potentiometer to increase the rpm, maybe as it reach 40 or 50rpm and we press the button, the speed should be remain and although we adjust the potentiometer, the speed must be still remain..How do we do that..? Can anyone help me with the coding..? The current coding im using is :

int analogInPin = A0; int sensorValue = 0; int outputValue = 0; int transistorPin = 3;

void setup() { Serial.begin(9600);

pinMode(8, OUTPUT); pinMode(9, OUTPUT); pinMode(transistorPin, OUTPUT); }

void loop() { sensorValue = analogRead(analogInPin)/4; outputValue = map(sensorValue, 0, 1023, 0, 255);

analogWrite(transistorPin, sensorValue);

if (sensorValue >= 160) { //example digitalWrite(8, HIGH); digitalWrite(9, LOW); } else { digitalWrite(9, HIGH); digitalWrite(8, LOW); }

delay(10); }

I dont how is your connection diagram

But you should read status of the PIN you choosing. I.e either high or LOW

Once button is pressed save previous state if button pressed high u set flag =1 if button pressed again set to Zero

if button is high read old analog value of potentiometer else keep changing values

if button_pressed== high { stay in current state


{ allow potentionmeter to change value }

Rather than address the coding issue, could I suggest you use a rotary encoder, rather than a potentiometer.

Here's my reasoning. When you move the potentiometer (without affecting the speed) it will now be set on a completely irrelevant value. When you next decide to set the speed, you will then have the issue that your potentiometer is not at a relevant position to begin with.

With a rotary encoder, you simply increment or decrement with each pulse. It's a much tidier interface if you're re-using the same control for multiple settings.