Use a pushbutton to control two servo motors

I would like to use a pushbutton to make two servos rotate.

~ Ideally after pressing the push button once, the two Arduinos would rotate 180 degrees from their starting point (Servo 1 rotating in one direction and Servo 2 rotating in the opposite direction), and then both servos would stop once that rotation is complete.

~ And then after pressing the push button once more, the servos would rotate (in opposing directions from one another) back to their original position.

This code below successfully prompts two servos to rotate in opposite directions, however after hours of attempting to integrate a pushbutton into the code, I couldn’t get the code to successfully compile or upload to my Arduino Uno. I would like to modify the code so that it allows me to control the servos with the push button, in the way I mentioned above.

#include <Servo.h>

Servo servo1;
Servo servo2;

int i = 0;

void setup() {
servo1.attach(3);
servo2.attach(9);
}

void loop() {
for (i = 0; i < 180; i++) {
servo1.write(i);
servo2.write(180 - i);
delay(10);
}
for (i = 180; i > 0; i–) {
servo1.write(i);
servo2.write(180 - i);
delay(10);

}

}

The code you ahve posted has not a single line of code that would show how you tried to attempt using a push-button.

So please post your attempt what you have tried to integrate a push-button.

You should post code by using code-tags
There is an automatic function for doing this in the Arduino-IDE
just three steps

  1. press Ctrl-T for autoformatting your code
  2. do a rightclick with the mouse and choose "copy for forum"
  3. paste clipboard into write-window of a posting

best regards Stefan

Re-write you code as tasks
Multi-tasking in Arduino
remove delays
How to write Timers and Delays in Arduino
and debounce your buttons
Debouncing Switches in Arduino

You could try it like this…


#include <Servo.h>
#include <mechButton.h>

#define SERVO1_PIN   3
#define SERVO2_PIN   9
#define BTN_PIN      2

Servo       servo1;              // A servo object.
Servo       servo2;              // The other servo object.
mechButton  ourButton(BTN_PIN);  // A denbounced button object.
bool        pointOut;            // A boolean for saving servo state.


void setup(void) {

   servo1.attach(SERVO1_PIN);          // Standard servo attach.
   servo2.attach(SERVO2_PIN);          // Attach the other one.
   ourButton.setCallback(btnClicked);  // Set the callback for the button.
   pointOut = true;                    // We'll default the servo to false.
   servo1.write(180);                  // And send them to that state.
   servo2.write(0);                    // This one as well.
}

// When the button changes state, it calls this.
void btnClicked(void) {

   if (!ourButton.trueFalse()) { // If the button is false (grounded)..
      if (pointOut) {            // If pointOut is false..
         servo1.write(0);        // Servo one moves to 0 deg.
         servo2.write(180);      // Servo two moves to 180 deg.
      } else {                   // else, pointOut is true..
         servo1.write(180);      // Servo one moves to 180 deg.
         servo2.write(0);        // Servo two moves to 0 deg.
      }
      pointOut = !pointOut;      // Flip our boolean for the next press.
   }
}


void loop(void) {

   idle();     // Idle runs stuff in the background, like the button code.
}

You will need to install LC_baseTools to get this to compile. (Tools menu ==>> Manage Lirbraries…) Search for LC_baseTools.

Good luck!

-jim lee

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.