Question about While loop with Servo Motor

I am trying create a program something like:

If button is pressed, then begin loop

  • If pressed, wait specific time (for now, 2 seconds), then begin loop*
  • Loop:*
  • At starting position of servo, move 26 degrees, then delay 1 second, then move back to starting \ position*
  • then delay again*
  • repeat loop until timer is greater than timeout(7.5 seconds for now)*

*/

#include <Servo.h>

unsigned long timer;
Servo myservo; // create servo object to control a servo
// twelve servo objects can be created on most boards
static const unsigned long timeout = 7500; // loop ends after specified duration

int pos = 0;
int button = 2;
int picpos = 26; // variable to store the servo position

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

void loop()
{
timer= millis();
if (digitalRead(button)==HIGH) // begin loop if button is pushed
{
delay(2000);

while(button ==LOW)
{
for(pos = 0; pos <= picpos; pos += picpos) // 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(10); // waits 15ms for the servo to reach the position
}
for(pos = picpos; pos>=0; pos-=picpos) // goes from 180 degrees to 0 degrees
{
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(1000); // waits 15ms for the servo to reach the position
if(((millis()-timer)>timeout))
{
break;
}
}
}
}
}

Everything worked (without repeating) until I tried adding a while loop, timer, timout. Could someone try explaining what I am missing or doing wrong.

Here is some formatted code in code tags.

I guess it's not enough to have a sticky post up there that says, "How to use this forum." Maybe we should add some kind of feature that kicks people off if they haven't read it yet.

#include <Servo.h> 

unsigned long timer; 
Servo myservo;  // create servo object to control a servo 
// twelve servo objects can be created on most boards
static const unsigned long timeout = 7500; // loop ends after specified duration

int pos = 0;
int button = 2;
int picpos = 26;  // variable to store the servo position 


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

void loop() 
{ 
  timer= millis();   
  if (digitalRead(button)==HIGH) // begin loop if button is pushed
  { 
    delay(2000);

    while(button ==LOW)
    {
      for(pos = 0; pos <= picpos; pos += picpos) // 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(10);                       // waits 15ms for the servo to reach the position 
      } 
      for(pos = picpos; pos>=0; pos-=picpos)     // goes from 180 degrees to 0 degrees 
      {                                
        myservo.write(pos);              // tell servo to go to position in variable 'pos' 
        delay(1000);        // waits 15ms for the servo to reach the position 
        if(((millis()-timer)>timeout))
        {
          break;
        }
      }
    }
  }
}
for(pos = picpos; pos>=0; pos-=picpos)

Whatever picpos is, if you set a variable to it, and then substract that from it you'll end up with zero. So this loop makes exactly one iteration and then is done. If you only need it to loop once, then a for loop is pretty redundant.

// goes from 0 degrees to 180 degrees
  {                                  // in steps of 1 degree

Does it?
Really?