How do you program two pushbuttons to control the servo motor?

Today at 05:19 pm
Hello the Arduino community!
I am very new to arduino and coding.(Please help me! :frowning: ) My project consists of using two buttons to control the servo motor. At the moment, I have the first button to move the servo counter-clockwise constantly, then if I press the button again, it changes the servo direction to clockwise while moving constantly.
My problem is programming the second button in order for it to stop the servo motor. Please instruct me in my nooby programming mistakes as I try to randomly write code for the 2nd button T.T!

#include <Servo.h>

const int buttonPin = 2;
const int buttonPin2 = 4;
int buttonState = 0;
int buttonState2 = 0;
int directionState = 0;

Servo servoA;
int position = 0;

void setup() {
servoA.attach(9);  // attaches the servo on pin 9 to the servo object
pinMode(buttonPin, INPUT);
pinMode(buttonPin2, INPUT);
}

void loop() {

buttonState = digitalRead(buttonPin);
buttonState2 = digitalRead(buttonPin2);
if (directionState == 0){
 if (buttonState == HIGH) {
   directionState = 1;
   for (position = 0; position < 180; position=position+1)
   {
     servoA.write(position);
     delay(5);
   }
 }
} else if (directionState == 1) {
 if (buttonState == HIGH) {
   directionState = 0;
   for (position = 180; position>=1; position=position-1)
   {
     servoA.write(position);
     delay(5);
   }
 }
if (buttonState2 == HIGH) {
 // Servo off
 servoA.write(position);
}
}
}

test.ino (1.11 KB)

Please instruct me in my nooby programming mistakes

Instruction 1
read this before posting a programming question

Instruction 2
Copy the code into the IDE then press Ctrl/T to format it

Instruction 3
Paste the formatted code into a post here, select it and click the code tags icon above the editor

Details of Instructions 2 and 3 can be found in the link in Instruction 1

By the way

buttonState = digitalRead(buttonPin);
buttonState2 = digitalRead(buttonPin);

Why are you reading the same input twice ?

#include <Servo.h>

const int buttonPin = 2;
const int buttonPin2 = 4;
int buttonState = 0;
int buttonState2 = 0;
int directionState = 0;

Servo servoA;
int position = 0;

void setup() {
servoA.attach(9);  // attaches the servo on pin 9 to the servo object
pinMode(buttonPin, INPUT);
pinMode(buttonPin2, INPUT);
}

void loop() {

buttonState = digitalRead(buttonPin);
buttonState2 = digitalRead(buttonPin2);
if (directionState == 0){
  if (buttonState == HIGH) {
    directionState = 1;
    for (position = 0; position < 180; position=position+1)
    {
      servoA.write(position);
      delay(5);
    }
  }
} else if (directionState == 1) {
  if (buttonState == HIGH) {
    directionState = 0;
    for (position = 180; position>=1; position=position-1)
    {
      servoA.write(position);
      delay(5);
    }
  }
if (buttonState2 == HIGH) {
  // Servo off
  servoA.write(position);
}
}
}

Better, thanks, but still not Auto Formatted.

Now what about

buttonState = digitalRead(buttonPin);
buttonState2 = digitalRead(buttonPin);

UKhelibob i fixed that part in my original message! Thanks I didnt notice that lol. I still cannot solve my programming problem on the 2nd push button to stop the servo motor or stop the loop.

How are the pushbuttons wired ?
Do you have pulldown resistors in place to keep the inputs in a known state ?

The pictures are in the attachments.

wires.jpg