Turning on and off a DC motor with 2 buttons

Hi

I was wondering how you turn on a motor with a button and turn it off with a separate button ,
i have written a code but i cant seem to make the second button turn off the motor. The motor should be constantly running until the second button is pressed to turn it off

Here is the code i have so far

int motorPin = 3;
const int  buttonPin = 2;
boolean buttonState = LOW; //Push button
const int  buttonPin = 4;                  //reedswitch
boolean buttonState = LOW;
int motorEnabled = 0;
boolean previousButtonState = LOW;
boolean previousButtonState = LOW;
void setup() {
 pinMode(buttonPin, INPUT_PULLUP);
 pinMode(buttonPin, INPUT_PULLUP);
 pinMode(motorPin, OUTPUT);
}
void loop() {
 buttonState = digitalRead(buttonPin);
 
if(previousButtonState != buttonState && buttonState == LOW){
    motorEnabled = !motorEnabled;
    }
  if(motorEnabled == 1){
    digitalWrite (motorPin,HIGH);
    }
  else{
    digitalWrite (motorPin,LOW);
    }
  previousButtonState = buttonState;
}

Thanks :slight_smile:

boolean previousButtonState = LOW;
boolean previousButtonState = LOW;

Which button's previous state is that one variable supposed to hold?

const int  buttonPin = 2;
const int  buttonPin = 4;

Which switch is connected to the pin named buttonPin?

boolean buttonState = LOW; //Push button

booleans get assigned true or false.

Use the appropriate type - byte.

Since your code won't even compile, it is pointless to talk about it actually doing anything.