No Servo codes will work! why ?

I want something simple as Turn on signal, move servo.
Turn Off signal, move servo back.

I have tested 4 different codes, including building a code from scratch.

but I never get it to work.

I try this code:

#include <Servo.h> 
 const int servoPin = 8;  // Servo pin
 const int buttonPin = 9;  // Pushbutton pin
 int buttonState = 0; 
 int directionState = 0; 
 Servo myservo; 
 int pos = 0;
 
void setup() {
   myservo.attach(8);
   pinMode(buttonPin, INPUT);
 }
 
 void loop(){
   buttonState = digitalRead(buttonPin);
   if (directionState == 0){
     if (buttonState == HIGH) {
       directionState = 1;
       for(pos = 0; pos < 180; pos += 1)
       {
         myservo.write(pos);  
         delay(15);  // waits 15ms to reach the position 
       }
     }
 
   } else if (directionState == 1) {
     if (buttonState == HIGH) {
       directionState = 0;   
       
      for (pos = 180; pos>=1; pos -=1)
       {
         myservo.write(pos);
         delay(15);
       }
     }
   }
 }

And when I power up my Arduino ONE, the servo go to middle position and stay there.
When I then try to have 3v signal into pin 9 , the servo first go all the way to end postion, and they just start to move back and forward and act totally strange.

This happen to all other codes I try also, it just dont work…
I was just going to do a 10 min code, but I have messed with this for an hour now :o

I power the Arduino from USB, can it be lack of power maybe ? (I dont have Battery to test)

There are servos and then there are servos and then there are other servos. Perhaps you have another type? Who can tell?

Paul

Please post a link to your exact servo.

NEVER attempt to power a servo from the 5V pin of the Arduino, you can damage the Arduino, and possibly even the computer if you use the USB port.

A 4xAA battery pack will work for one or two servos. Don’t forget to connect all the grounds.

Does the Servo Sweep example work ?

Attache a wiring diagram showing especially the powerdistribution. Give voltages in the drawing and type of pwr source.
And the motor type, data etc......

What servo? Why 3V? Unos are 5V. Do you have a pulldown resistor on the switch?

If you don’t want the servo to centre on program start you need a write(0) BEFORE the attach().

Steve

I got servo to move when testing the sweep.
I now try to add a button (or a input signal from another arduino) to make it move and stop.
Then move back again.

Like this: input = HIGH It move to pos 150-10
And then input = LOW it move to pos 10-150

I tested with this code

/* 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
*/

#include <Servo.h>

const int buttonPin = 8;  // Pushbutton pin
int buttonState = 0; 
 
Servo myservo;  // create servo object to control a servo
// twelve servo objects can be created on most boards

int pos = 0;    // variable to store the servo position

void setup() {

  pinMode(buttonPin, INPUT);

  
  myservo.attach(9);  // attaches the servo on pin 9 to the servo object
}

void loop() {

 buttonState = digitalRead(buttonPin);

  if (buttonState == HIGH) // Denne for å lese at signal er PÅ?
 
{
  for (pos = 10; pos <= 120; pos += 1) { // goes from 0 degrees to 180 degrees
    // in steps of 1 degree
    myservo.write(pos);              // tell servo to go to position in variable 'pos'
    delay(16);                       // waits 15ms for the servo to reach the position
  }

 

if (buttonState == LOW) {
  
  for (pos = 120; pos >= 10; pos -= 1) { // goes from 180 degrees to 0 degrees
    myservo.write(pos);              // tell servo to go to position in variable 'pos'
    delay(16);                       // waits 15ms for the servo to reach the position
  }
}
}
}

When I power up it stand still (OK)
But when I press button it go to the positon I want, but it also go back again.

Why will it not wait for my IF (buttonstate =LOW) before it goes back ?

My button is not LOW, so it should wait there.

The statements repeat while the state is high and also the other while the state is low. Does that help?

liteglow:
I got servo to move when testing the sweep.

What was the problem with the setup in the first post ?

Look carefully at the code block for the test for HIGH input. It should end after the first for loop, but it doesn't

Auto formatting the code in the IDE and putting each { and } on its own line shows up the problem.

Hi,
Do you have pull-down 10K resistor on the button input pin of the UNO?

Thanks… Tom… :slight_smile:

UKHeliBob:
Look carefully at the code block for the test for HIGH input. It should end after the first for loop, but it doesn't

Auto formatting the code in the IDE and putting each { and } on its own line shows up the problem.

I tried, but it does not affect the servo.

srnet:
What was the problem with the setup in the first post ?

I dont know, maybe the same problem as this code.. (I cant get it to work)
But in my first code it was IF button state is HIGH = .. and then again IF button state is HIGH = ..
Should it not be HIGH in the first, and then LOW on the last ?

But what you really want to say is IF button is high (AND THE SERVO ISN'T ALREADY AT 120) then move it to 120. Likewise for button low.

The problem with that Sweep technique is that it actually says "Wherever the servo is now, make it jump at full speed to 10 then move slowly to 120". If the servo was already at 120 that really wasn't what you wanted.

Steve

TomGeorge:
Hi,
Do you have pull-down 10K resistor on the button input pin of the UNO?

Thanks… Tom… :slight_smile:

Yep. So I think the button work.
Also when I push the button the servo move, but problem is that it does not stay in position.
It goes back again.

There is nothing wrong with the servo, power, or the board. Only MY skills in programming :o

I tried to write a new code.
It did not work either

#include <Servo.h>

const int buttonPin = 8;  // Pushbutton pin
int buttonState = 0;
Servo myservo;  // create servo object to control a servo
int pos = 0;    // variable to store the servo position

void setup() {
  // put your setup code here, to run once:


 pinMode(buttonPin, INPUT); //set the button pin as INPUT
  myservo.attach(9);  // attaches the servo on pin 9 to the servo object
 
}



void loop() {

  int stateButton = digitalRead(buttonPin); //read the state of the button
  if(stateButton == 1) { //if is pressed
    
  
for (pos = 10; pos <= 120; pos += 1) { // goes from 0 degrees to 180 degrees
      // in steps of 1 degree
      myservo.write(pos);              // tell servo to go to position in variable 'pos'
      delay(16);}




}

 else { //if not pressed


for (pos = 120; pos >= 10; pos -= 1) { // goes from 180 degrees to 0 degrees
        myservo.write(pos);              // tell servo to go to position in variable 'pos'
        delay(16);                       // waits 15ms for the servo to reach the position
      }



}



}

liteglow:
There is nothing wrong with the servo, power, or the board. Only MY skills in programming :o

I tried to write a new code.
It did not work either
But we don't know that?

A circuit diagram and a picture of your project so we can see your component layout will help immensely.
Thanks.. Tom.. :slight_smile:

Hi,
The basic problem with your code is that when it gets to say 120, you do not stop the for loop executing another 10 to 120 sweep.
Try this edited code.
Open the IDE monitor screen and make sure it is set for 115200 baud.
Observe the action and the data in the monitor screen.

#include <Servo.h>
const int buttonPin = 8;  // Pushbutton pin
int buttonState = 0;
bool ServoAt120 = false; //flag for servo at 120
bool ServoAt10 = true;  //flag for servo at 10
Servo myservo;  // create servo object to control a servo
int pos = 0;    // variable to store the servo position

void setup()
{
  Serial.begin(115200);
  pinMode(buttonPin, INPUT); //set the button pin as INPUT
  myservo.attach(9);  // attaches the servo on pin 9 to the servo object
  myservo.write(10);  // intialise servo at 10
}

void loop()
{
  int stateButton = digitalRead(buttonPin); //read the state of the button

  if (stateButton == 1 && !ServoAt120) //if is pressed and servo not at 120
  {
    for (pos = 10; pos <= 120; pos += 1) // goes from 10 degrees to 120 degrees in steps of 1 degree
    {
      myservo.write(pos);              // tell servo to go to position in variable 'pos'
      Serial.print("Button  ");
      Serial.print(stateButton);
      Serial.print(" Servo Position = ");
      Serial.println(pos);
      ServoAt120 = true;   //when for 10 to 120 for loop is exited servo will be at 120
      ServoAt10 = false;
      delay(16);
    }
  }
  if (stateButton == 0 && !ServoAt10) //if is pressed and servo not at 10
  {
    for (pos = 120; pos >= 10; pos -= 1) // goes from 120 degrees to 10 degrees in steps of 1 degree
    {
      myservo.write(pos);              // tell servo to go to position in variable 'pos'
      Serial.print("Button  ");
      Serial.print(stateButton);
      Serial.print(" Servo Position = ");
      Serial.println(pos);
      ServoAt120 = false;
      ServoAt10 = true;   //when 120 to 10 for loop is exited servo will be at 10
      delay(16);
    }
  }
}

Tom… :slight_smile: