Using a button and a potentiometer to stop/start and change the speed of a dc motor with a h-bridge

Hey guys, I am trying to get a dc motor to start/stop with the press of button while also being able to change the speed of the motor with a potentiometer. I have used the following code and had some success. At first, I was able to get the start/stop button to work and even the potentiometer worked somewhat. However, after repeated use that motor would either only run when I held the button down, only run for a brief second or two and then stop, start when I pressed the button but then not stop, or just run as soon as I uploaded the code and not respond to any button or potentiometer. This all happened when I didn't change the physical circuit at all and not altering the code. I am really confused as to why the motor seemingly does what it wants and how to fix it.

const int controlPin1 = 2, controlPin2 = 3, onOffPin=9;
int speed;
int start = 0;
int direc ;
void setup() {
pinMode(controlPin1, OUTPUT);
pinMode(controlPin2, OUTPUT);
pinMode(onOffPin, OUTPUT);
pinMode(A1, INPUT);
// put your setup code here, to run once:

}

void loop() {
speed = map((analogRead(A2)),0,1023,10,250);
while (start == 0) {
analogWrite(onOffPin, 0);
analogWrite(controlPin1, LOW);
analogWrite(controlPin2, LOW);
if (digitalRead(A1) == HIGH) {
start = 1;
}
}

while (start == 1) {
analogWrite(onOffPin, speed);
digitalWrite(controlPin1, HIGH);
digitalWrite(controlPin2, LOW);
delay(1000);
if (digitalRead(A1) == HIGH) {
start = 0;
delay(1000);
}

}

}

Start by reading this: How to get the best out of this forum - Using Arduino / Installation & Troubleshooting - Arduino Forum

Then, tidy up your code (in the IDE - use Ctrl-T) and put it inside code tags so people can easily read it and cut&paste the code. It helps others help you.

look this over

need to recognize button change of state, press and debounce

const int controlPin1 = 2, controlPin2 = 3, onOffPin=9;
const byte PinBut = A1;

int  speed;
bool run;

int butState;

// -----------------------------------------------------------------------------
void setup () {
    pinMode (controlPin1, OUTPUT);
    pinMode (controlPin2, OUTPUT);
    pinMode (onOffPin,    OUTPUT);

    pinMode (PinBut,      INPUT_PULLUP);    // switch between pin and ground
    butState = digitalRead (PinBut);
}

// -----------------------------------------------------------------------------
void loop ()
{
    byte but = digitalRead (PinBut);
    if (butState != but)  {         // state change
        butState  = but;
        delay (20);                 // debounce

        if (LOW == but)             // pressed
            run = !run;
    }

    if (run)  {
        speed = map ((analogRead(A2)),0,1023,10,250);
        analogWrite  (onOffPin,    speed);
        digitalWrite (controlPin1, HIGH);
        digitalWrite (controlPin2, LOW);
    }
    else {    // off
        analogWrite (onOffPin,    0);
        analogWrite (controlPin1, LOW);
        analogWrite (controlPin2, LOW);
    }
}

No requirement to use a h-bridge in a single direction motor and h-bridge nothing to do with speed control.

Hi, @tjc55eng
Welcome to the forum.

Can you please post a copy of your circuit, a picture of a hand drawn circuit in jpg, png?
Hand drawn and photographed is perfectly acceptable.
Please include ALL hardware, power supplies, component names and pin labels.

Thanks.. Tom.. :smiley: :+1: :coffee: :australia:

Hi @tjc55eng ,

You might want to try ButtonToSwitch library, and use the SldrDALtchMPBttn class that gives you the chance to set a button that turns On/Off a device, and when held down acts as a potentiometer. You might try the 11_SldrDALtchMPBttn_1a example. I think it's documented enough, but if you have any dobut PM me.

Good Luck!

Gaby.//

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.