looping problem

Hi,
I’m trying to control my servo with Arduino Duemilanov. I’d like to make it turn from 0 to 90 degrees then go back to original position. It successfully turned however it keep doing it forever. I just wanna make it does once and stops. please have a quick look on my code. your advices will be appreciated.

#include <Servo.h>

Servo myservo;
int pos = 0;

void setup() {
pinMode(1,OUTPUT);
myservo.attach(14); //analog pin 0
Serial.begin(19200);
Serial.println(“Ready”);
}

void loop() {
for(pos = 0; pos>=90; pos+=1) // goes from 180 degrees to 0 degrees
{
myservo.write(pos); // tell servo to go to position in variable ‘pos’
delay(15);
}
for(pos = 90; pos>=1; pos-=1) // goes from 180 degrees to 0 degrees
{
myservo.write(pos); // tell servo to go to position in variable ‘pos’
delay(15);
}
}

The loop function is called in an endless loop. So, in your case, the servo moves from its start position to a new position, in small steps, then moves back to the initial position, and loop ends. Then, it gets called again. And again, and again.

If you want loop to do something only once, you have [edit]at least[/edit] two choices. First is to create a global boolean variable, set to true.

Then, in loop, if that variable is true, do the one time thing AND set the variable to false.

Alternatively, you can ensure that loop never ends, by creating your own infinite loop, after the servo movement completes.

void loop()
{
   // Move the servo

   // Now, do nothing forever
   while(true)
   {
   }
}

Using a microcontroller to move a servo once seems wasteful, somehow.

you have two choices

or the third of two choices is to put your code into “setup”, which is called only once.

or the third of two choices is

I fixed that!

nice ! love you fox.