button as toggle switch for a servo

Hello

I have trouble to get my Servo to move.
What I want to achieve is to toggle a servo between two States (0° and 180°) by pushing a single button.

button push → got to 0°, wait there till next button push → go to 180°, wait there till next…

See posted code below. And the attached jpg for wiring.
The serial output works fine(with “up” and “down”) but my servo is not doing anything at all.

If I use the Sweep sketch from the examples my servo moves like it should so I can say wiring and servo is fine.

I Just want to understand what I am doing wrong.
It seems like my servo.write() is not working?

//Button Toggle Servo

#include <Servo.h> 
Servo servo;  // create servo object to control a servo 
                // twelve servo objects can be created on most boards


const int buttonPin = 53;
boolean currentState = LOW;//stroage for current button state
boolean lastState = LOW;//storage for last button state

boolean pos = true;


void setup(){
  
  servo.attach(9);  // attaches the servo on pin 9 to the servo object
  pinMode(buttonPin, INPUT_PULLUP);//this time we will set the pin as INPUT
  Serial.begin(9600);//initialize Serial connection
  
  
}

void loop(){
  
  currentState = digitalRead(buttonPin);
  if (currentState == HIGH && lastState == LOW){//if button has just been pressed
    delay(10);//crude form of button debouncing
    
    //toggle the state of the Servo
    
    if (pos == true){
      servo.write(180);              // tell servo to go to "180" 
      delay(150);                       // waits 15ms for the servo to reach the position 

     
      Serial.println("up");          
      
            pos = false;

      
      
    } else {
      servo.write(0);              // tell servo to go to "0" 
      delay(150);                       // waits 15ms for the servo to reach the position 

      Serial.println("down");
      
            pos = true;

      
    }
  }
  
  lastState = currentState;
}

  if (currentState == HIGH && lastState == LOW){//if button has just been pressed

Whyareyoutryingtohidethecurlybrace? Put the thing on it's own line and loose the useless comment.

Put every curly brace on its own line, and use Tools + Auto Format to properly indent the code. Delete the

useless blank lines. Perhaps having done that, the problem will be obvious. If not, then the assumption that the wiring is correct may be false. Expecting the Arduino to power the servo is a bad idea.

  currentState = digitalRead(buttonPin);
  if (currentState == HIGH && lastState == LOW)
  {//if button has just been pressed

Because you have used INPUT_PULLUP in the pinMode() for the button its normal state will be HIGH and it will go LOW when pressed, assuming that is that the button os wired to take the input LOW when pressed. Your comment is therefore wrong.

Id look in to a little better debouncing first. Check out http://arduino.cc/en/Tutorial/Debounce.

Also an i am not sure if it has anything to do with it buy i would try something like myServo rather than just servo.

drksam: Id look in to a little better debouncing first. Check out http://arduino.cc/en/Tutorial/Debounce.

I reckon there is more than enough debouncing in the code already.

Also an i am not sure if it has anything to do with it buy i would try something like myServo rather than just servo.

"servo" will work fine though it makes it easy to confuse the instance of the Class with the Class itself..

The problem is almost certainly what @UKHeliBob pointed out.

...R

A debounce with a delay isn't really a good way to go still as when you get more going on it can slow stuff down but sure it may work.

And i didn't figure it wold help but makes reading easier.

@ OP Try changing

if (currentState == HIGH && lastState == LOW)

to

if (currentState == LOW && lastState == HIGH)

drksam: A debounce with a delay isn't really a good way to go still as when you get more going on it can slow stuff down but sure it may work.

since he's using the state change, the servo's delay to position is plenty of debounce time, I agree with Robin.

as you clean up your code, it looks like a good opportunity to also learn the ternary operator:

void loop()
{
  currentState = digitalRead(buttonPin);
  if (currentState == LOW && lastState == HIGH)
  {
    Serial.print(pos ? "up" : "down");
    servo.write(pos ? 180 : 0);
    delay(150);
    pos = pos ? false : true;
  }
  lastState = currentState;
}

instead of all this:

void loop(){
  
  currentState = digitalRead(buttonPin);
  if (currentState == HIGH && lastState == LOW){//if button has just been pressed
    delay(10);//crude form of button debouncing
    
    //toggle the state of the Servo
    
    if (pos == true){
      servo.write(180);              // tell servo to go to "180" 
      delay(150);                       // waits 15ms for the servo to reach the position 

     
      Serial.println("up");          
      
            pos = false;

      
      
    } else {
      servo.write(0);              // tell servo to go to "0" 
      delay(150);                       // waits 15ms for the servo to reach the position 

      Serial.println("down");
      
            pos = true;

      
    }
  }
  
  lastState = currentState;
}

pos = pos ? false : true;or justpos = !pos;

I found out by now that the servo must be damaged. It only works with the "sweep" example for some reason.

All other examples such as "knob" and codes I found on the net just caused erratic movement or nothing at all.

Seems like it just accepts rotation pulses stepwise 0-->1-->2, not from 0-->180.

But nevertheless thanks for all the help.

regards

/ingo

p.s. I`ll try to keep my code as tidy as possible.

der_ingo: I found out by now that the servo must be damaged. It only works with the "sweep" example for some reason.

That comment makes no sense.

If it works with the sweep example it will work with any other correct code.

Think about it for a couple of seconds ... how could the servo possibly know what sketch is being used?

...R