Driving a motor to go forward/reverse continuously using push button

Hi. I really need help.

What I need is to move a dc motor forward and reverse continuously when a push button is pressed. When the push button is pressed again, it will stop moving.

I'm currently using this coding and circuit below. However this circuit automatically move the motor forward. When the push button is pressed, the motor will reverse direction.

Please help me. I've been trying to change the coding but I'm new to this so I don't get the results up till now.

I really hope someone could help. Thank you in advance!

Please read #7 below about how to post your code:

http://forum.arduino.cc/index.php/topic,148850.0.html

What I need is to move a dc motor forward and reverse continuously when a push button is pressed. When the push button is pressed again, it will stop moving.

What?

  • Do you have two pushbuttons, one for forward and one for reverse?

  • Do you want the motor to start moving back and forth (like, once per second) when a button is pushed, and stop moving when it is pushed again?

  • Do you want to go forward when a button is pressed, then stop when it is pressed again, then go backward when it is pressed again, then stop when it is pressed again, and so on in a cycle?

If you can't explain in regular language what you actually want the arduino to do, then you won't be able to explain it to an arduino (which, after all, is what programming actually is).

(ps: as mentioned in point 6 in the sticky post, don't bother saying something is urgent. Sorting out your personal schedule is your problem and not anyone else's.)

Sorry was new here. I take note now on the proper way to post a forum.

The 2nd one. When the push button is pressed, the motor will start moving back and forth. It will stop moving when it is pushed again.

Here's the coding I'm using. But I'm not able to edit and get it to what i wanted yet.

int enablePin = 11;

int in1Pin = 10;

int in2Pin = 9;

int switchPin = 7;

int potPin = 0;

int statusPin= 13;

void setup()

{

pinMode(in1Pin, OUTPUT);

pinMode(in2Pin, OUTPUT);

pinMode(enablePin, OUTPUT);

pinMode(switchPin, INPUT_PULLUP);

pinMode(statusPin,OUTPUT);

}

void loop()

{

digitalWrite(13,HIGH);

int speed = analogRead(potPin) / 4;

boolean reverse = digitalRead(switchPin);

setMotor(speed, reverse);

}

void setMotor(int speed, boolean reverse)

{

analogWrite(enablePin, speed);

digitalWrite(in1Pin, ! reverse);

digitalWrite(in2Pin, reverse);

}

The 2nd one. When the push button is pressed, the motor will start moving back and forth. It will stop moving when it is pushed again.

You probably need to search for "toggle" code.