19. {
// start main loop
myservo. write (pos); // set servo speed/direction to the value of pos variable
tbuttonState = digitalRead (topButton); // read the state of the top button
bbuttonState = digitalRead (bottomButton); // read the state of the bottom button
if (bbuttonState == HIGH ) // if top button pressed
{newParameters(85, 93);} //calculate a speed in other direction
if (tbuttonState == HIGH ) // if bottom buttom pressed
{newParameters(96, 103);} //calculate a speed in other direction
timer +=10; // increment timer value by 10
if (timer >= stoptime)
//if run time limit is reached,
{
myservo. write (95); //stop servo
newParameters(80, 105); //compute new parameters
delay (randpause); // pause for a random time
}
delay (100); // pause for 100 milliseconds
if ( pos < 97)
{ digitalWrite (led2, HIGH );}
else
{ digitalWrite (led2, LOW ); }
//power on led2
//power off led2
if ( randpause > 2000)
{ digitalWrite (led3, HIGH );}
else { digitalWrite (led3, LOW ); }
if ( timer > 90)
{ digitalWrite (led1, HIGH );}
else { digitalWrite (led1, LOW ); }
}
// end main loop
void newParameters( int hi, int lo) // function to calculate new speed/direction, time
and pause
{
pos = random (hi, lo); //generate new speed/direction, within hi and lo limits
randpause = random (1000, 5000); // generate random pause between 1 and 5
seconds
pause = 100;
timer = 0; //set timer to zero
stoptime = random (50, 200); // generate random time to run servo
}