CONTROLLING SERVO WITH BUTTONS

Hi. I know this topic could have gone under motors but it mainly has to do with the programming of it. What I am trying to do is control my servo, or myservo, with two buttons, greenButton and redButton, which turn it on and off. So, I push the green button and the servo arm sweeps back and forth, but, when I click the red button, it stops. I could do this as much as possible… Below is my code. Thank you!!!

#include <Servo.h>
Servo myservo;
int pos = 0;
int greenButton = 3;
int redButton = 4;
void setup() {
  myservo.attach(2);
  pinMode(greenButton, INPUT_PULLUP);
  pinMode(redButton, INPUT_PULLUP);
}
void startServo() {
  for (pos = 0; pos <= 180; pos += 1) { 
    // in steps of 1 degree
    myservo.write(pos);              
    delay(15);                       
  }
  for (pos = 180; pos >= 0; pos -= 1) { 
    myservo.write(pos);              
    delay(15);                       
  }
}
void stopServo() {
  
}
void loop() {
  if (digitalRead(greenButton) == HIGH) {
    startServo();
  }
  else if (digitalRead(redButton) == HIGH) {
    stopServo();
  }
  else {
    //do nothing
  }
}

Thank you! :slight_smile: :slight_smile: :slight_smile:
Ethan

which turn it on and off.

Servos don't get turned on or off. They get told to go to a new position or a new speed.

So, I push the green button and the servo arm sweeps back and forth, but, when I click the red button, it stops.

Is that what you want? If so, what is the problem?

If not, what do you want?

You are welcome! :slight_smile: :slight_smile: :slight_smile:

Did you have a question?

Your code does something. We can guess what it does. You want the code to do something. You tried to explain but were incomplete so we are only guessing. Do you want us to continue guessing?

By the way, your stopServo() function does nothing.

Furthermore, your startServo() function will run for about 5.4 seconds once the green button gets pushed, regardless of what the red button does. Perhaps you meant to check the red button at every myservo.write(pos) in startServo() ?

A wiring diagram would be helpful as well.

Ohh. Sorry for the miscommunication.

PaulS:
Servos don't get turned on or off. They get told to go to a new position or a new speed.
Is that what you want? If so, what is the problem?

If not, what do you want?

That is what I want to happen but I cannot figure out how. Thank you. I know I said some stuff wrong but what i mean is that once I click greenButton, the arm sweeps back and forth, 180 degrees to 0 degrees again and again, until redButton is pressed and the motion stops. I thought having nothing in stopServo() would override startServo(), but I guess not. Thx for help.

Ethan :slight_smile:

Have you gotten the servo sweep code to work? You might modify that code to use millis instead of the delay so c change in a button status can be checked while the servo is being moved.

Yes. I have gotten sweep t work so I will try to incorporate that in. Thx.
Ethan :slight_smile: