Hello.
I am trying to control my DC motor direction by pressing a button. It is spinning foward as i want but i need to keep the button pressed to go reverse. I need help please. I want to keep the current state of the button and spin the motor based on that. For example.. If(button == pressed) then spin reverse. See my code and circuit attached
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);
}