4 Servo programming with 2 buttons

So I am trying to create a code where I have two buttons which controls 2 servo buttons each, with a total of 4 servo's controlled by 2 buttons. One of the two servo's needs to turn 0-180 degrees and back with one click with the button as well as the other two servo's which however needs to turn 180-0 degrees and back continuously with the other button.

I am having trouble coding this and I need help, I keep trying but the code ends up not working when I test it out.

If you know Adurino pretty well can you tell me what's wrong with my code or what I need to add

An example of your code would be great!!!

Thank you very much my code is below

#include <Servo.h>

Servo myservo; // create servo object to control a servo
Servo myservo2; // create 2nd servo object to control a 2nd servo
Servo myservo3; // create 3rd servo object to control a 3rd servo
Servo myservo4; //create 4rd servo object to control a 3rd servo
// this constant won't change:
const int buttonPin = 2; // the pin that the pushbutton is attached to
const int Buttonpin = 3; // the pin that the 2nd pushbutton is attached to
int buttonState = 0; // current state of the button
void setup()
{
{
// attach the Servo to pins
myservo.attach(9);
myservo2.attach(10);
// tell servo to go to position in variable 'pos'
myservo.write(0);
myservo2.write(0);

delay(100);
// initialize the button pin as a input:
pinMode(buttonPin, INPUT);
}
{
//attach the Servo to pins
myservo3.attach(11);
myservo4.attach(12);
//tell servo to go to position in variable 'pos'
myservo3.write(180);
myservo4.write(180);

delay(100);
//initialize the button pin as a input:
pinMode(Buttonpin, INPUT);
}

}
void loop() {
{
buttonState = digitalRead(buttonPin);
if (buttonState == HIGH) {
myservo.write(180);
myservo2.write(0);
}
else {
myservo.write(0);
myservo2.write(0);
}
}
{
buttonState = digitalRead(Buttonpin);
if (buttonState == HIGH) {
myservo3.write(0);
myservo4.write(0);
}
else {
myservo3.write(180);
myservo4.write(180);
}
}
}

Please correct your post above and add code tags around your code:
[code]`` [color=blue]// your code is here[/color] ``[/code].

It should look like this:// your code is here
(Also press ctrl-T (PC) or cmd-T (Mac) in the IDE before copying to indent your code properly)

========

What’s going on with all the {}?

This is poor practice to use the same name with just capitalization difference to name two things...

const int  buttonPin = 2;    // the pin that the pushbutton is attached to
const int  Buttonpin = 3;   // the pin that the 2nd pushbutton is attached to

To the true questions:

  • how have you wired the buttons
  • how do you power the servos
  • what do you think the last if drives as behavior? wish this matching your goal?
#include <Servo.h>

Servo myservo;  // create servo object to control a servo
Servo myservo2;  // create 2nd servo object to control a 2nd servo
Servo myservo3;  // create 3rd servo object to control a 3rd servo
Servo myservo4; //create 4rd servo object to control a 3rd servo
// this constant won't change:
const int  Button100 = 2;    // the pin that the pushbutton is attached to
const int  Button200 = 3;   // the pin that the 2nd pushbutton is attached to
int buttonState = 0;         // current state of the button
void setup()
{
  {
    // attach the Servo to pins
    myservo.attach(9);
    myservo2.attach(10);
    // tell servo to go to position in variable 'pos'
    myservo.write(0);
    myservo2.write(0);

    delay(100);
    // initialize the button pin as a input:
    pinMode(Button100, INPUT);
  }
  {
    //attach the Servo to pins
    myservo3.attach(11);
    myservo4.attach(12);
    //tell servo to go to position in variable 'pos'
    myservo3.write(180);
    myservo4.write(180);

    delay(100);
    //initialize the button pin as a input:
    pinMode(Button200, INPUT);
  }

}
void loop() {
  {
    buttonState = digitalRead(Button100);
    if (buttonState == HIGH) {
      myservo.write(180);
      myservo2.write(0);
    }
    else {
      myservo.write(0);
      myservo2.write(0);
    }
  }
  {
    buttonState = digitalRead(Button200);
    if (buttonState == HIGH) {
      myservo3.write(0);
      myservo4.write(0);
    }
    else {
      myservo3.write(180);
      myservo4.write(180);
    }
  }
}

