0

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.

Code:

#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
  pinMode(pushButtonPin,INPUT_PULLUP);
   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 – SABLE MOTION Apr 26 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 at 19:33
  • ok how about now – SABLE MOTION Apr 26 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 at 19:39
  • sorry, but I don't really know how to do that. :( – SABLE MOTION Apr 26 at 19:45
-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 :

https://dduino.blogspot.com/2012/03/arduino-button-debouncing.html

enter image description here

| improve this answer | |
  • 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 at 19:38
  • An if statement works with integers! – Sim Son May 1 at 19:46
  • Mea culpa you are right : arduino.cc/reference/en/language/variables/constants/constants – skourkos May 1 at 20:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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