Control motor direction with switches

Hi, I will produce a linear DC drive. (Wire spacing on reel)
I want to control the direction of rotation left and right with two buttons.

When the switch is closed, (change direction).
Motorbutton: start, stop

I have the code with the library in the attachment, but somehow the end buttons don't work (when I switch them to GND)

Bounce2 Library: GitHub - thomasfredericks/Bounce2: Debouncing library for Arduino and Wiring

Can I ask for the correct code if the error is somewhere in the code?

Somehow I can't get the codes right

Thank you very much

code:

// Detect the falling edge

// Include the Bounce2 library found here :
// GitHub - thomasfredericks/Bounce2: Debouncing library for Arduino and Wiring

#include <Bounce2.h>

const int motorbutton = 7;
const int switchp = 5;
const int switchc = 3;

// Instantiate a Bounce object :
Bounce debouncer = Bounce();

void setup() {

Serial.begin(9600);

// Setup the button with an internal pull-up :
pinMode(motorbutton,INPUT_PULLUP);

// After setting up the button, setup the Bounce instance :
debouncer.attach(motorbutton);
debouncer.interval(500);

pinMode(10, OUTPUT);
pinMode(11, OUTPUT);
}

void loop() {

// Update the Bounce instance :
debouncer.update();

// Call code if Bounce fell (transition from HIGH to LOW) :
if ( debouncer.fell() ) {
Serial.println("Motor Button just pressed");

if (digitalRead(switchp)) //if this is not the intended direction for 'swichp' change to 'switchc'
motorclockwise();
else
motoranticlockwise();
delay(100);
}

//stop motor is reached limit
if(digitalRead(switchp)|digitalRead(switchc)){
digitalWrite(10, LOW);
digitalWrite(11, LOW);
}
}

void motorclockwise() {
digitalWrite(10, HIGH);
digitalWrite(11, LOW);
}

void motoranticlockwise() {
digitalWrite(10, LOW);
digitalWrite(11, HIGH);
}

Split from an old topic

Hi,
Welcome to the forum.

Please read the post at the start of any forum , entitled "How to use this Forum".
OR
http://forum.arduino.cc/index.php/topic,148850.0.html.
Then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Thanks.. Tom.. :slight_smile:

Are you lacking pullups? If so you could add this to setup():

  pinMode (switchp, INPUT_PULLUP) ;
  pinMode (switchc, INPUT_PULLUP) ;