Well with the {} I honestly don't know I am pretty new so I am bound to make mistakes like that and I changed up the names thank you for the advice!! and I power my servo's with a 9 volt battery and I will send you a link to this youtube video which was was I used to set up the wires but the video won't show the full wiring since I added on 9 wires each for the 3 additional servo's you will see what I mean when you see the video.

My goal is for one button to control the first 2 servo motors and start from 0 degrees and turn to 180 degrees and back continuously with click of the button as well as the other 2 servo motors with the other button but this time starting at 180 degrees turning to 0 degrees and back continuously with one click.

What do you call a click ? Push down and hold or push and release ?

Do you have momentary buttons ? How are they wired ?

Hi,
Welcome to the forum.

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

Thanks... Tom.. :slight_smile:

Hi,
Can I suggest you dump your code...

And start coding in stages.
First get one servo to sweep back and forth.
Then get it to sweep when you push a button.

When you have got the code doing what you need with one button, then write code to include the other servo.
When you have got the code doing what you need then add the other button and anti phase servo.

By this time you will have learnt how to structure your code and use { } in the way the were meant.

I know it sounds long process, BUT you will not end up like you are now, trying to debug a code that is supposed to do many different things, and non of them functioning.

Writing in stages and debugging as you go, will help you learn, and provide good structured code so we can read it and help you.

Tom... :slight_smile:

What I mean by click is pushing down the button once with a push button. The push buttons are momentary buttons as well. The wires setup is in this video I am not sure how I can screenshot on a pc that's why I don't have an attachment added on and I am currently not at home so I am limited to the time that I can use the school computer.

^
^
This is the wire setup I used

And tom your idea is wonderful but as much as I would like to take my time on this, I don't have the time to do so. If you guys have experience and know how to program 2 servo's each controlled by a button that turn's 0-180 and back continuously and another 2 that turns 180-0 degrees and back continuously it would be much appreciated!!

Joeycode:
And tom your idea is wonderful but as much as I would like to take my time on this, I don't have the time to do so. If you guys have experience and know how to program 2 servo's each controlled by a button that turn's 0-180 and back continuously and another 2 that turns 180-0 degrees and back continuously it would be much appreciated!!

hum... this is your project... if you don't have time to do it correctly why do you think we have time to do it for you... ?

Joeycode:
And tom your idea is wonderful but as much as I would like to take my time on this, I don't have the time to do so. If you guys have experience and know how to program 2 servo's each controlled by a button that turn's 0-180 and back continuously and another 2 that turns 180-0 degrees and back continuously it would be much appreciated!!

How much? I accept PayPal. But then this should be posted in Gigs and Collaborations, where people who do not have time or knowledge to do their own work pay others to do that work.

Edit: Sarcasm aside, this is a really simple task. Have you tried following any of the suggestions?
Follow Tom's advice and start with servo sweep.
If you are stuck, we will help.
But YOU have to try.

Actually my bad you guys are right this is my project, I will try Tom's method and let you guys know over my progress, thank you very much for trying to help me out!!

Awesome!
And it is worth saying again. If you are stuck, we will help.

Great attitude - will keep helping !

#include <Servo.h>

Servo myservo;
Servo myservo2;
Servo myservo3;

const int Button100 = 2;   // the number of the pushbutton pin
const int Button200 = 3;   // the number of the pushbutton pin
int pos = 0;

// variables will change:
int buttonState = 0;         // variable for reading the pushbutton status


void setup() {
  myservo.attach(9);
  myservo.attach(10);
  myservo.attach(11);
  pinMode(Button100, INPUT);
  pinMode(Button200. INPUT);
}


void loop() {
  buttonState = digitalRead(Button100);
  myservo.write(pos);
  myservo3.write(pos);
}
if (buttonState == HIGH) {
  for (pos = 0; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees
    myservo.write(pos);
    myservo3.write(pos);              
    delay(15);                      
  }
} else {
  for (pos = 180; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
    myservo.write(pos);
    myservo3.write(pos);              
    delay(15);
  }
}

