How to wire 2 servo motors and 1 push button, beginner

Hello everyone! I apologize in advance if I am breaking any forum rules or standards. I am a complete beginner. I am reaching out on behalf of my group. We have been assigned a robotics task in school, and we have zero background in robotics.

One circuit we have intends to move the motors 180 degrees when the button is pressed. Then, it should return to 0 degrees when the button is pressed again. The code works perfectly fine in Tinkercad, and it works okay in real life. However, the button appears to send inconsistent bursts of information to the Arduino Uno microcontroller. The motors sometimes move twice after just one button press.

From what I've seen on the forums so far, its something about Arduino not being able to check the state changes perfectly. The code we use has INPUT_PULLUP to help with that, but the real life circuit still has issues. I vaguely remember another forum post saying that a capacitor can help, but we do not have a capacitor. If there is any code that can somehow simulate how a capacitor works, the same way INPUT_PULLUP can simulate a pullup resistor, we would appreciate it if it can be shared with us.

Our real life circuit is using a 9 volts battery. I'm not sure if that is important information. If there is any other information needed, I will be glad to supply it. Any help is appreciated. I can also send the code we use, and full transparency, we took that code from a website and just slightly repurposed it. Please send any resources or links that may help. Thank you so much. Our project hinges on you all. The deadline of the project is November 13. We are sorry. We love you. From the Philippines.

Seems your button is bouncing.

I’d suggest you check the code for or use one of the numerous button library such as Button in easyRun or OneButton or Toggle or EasyButton or Bounce2, ...

1. Try first the advice of post #2 @J-M-L.

2. Post the picture/type of your motor you are using. Is it a Servo Motor (SG90) or a Stepper Motor?

3. Post your complete code.

4. Post connection diagram of your motor and buton.

5. Post te link of the we site from where you have taken the project.

Is it a PP3 battery by any chance ?
If so then it is only intended to power low current devices such as smoke detectors. Have you measured its voltage whilst powering your project ?

Please post your full sketch, using code tags when you do

Hello! Thank you for your response. I will be sure to try those button libraries. I will update this thread once I find something that works (or if I have any other questions...)

Hello! Thank you for your response.

We are using 2 SG90 Servo Motors.

/*
 * Created by ArduinoGetStarted.com
 *
 * This example code is in the public domain
 *
 * Tutorial page: https://arduinogetstarted.com/tutorials/arduino-button-servo-motor
 */

#include <Servo.h>

// constants won't change
const int BUTTON_PIN = 2; // Arduino pin connected to button's pin
const int SERVO_PIN1  = 9; // Arduino pin connected to servo motor's pin
const int SERVO_PIN2 = 10;

Servo servo1; // create servo object to control a servo
Servo servo2;

// variables will change:
int angle = 0;          // the current angle of servo motor
int lastButtonState;    // the previous state of button
int currentButtonState; // the current state of button

void setup() {
  Serial.begin(9600);                // initialize serial
  pinMode(BUTTON_PIN, INPUT_PULLUP); // set arduino pin to input pull-up mode
  servo1.attach(SERVO_PIN1);           // attaches the servo on pin 9 to the servo object
  servo2.attach(SERVO_PIN2);
  
  servo1.write(0);
  servo2.write(0);
  currentButtonState = digitalRead(BUTTON_PIN);
}

void loop() {
  lastButtonState    = currentButtonState;      // save the last state
  currentButtonState = digitalRead(BUTTON_PIN); // read new state

  if(lastButtonState == HIGH && currentButtonState == LOW) {

    // change angle of servo motor
    if(angle == 0)
      angle = 90;
    else
    if(angle == 90)
      angle = 0;

    // control servo motor arccoding to the angle
    servo1.write(angle);
    servo2.write(angle);
  }
}

We found the code here: Arduino - Button - Servo Motor | Arduino Tutorial
I simply modified it slightly to accomodate 2 motors.


Here is how our circuit is laid out.

Hello! Thank you for your response.

Yes, it is a PP3 battery (Eveready 9V battery). The battery is attached like this:

We have not yet measured the voltage of our project. We will try to do that.

Code and appearance of the circuit has been sent in my previous reply.

Try adding a small delay, it may fix the problem

Add delay(50); right after servo2.write(angle)

Tank you for the response.

The schematic shows that the button pin (2) and motor pin (10) are shorted together. Please, isolate it and run your sketch again and report thr result.

But your picture diagram is correct.
So how is it actually wired?

You should expect possibly some high current draw from the 2 SG90 Servo Motors... under load they could go to 500mA each...

Your 9V Duracell battery is good for smoke detectors and can't provide high current for a long time.

@ricookie

Your sketch of post #6 works well (with occasional jittering) for me, but I have connected one Servo at a time as I am supplying test supply from UNO.

Do you hhave any programming experience to understand the working principles of the code of post #6?

Thank you for your input! I'd like to ask what is the recommended power source for 2 SG90 Servo Motors, and if powerbanks are applicable?

Thank you for the response! it is wired like this:


Why is the resistor over there?

I followed a servo motor and pushbutton connection online. Should I remove it? I have tried removing it already in place of a jumperwire instead, but it retains the same problem, wherein jittering occurs to one of the motors, while as the other ceases completely when connected to a 9V Battery. It works perfectly fine when connected to a laptop or PC.

How much mAh capacity of the Duracell 9V battery you are usng?

Does your eventual robotics design need to run from a battery?

I am using an Eveready 9V battery and the mAh is 175.

The photographs are not too useful. Your post # 6 is a copy of your instructions. Trustworthy wiring diagrams are drawn by hand while CAREFULL verifying where each end of each wire is conected. Post a picture of that. Meanwhile you should have picked up on the fact one 9VDC battery can not work, either get a wal wart or a big powerbank or a bench supply.