Controlling 4 Continuous Servo Motors Using Multiple Push Buttons

I'm trying to design a car that follows a specific sequence based on the push button that is activated. For instance, when push button #1 is activated, sequence #1 will begin and end. I want the sequence to follow the steps: no motion in any servo motors, push button x is activated, sequence x begins and ends, no motion in any servo motors again. However, my servo motors just end up following all sequences on an infinite loop, even when I am not activating any of my push buttons. I have pasted my code down below, any help would be great!

#include <Servo.h>

Servo leftservo1;
Servo rightservo1;
Servo leftservo2;
Servo rightservo2;

int button1 = 9;
int button2 = 10;
int button3 = 11;

int button1State = LOW;
int button2State = LOW;
int button3State = LOW;

void setup() {
leftservo1.attach(2);
rightservo1.attach(3);
leftservo2.attach(4);
rightservo2.attach(5);

pinMode(button1, INPUT);
pinMode(button2, INPUT);
pinMode(button3, INPUT);

}

void loop() {
button1State = digitalRead(button1); // check if button 1 is pressed
button2State = digitalRead(button2); // check if button 2 is pressed
button3State = digitalRead(button3); // check if button 3 is pressed

if(button1State == HIGH){
drawing1();
delay(2000);
} else if(button2State == HIGH){
drawing2();
delay(2000);
} else if(button3State == HIGH){
drawing3();
delay(2000);
} else{
leftservo1.write(90);
leftservo2.write(90);
rightservo1.write(90);
rightservo2.write(90);
}
}

void drawing1(){
leftservo1.write(180);
leftservo2.write(180);
delay(1000);
leftservo1.write(90);
leftservo2.write(90);
}

void drawing2(){
rightservo1.write(180);
rightservo2.write(180);
delay(1000);
rightservo1.write(90);
rightservo2.write(90);

}
void drawing3(){
leftservo1.write(180);
leftservo2.write(180);
rightservo1.write(180);
rightservo2.write(180);
delay(1000);
leftservo1.write(90);
leftservo2.write(90);
rightservo1.write(90);
rightservo2.write(90);
}

How are the buttons wired? Is there a pulldown resistor connected to the button's input? Are the servos powered by an external supply? Post a schematic of the wiring.

The most common way to wire button switches is to wire one side to ground and the other to an input set to pinMode INPUT_PULLUP. The switch will read HIGH when not pressed and LOW when pressed. Adjust your code accordingly.

Read the how to use this forum-please read sticky to see how to properly post code. Remove useless white space and format the code with the IDE autoformat tool (crtl-t or Tools, Auto Format) before posting code.

Yes, I have each button connected to a 220 resistor which is connected to its individual pin. Then on the other side, I connected all three buttons to the 3.3 V pin.

Yes, I have each button connected to a 220 resistor which is connected to its individual pin. Then on the other side, I connected all three buttons to the 3.3 V pin.

That is a bit ambiguous. Can you draw a schematic?

instructions for posting images
How to post an image.
Another page on posting images.

How to wire switches using internal pullup. 220 Ohms is pretty low value for a pulldown (if that is how it is wired). 10K is usually fine unless there is a noise problem, then a lower value may be necessary. But usually never less than about 1K.

groundFungus:
How to wire switches using internal pullup

Actually, you are showing external pullup...

groundFungus:
That is a bit ambiguous. Can you draw a schematic?

instructions for posting images
How to post an image.
Another page on posting images.

Actually, you are showing external pullup...

R1 optional if internal pullup enabled.

Here is a different (better?) one.

Switches not wired right for pulldown or pullup. Pulldown resistors would go from the input to ground.

dig sw button pulldown.jpg

The switches should be wired to 5V. The servos will need an external power supply. An Uno 5V regulator can power maybe 1 unloaded servo. Once you try to move more than one loaded servos you are likely to have trouble. A 4 AA cell pack is a good supply for servos.

dig sw button pulldown.jpg

groundFungus:
R1 optional if internal pullup enabled.

Here is a different (better?) one.

I changed the wire that connects to the 3.3V pin and I connected it to ground. Then, I changed my if statements to execute the functions when the button states are low. However, the servo motors still do not respond to the push buttons.

Did you also select INPUT_PULLUP option in pinMode(), as indicated in the diagram?

Please post a schematic of the new wiring and the modified code so we can keep up with what you have done.

This version of your code seems to work OK. Buttons wired with internal pullups. Serial prints inserted to monitor program state.

#include <Servo.h>

Servo leftservo1;
Servo rightservo1;
Servo leftservo2;
Servo rightservo2;

int button1 = 9;
int button2 = 10;
int button3 = 11;

int button1State = HIGH;
int button2State = HIGH;
int button3State = HIGH;