{
  buttonState = digitalRead(Button200);
  myservo2.write(pos);
}
if (buttonState == HIGH) {
  for (pos = 180; pos >= 0; pos -= 1)
 { 
    myservo2.write(pos);              
    delay(15);                      
  }
} else {
     for (pos = 0; pos <= 180; pos += 1){ 
    myservo2.write(pos);             
    delay(15);
  }
}

Alright so, so far this is what I have and I just wanna know by your opinion if I am on the right path.

So for the code and my goal of it has slightly changed this time with me using 3 motors but one 2 turns 0-180 and back continuously with the push button with only one click and another motor controlled by another button that starts from 180 and turns to 0 and back continuously as well.

If you can, critique me over my code and tell me what I should add, delete and so on to make this code at it's best.

#include <Servo.h>

Servo myservo;
Servo myservo2;
Servo myservo3;

const int Button100 = 2;   // the number of the pushbutton pin
const int Button200 = 3;   // the number of the pushbutton pin
int pos = 0;

// variables will change:
int buttonState = 0;         // variable for reading the pushbutton status


void setup() {
  myservo.attach(9);
  myservo.attach(10);
  myservo.attach(11);
  pinMode(Button100, INPUT);
  pinMode(Button200, INPUT);
}


void loop() {
  buttonState = digitalRead(Button100);
  myservo.write(pos);
  myservo3.write(pos);

  if (buttonState == HIGH) {
  for (pos = 0; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees
    myservo.write(pos);
    myservo3.write(pos);
    delay(15);
  }
} else {
  for (pos = 180; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
    myservo.write(pos);
    myservo3.write(pos);
    delay(15);
  }
}

{
  buttonState = digitalRead(Button200);
  myservo2.write(pos);
}
if (buttonState == HIGH) {
  for (pos = 180; pos >= 0; pos -= 1)
 { 
    myservo2.write(pos);
    delay(15);
  }
} else {
     for (pos = 0; pos <= 180; pos += 1){ 
    myservo2.write(pos);
    delay(15);
  }
 }
}
#include <Servo.h>

Servo myservo;
Servo myservo2;
Servo myservo3;

const int Button100 = 2;   // the number of the pushbutton pin
const int Button200 = 3;   // the number of the pushbutton pin
int pos = 0;

// variables will change:
int buttonState = 0;         // variable for reading the pushbutton status


void setup() {
  myservo.attach(9);
  myservo.attach(10);
  myservo.attach(11);
  pinMode(Button100, INPUT);
  pinMode(Button200, INPUT);
}


void loop() {
  buttonState = digitalRead(Button100);
  myservo.write(0);
  myservo3.write(0);

  if (buttonState == HIGH) {
    for (pos = 0; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees
      myservo.write(pos);
      myservo3.write(pos);
      delay(100);
    }
  } else {
    for (pos = 180; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
      myservo.write(pos);
      myservo3.write(pos);
      delay(100);
    }
  }

  {
    buttonState = digitalRead(Button200);
    myservo2.write(180);
  }
  if (buttonState == HIGH) {
    for (pos = 180; pos >= 0; pos -= 1)
    {
      myservo2.write(pos);
      delay(100);
    }
  } else {
    for (pos = 0; pos <= 180; pos += 1) {
      myservo2.write(pos);
      delay(100);
    }
  }
}

So after auto-formatting this and testing this code out it appears what I expected to do didn't go to plan when I pressed the buttons the servo motors would just shake and sometimes the button would control 2 and for another second it wouldn't, I don't think there is anything wrong with my set up but just the code itself

Just in case you are curious this is my wire setup but more modified with me using a 9 volt battery and me connecting 6 more wires with the 2 extra servo motors which is plugged in right which I can fully guarantee.

image.jpg

OHH I see but even if I am using the wrong battery I think there is something wrong with the code as well.

I have a feeling where everything went wrong was with the for (pos = 0; pos <= 180; pos += 1) codes do you guys think so as well or do your opinions differ?

And what does that code do exactly like I know it makes it go 0-180 but do you have a more detail explanation of what it really does and it's effect

So alright I see, I been using the wrong code, with the one I mentioned earlier, that code just goes in a loop by itself and that's not what I am trying to do but instead I am trying to control them by buttons, one button controlling 2 servo's and another controlling 1 servo, here is a video of what it is doing.

What other servo code can I use that can be controlled by the push buttons?