0

I tried to write a simple program. Press button 1 servo goes to position 1, press button 2 servo goes to position 2.

But when I hook everything up my Servo goes full Crazy mode. It starts to jiggle back and forth like a madman. Even when I unhook the buttons.

What is the mistake I made? Any help is appreciated :D

Thanks in advance

/* Sweep
     by BARRAGAN <http://barraganstudio.com>
     This example code is in the public domain.

     modified 8 Nov 2013
     by Scott Fitzgerald
     http://www.arduino.cc/en/Tutorial/Sweep

    modified 5-12-2017
*/

#include <Servo.h>

Servo myservo;  // create servo object to control a servo

int buttonState = 0;         // variable for reading the pushbutton status
int buttonState2 = 0;        // variable for reading the pushbutton status
const int buttonPin = 4;     // the number of the pushbutton pin
const int buttonPin2 = 5;    // the number of the pushbutton pin

void setup() {
  myservo.attach(9);         // attaches the servo on pin 9 to the servo object
  pinMode(buttonPin, INPUT);
  pinMode(buttonPin2, INPUT);
}

void loop() {
  buttonState = digitalRead(buttonPin);
  buttonState2 = digitalRead(buttonPin2);
  if (buttonState == HIGH) {
    myservo.write(173);        // tell servo to go to position
    delay(25);                 // waits 25ms for the servo to reach the position
  }
  if (buttonState2 == HIGH) {
    myservo.write(97);         // tell servo to go to position
    delay(25);                 // waits 25ms for the servo to reach the position
  }
}    

photo

  • How did you connect the buttons to the Arduino? Schematic please. – Edgar Bonet Dec 5 '17 at 21:14
  • added a picture – Anton van der Wel Dec 5 '17 at 21:27
1

Please, connect the buttons to the ground and change the code:

#include <Servo.h>

Servo myservo;  // create servo object to control a servo

int buttonState = 0;         // variable for reading the pushbutton status
int buttonState2 = 0;        // variable for reading the pushbutton status
const int buttonPin = 4;     // the number of the pushbutton pin
const int buttonPin2 = 5;    // the number of the pushbutton pin

void setup() {
  myservo.attach(9);  // attaches the servo on pin 9 to the servo object
  pinMode(buttonPin, INPUT_PULLUP);
  pinMode(buttonPin2, INPUT_PULLUP);
}

void loop() {
  buttonState = digitalRead(buttonPin);
  buttonState2 = digitalRead(buttonPin2);
  if (buttonState == LOW) {
    myservo.write(173);              // tell servo to go to position
    delay(300);                       // waits 25ms for the servo to reach the position
  }
  if (buttonState2 == LOW) {
    myservo.write(97);              // tell servo to go to position
    delay(300);                       // waits 25ms for the servo to reach the position
  }
}

Pull-up resistors get a high input level when the buttons are open. Otherwise, electromagnetic interference may occur.

  • Very cool it stopped randomly moving!. Now it goes to the 2 positons. Yet one of those positions it keeps "buzzing" like some of the gears are still turning but I dont see any rotation as result. The second Position is Silent and very nice :D Do you know how this buzzing can be removed? – Anton van der Wel Dec 5 '17 at 21:41
  • Please try to increase the delay from 25 to 300. – AltAir Dec 5 '17 at 21:53

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.