Servo Code Double Check

Hey , never have written code before and just wanted some input. What I am looking to do is as follows. Servo starts as far negative as it can go. Then when momentary pressed it goes to 120 and checks button status again. If button released return to negative position. If button still pressed move to 70 then 120 and repeat till released.

Also can bad code damage the servo or is there a way to test it with out the servo attached?

Arduino Uno
HS-755HB
Running off a 12v battery with voltage regulator set to 9v for Uno.

#include <Servo.h>

Servo myservo;

int pos = 0;
int buttonPin = 2;
int buttonState = 0;


void setup()
{
  myservo.attach(9);
  myservo.write(0);
}

void loop()
{
  if (digitalRead(buttonState) == HIGH){
    
    myservo.write(120); {
      delay(15);
      if (digitalRead(buttonState) == LOW) {
    
    myservo.write(0);
  }
      else {
        myservo.write(70);
        delay(15);
        myservo.write(120);
      }
    }
  }
  
  else {
    myservo.write(0);
  }
}

Each servo is different so you should experiment to find the limits of movement. You might find that it can only move between (say) 9 deg and 177 deg. You might also find that it can go further than 0deg and you can check that using servo.writeMicroseconds(). 1000 microsecs is about the same as 0deg and 2000 is 180 deg.

It is not a good idea to try to get the servo to move past its physical end-stops as it overloads the motor and may damage the gears.

If you are thinking of developing your project into something more complex you would find it easier to do if you reorganize your code into separate functions such as one to read and save the switch value and another to move the servo. You would also need a variable to record the current state of the servo so that the program would know whether to go forward or backward.

The demo code in the first post in this Thread illustrates the idea.

...R

Thanks for the reply. Read the code you mentioned but not sure how to do the variable still. That was a lot of code to look at for me still. I got what was going on just not sure where variable for sensor position is. Besides reorganizing and adding the variable for servo position the rest looks good. I wasn't positive with the delays and if i used if...else properly.

Flagger:
Read the code you mentioned but not sure how to do the variable still.

If you make your best attempt and then post your code people will be able to help.

...R

Judging by the voltages that you mentioned the servo is being supplied 5V from the Arduino. If so that is not a good idea because the servo can easily take more current than the Arduino voltage regulator can supply. Consider powering the servo from an external supply but don't forget to connect the two grounds.

Well looked at at your sample code you had me look at again. Got some good ideas and had it working exactly like I wanted. Then it stopped working a day or two later. The servo now goes all the way to the stop point on counter clockwise side. The momentary switch has no effect. So not sure if servo went bad or what happened? The code was never changed either.

#include <Servo.h>

Servo myservo;

int pos = 0;
int buttonPin = 2;
int buttonState = 0;
int servoPosition = 25;

void setup()
{
  myservo.attach(9);
  myservo.write(servoPosition);
}

void loop()
{
  buttonState = digitalRead(buttonPin);
  switch(buttonState)
  {
  case HIGH:
    {
      myservo.write(110);
      delay(8);
      myservo.write(80);
      delay(8);
      myservo.write(110);
    }
    case LOW:
    {
      myservo.write(25);
    }
  }
}

You have no pinMode() for your button pin.

If you have no pull-down resistor on the button pin you should add pinMode(buttonPin, INPUT_PULLUP); into setup() and then you should swap the HIGH and LOW in your switch/case statements because LOW will signify the button is pressed.

If you have a pull-down resistor then the code should be pinMode(buttonPin, INPUT); but it is better to use a pull-up resistor, and the internal one is easiest.

As it stands your code causes the servo to move back and forth quickly while you press the button. Is that what you want?

The delay( 8 ) seems a very short time for the servo to move to its new position. Try 108 and 208.

...R

case HIGH:
    {
      myservo.write(110);
      delay(8);
      myservo.write(80);
      delay(8);
      myservo.write(110);
    }
    case LOW:
    {
      myservo.write(25);
    }

I think you need to take a break.
Or two.

AWOL:
I think you need to take a break.
Or two.

I tried out this code before my earlier reply and I confess I had not noticed the missing break; statements. The code seemed to work fine.

Interestingly I have just tried the code again with and without break and it seems to make no difference.

...R

but it is better to use a pull-up resistor

Why ?

and the internal one is easiest.

Agreed wholeheartedly

Robin2:

AWOL:
I think you need to take a break.
Or two.

I tried out this code before my earlier reply and I confess I had not noticed the missing break; statements. The code seemed to work fine.

Interestingly I have just tried the code again with and without break and it seems to make no difference.

...R

Because of the speed of "loop()", the fall-through from the HIGH case to the LOW case would result in a very brief write to 25 "degrees", and back round to read the switch again, which would almost certainly still be HIGH, so you'd write 110 "degrees" almost straightaway.
The servo simply wouldn't have time to move.

AWOL:
Because of the speed of “loop()”, the fall-through from the HIGH case to the LOW case would result in a very brief write to 25 “degrees”, and back round to read the switch again, which would almost certainly still be HIGH, so you’d write 110 “degrees” almost straightaway.
The servo simply wouldn’t have time to move.

Sounds very sensible. I must try it out later.

…R

I do have a pull-down on the switch. Yes it is supposed to move rapidly back and forth when the button is pressed. Robin2 so the code worked for you then? If it did could this point to a bad servo. Is there a clear cut way to know or do I have to just replace the servo. I just find it odd that it worked for awhile then stopped. I will try to add the pinMode() after work too.

Try the servo sweep program. If the servo works with that then it is OK.

Did we establish how the servo is powered ? From the Arduino 5V and GND pins or from the external supply ?

Flagger:
I will try to add the pinMode() after work too.

Trying is not an option - it must be done.

Also add break; at the end of each CASE. See the reference pages.

...R

I do have external 6 volts for the servo. I tried the sweep program and same thing. It just wants to go past -90 and does nothing else. So assuming something got fried in the servo. By the way Thank You for the help.

Mate, 8ms for a servo to move is nothing. Did you want 8 seconds? If so, delay(8000) would be a bit better.

edit: I just read your last post, and you want it to move rapidly. But I can assure you, the servo won't make it back and forth in 8ms. A standard servo requires 120ms to move 60 degrees.
Feel free to post the sweep-code you used, to make sure the servo is broken. There might just be a small error with the code! :slight_smile: