Program dont follow

Hello everybody. I have another problem. This is my code:

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


#include <Servo.h> 
 
Servo myservo;  // create servo object to control a servo 
                // a maximum of eight servo objects can be created 
 
int pos = 0;    // variable to store the servo position 
 
void setup() 
{ 
  myservo.attach(9);  // attaches the servo on pin 9 to the servo object 
} 
 
 
void loop() 
{ 
  for(pos = 0; pos < 180; 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(150);                       // waits 15ms for the servo to reach the position 
  } 
  delay(10000);
  for(pos = 180; pos>=1; pos-=1)     // goes from 180 degrees to 0 degrees 
  {                                
    myservo.write(pos);              // tell servo to go to position in variable 'pos' 
    delay(150);                       // waits 15ms for the servo to reach the position 
  } 
}

The servo turn for the max position, then waits 10 seconds and turn back to the original position. My problem is that it not follow the program. He not waits the 10 second and it not delay(150) inside each loop. I dont know why. I am using arduino UNO with Arduino 1.0.3. I Upload the programm and then reset the Arduino, but doesnt follow that code.

Anyone knows why?

Because “loop()” loops?

Please use code tags when posting code.

NB:

 for(pos = 0; pos <= 180; pos += 1)  // goes from 0 degrees to 180 degrees

AWOL:
Because “loop()” loops?

Please use code tags when posting code.

NB:

 for(pos = 0; pos <= 180; pos += 1)  // goes from 0 degrees to 180 degrees

Sorry, modify!.

I know that loop() loops, but I refer to another thing. The first for move the servo from position 0 to position 180º correct? And the second for, from 180 to 0º. But the problem is that the delay(10000) between the two for dont do it.

But the problem is that the delay(10000) between the two for dont do it.

You have only one delay - are you sure you’re observing the correct cycle?

Have you tried a second “delay (10000)” at the end of “loop()” ?

The first for move the servo from position 0 to position 180º correct?

Incorrect.

elseine:
Hello everybody. I have another problem. This is my code:

elseine, quote this post to see what code tags look like.
They will be in brackets . The edit window # button generates them or you can type them.

I made some additions to help you know what your code -is- doing.
It compiles but I have no servo to test with.

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


#include <Servo.h> 
 
Servo myservo;  // create servo object to control a servo 
                // a maximum of eight servo objects can be created 
 
int pos = 0;    // variable to store the servo position 

byte ledPin = 13; // the built-in led on UNO and MEGA Arduinos
 
void setup() 
{ 
  myservo.attach(9);  // attaches the servo on pin 9 to the servo object 
  pinMode( ledPin, OUTPUT ); // default state is LOW
} 
 
 
void loop() 
{ 
  for(pos = 0; pos < 180; 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(150);                       // waits 15ms for the servo to reach the position 
  } 

  digitalWrite( ledPin, HIGH ); // turn the Arduino led ON

  delay(10000);

  digitalWrite( ledPin, LOW ); // turn the Arduino led OFF

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

AWOL:

But the problem is that the delay(10000) between the two for dont do it.

You have only one delay - are you sure you’re observing the correct cycle?

Have you tried a second “delay (10000)” at the end of “loop()” ?

The first for move the servo from position 0 to position 180º correct?

Incorrect.

What does the first “for” then?

0 to 179 degrees.
It is a small difference, but OBO (out-by-one) errors are very common, and potentially dangerous.

GoForSmoke:

elseine:
Hello everybody. I have another problem. This is my code:

elseine, quote this post to see what code tags look like.
They will be in brackets . The edit window # button generates them or you can type them.

I made some additions to help you know what your code -is- doing.
It compiles but I have no servo to test with.

// Sweep

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

#include <Servo.h>

Servo myservo;  // create servo object to control a servo
                // a maximum of eight servo objects can be created

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

byte ledPin = 13; // the built-in led on UNO and MEGA Arduinos

void setup()
{
  myservo.attach(9);  // attaches the servo on pin 9 to the servo object
 pinMode( ledPin, OUTPUT ); // default state is LOW
}

void loop()
{
  for(pos = 0; pos < 180; 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(150);                       // waits 15ms for the servo to reach the position
  }

digitalWrite( ledPin, HIGH ); // turn the Arduino led ON

delay(10000);

digitalWrite( ledPin, LOW ); // turn the Arduino led OFF

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

Is good to know this example, but not works. And I dont know why, because there is no logic. It can to turn, wait and turn, and the only thing that it does, is turn left, then turn right, then turn left…with 45º degress each time.

Why don't you tell us exactly what you observe, and what you expect to observe, and how those two things differ?

GFS's code works as expected for me.... does a half circle, pauses for 10s with LED on, goes back, starts again.

AWOL:
Why don't you tell us exactly what you observe, and what you expect to observe, and how those two things differ?

I expect to observe the next: Move until the limit.In this case, taking 0, mid-position, 90 right position y -90 left position, the program i want to do is go from -90 to 90, wait 10 seconds, and then, turn back to -90 and again, like Sweep demo, with the add of the 10 seconds pause. That I observe is the next: move 45º to left, and then 45º to right; so its moving in the range 45º-0º.

I hope that I explain well. If not, ask me.

Thanks for your time.

JimboZA:
GFS's code works as expected for me.... does a half circle, pauses for 10s with LED on, goes back, starts again.

I just want elseine to see a way to make his/her own clues as to what the code does.
Hint for elseine: you can do more using Serial.print() to show not just where in code is executing but also show values of variables and where you think the servo will point.

What AWOL posted about 0-179 instead of 0-180 (181 degrees) is more important.

Mostly I am waiting for elseine to get off the track he/she seems stuck on and to think.

GoForSmoke:

JimboZA:
GFS's code works as expected for me.... does a half circle, pauses for 10s with LED on, goes back, starts again.

I just want elseine to see a way to make his/her own clues as to what the code does.
Hint for elseine: you can do more using Serial.print() to show not just where in code is executing but also show values of variables and where you think the servo will point.

What AWOL posted about 0-179 instead of 0-180 (181 degrees) is more important.

Mostly I am waiting for elseine to get off the track he/she seems stuck on and to think.

I put a println into the first "for" like Serial.print(pos, DEC) and in the five iteration, after open the Serial Monitor, give me a Exception SerialNotFoundException, and no print more. And other times, it arrives to the five iteration and print pos in each iteration, and the servo stops at sounds like that it cant move, but I move it a little and still running, but not print anymore.

If I can, tomorrow I will upload a video showing it, but I dont know if I can do it tomorrow.

How is your servo powered? It sounds like you are expecting the Arduino power, as well as control, the servo. That won’t happen.

elseine:

GoForSmoke:

JimboZA:
GFS's code works as expected for me.... does a half circle, pauses for 10s with LED on, goes back, starts again.

I just want elseine to see a way to make his/her own clues as to what the code does.
Hint for elseine: you can do more using Serial.print() to show not just where in code is executing but also show values of variables and where you think the servo will point.

What AWOL posted about 0-179 instead of 0-180 (181 degrees) is more important.

Mostly I am waiting for elseine to get off the track he/she seems stuck on and to think.

I put a println into the first "for" like Serial.print(pos, DEC) and in the five iteration, after open the Serial Monitor, give me a Exception SerialNotFoundException, and no print more. And other times, it arrives to the five iteration and print pos in each iteration, and the servo stops at sounds like that it cant move, but I move it a little and still running, but not print anymore.

If I can, tomorrow I will upload a video showing it, but I dont know if I can do it tomorrow.

Did you put Serial.begin in the setup()?