Today at 05:19 pm
Hello the Arduino community!
I am very new to arduino and coding.(Please help me! ) 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);
}
}
}
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.