I'm new to Arduino and I'm following this tutorial: https://www.youtube.com/watch?v=T2HMJiy9b_I but the problem is the servo just keeps moving back and forwards on it own and the button is doing nothing when pressed please help.


#include <Servo.h>

Servo myservo;  // create servo object to control a servo
#define servoPin 3 //~
#define pushButtonPin 2 

int angle =90;    // initial angle  for servo
int angleStep =5;
const int minAngle = 50;
const int maxAngle = 110;

int buttonPushed =0;

void setup() {
  // Servo button demo by Robojax.com
  Serial.begin(9600);          //  setup serial
  myservo.attach(servoPin);  // attaches the servo on pin 3 to the servo object
   Serial.println("Robojax Servo Button ");

void loop() {
  if(digitalRead(pushButtonPin) == LOW){
    buttonPushed = 1;
   if( buttonPushed ){
  // change the angle for next time through the loop:
  angle = angle + angleStep;

    // reverse the direction of the moving at the ends of the angle:
    if (angle <= minAngle || angle >= maxAngle) {
      angleStep = -angleStep;
       buttonPushed = 0;
    myservo.write(angle); // move the servo to desired angle
      Serial.print("Moved to: ");
      Serial.print(angle);   // print the angle
      Serial.println(" degree");    
  delay(100); // waits for the servo to get there

  • I posted the code:robojax.com/learn/arduino/…. I need help with the code so when the button is pressed the servo will rotate 0 to 180 degrees and stop but the button is unresponsive and the servo just keeps moving back and forth Apr 26, 2020 at 19:31
  • Edit your question. Select all of the code, and tap the {} code formatting button. Your code is unreadable as posted.
    – Duncan C
    Apr 26, 2020 at 19:33
  • ok how about now Apr 26, 2020 at 19:37
  • Once you push the button once and buttonPushed becomes 1 the servo will move forever because nothing ever sets buttonPushed back to 0. You need an else there to set it back to 0.
    – Delta_G
    Apr 26, 2020 at 19:39
  • sorry, but I don't really know how to do that. :( Apr 26, 2020 at 19:45

1 Answer 1


This condition below is never met because a boolean comparison is implied :

if( buttonPushed ){

and buttonPushed is declared as :

int buttonPushed =0;

A proper comparison should be OK :

if( buttonPushed == 1) 
   // Do something

Also the buttons need proper debouncing or the push event will have undetectable results :


enter image description here

  • 1
    C++ will treat any non-zero value as true and 0 as false. It is perfectly legal to use an int inside an if statement in this way. If you would like I can post demonstration code to prove it. It not great coding style, but it won't cause any failure.
    – Delta_G
    May 1, 2020 at 19:38
  • An if statement works with integers!
    – Sim Son
    May 1, 2020 at 19:46
  • Mea culpa you are right : arduino.cc/reference/en/language/variables/constants/constants
    – skourkos
    May 1, 2020 at 20:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.