void setup()
{
   Serial.begin(115200);
   Serial.println("here we go");
   leftservo1.attach(2);
   rightservo1.attach(3);
   leftservo2.attach(4);
   rightservo2.attach(5);

   pinMode(button1, INPUT_PULLUP);
   pinMode(button2, INPUT_PULLUP);
   pinMode(button3, INPUT_PULLUP);

}

void loop()
{
   button1State = digitalRead(button1);  // check if button 1 is pressed
   button2State = digitalRead(button2);  // check if button 2 is pressed
   button3State = digitalRead(button3);  // check if button 3 is pressed

   if (button1State == LOW)
   {
      drawing1();
      delay(2000);
   }
   else if (button2State == LOW)
   {
      drawing2();
      delay(2000);
   }
   else if (button3State == LOW)
   {
      drawing3();
      delay(2000);
   }
   else
   {
      leftservo1.write(90);
      leftservo2.write(90);
      rightservo1.write(90);
      rightservo2.write(90);
   }
}

void drawing1()
{
   Serial.println("drawing 1");
   leftservo1.write(180);
   leftservo2.write(180);
   delay(1000);
   leftservo1.write(90);
   leftservo2.write(90);
}

void drawing2()
{
   Serial.println("drawing 2");
   rightservo1.write(180);
   rightservo2.write(180);
   delay(1000);
   rightservo1.write(90);
   rightservo2.write(90);

}
void drawing3()
{
   Serial.println("drawing 3");
   leftservo1.write(180);
   leftservo2.write(180);
   rightservo1.write(180);
   rightservo2.write(180);
   delay(1000);
   leftservo1.write(90);
   leftservo2.write(90);
   rightservo1.write(90);
   rightservo2.write(90);
}
#include <Servo.h>

Servo leftservo1;
Servo rightservo1;
Servo leftservo2;
Servo rightservo2;

int button1 = 9;
int button2 = 10;
int button3 = 11;

int button1State = HIGH;
int button2State = HIGH;
int button3State = HIGH;


void setup() {
  leftservo1.attach(2);
  rightservo1.attach(3);
  leftservo2.attach(4);
  rightservo2.attach(5);
  
  pinMode(button1, INPUT);
  pinMode(button2, INPUT);
  pinMode(button3, INPUT);

  leftservo1.write(95);
  leftservo2.write(90); 
  rightservo1.write(90);
  rightservo2.write(90); 

}


void loop() {
  button1State = digitalRead(button1);  // check if button 1 is pressed
  button2State = digitalRead(button2);  // check if button 2 is pressed
  button3State = digitalRead(button3);  // check if button 3 is pressed

  if(button1State == LOW){
    drawing1();
    delay(2000);
  } else if(button2State == LOW){
    drawing2();
    delay(2000);
  } else if(button3State == LOW){
    drawing3();
    delay(2000);
  } else{
    leftservo1.write(95);
    leftservo2.write(90);
    rightservo1.write(90);
    rightservo2.write(90);
  }
}


void drawing1(){
  leftservo1.write(180);
  leftservo2.write(180);
  delay(1000);
  leftservo1.write(95);
  leftservo2.write(90);
}

void drawing2(){
  rightservo1.write(180);
  rightservo2.write(180);
  delay(1000);
  rightservo1.write(90);
  rightservo2.write(90);

}
void drawing3(){
  leftservo1.write(180);
  leftservo2.write(180);
  rightservo1.write(180);
  rightservo2.write(180);
  delay(1000);
  leftservo1.write(95);
  leftservo2.write(90);
  rightservo1.write(90);
  rightservo2.write(90);
}

groundFungus:
Please post a schematic of the new wiring and the modified code so we can keep up with what you have done.

Your resistors are in series with the inputs. That will leave them "floating" when the switches are not pressed.


Posting images
How to post an image.
Another page on posting images.

And if the switches are really in the breadboard like that they are shorted anyway.

Fritz with wiring for tact pushbutton wired for internal pullup.
fritz switch.jpg

fritz switch.jpg

I put the resistors in parallel in Tinkercad and it worked then, but it still doesn't work in my actual project even though I checked that the wiring was the same. Do you have any idea why that would be the case?

aarg:
Your resistors are in series with the inputs. That will leave them "floating" when the switches are not pressed.

If you are using internal pullups the external resistors are not used. See my last post with the fritz.

Can you post photos of your project so we can see how it is actually wired?

I tried wiring brand new buttons like this just in case I accidentally shorted the old ones, but the servo motors still do not respond to the buttons. I'll put a picture of it down below, it's kind of messy tho

groundFungus:
Fritz with wiring for tact pushbutton wired for internal pullup.
fritz switch.jpg