I’ll never let you down, little servo!

Hi, everybody!  So, I did my best to modify this code a little bit, but never really understood why I couldn’t control the function better. I’m pocketing this question for class. But, here’s what the sample code looks like:


#include <Servo.h> 
 
Servo myservo;  
                
int pos = 0;   
 
void setup() 
{ 
  myservo.attach(9);  
} 
 
void loop() 
{ 
  for(pos = 0; pos <= 180; pos += 1) 
  {                                   
    myservo.write(pos);             
    delay(15);                       
  } 
  for(pos = 180; pos>=0; pos-=1)     
  {                                
    myservo.write(pos);              
    delay(15);                       
  } 
} 

Here’s the closest thing to the way I had it in the end, when it still wasn’t functioning… I don’t have a video of that, because it really wasn’t any different with the exception of a little skip in the middle of the sweep.


#include <Servo.h> 
 
Servo myservo;  
                
 
int pos = 0;    
 
void setup() 
{ 
  myservo.attach(9);  
 
void loop() 
{ 
  for(pos = 0; pos <= 10; pos += 1) 
  {                                 
    myservo.write(pos);               
    delay(50);                       
  }
  for(pos = 10; pos <= 20; pos -= 1) 
  {                                 
    myservo.write(pos);               
    delay(50);                       
  }
  for(pos = 10; pos <= 20; pos += 1) 
  {                                 
    myservo.write(pos);               
    delay(50);                       
  } 
  for(pos = 20; pos <= 30; pos += 1) 
  {                                 
    myservo.write(pos);               
    delay(50);                       
  } 
  for(pos = 30; pos <= 40; pos += 1) 
  {                                 
    myservo.write(pos);               
    delay(50);                       
  } 
  for(pos = 40; pos <= 50; pos += 1) 
  {                                 
    myservo.write(pos);               
    delay(15);                       
  } 
  for(pos = 50; pos <= 60; pos += 1) 
  {                                 
    myservo.write(pos);               
    delay(15);                       
  } 
  for(pos = 60; pos <= 70; pos += 1) 
  {                                 
    myservo.write(pos);               
    delay(15);                       
  }  
  for(pos = 70; pos >= 80; pos -= 1)    
  {                                
    myservo.write(pos);              
    delay(15);                       
  } 
  for(pos = 80; pos <= 90; pos += 1) 
  {                                 
    myservo.write(pos);               
    delay(15);                       
  } 
  for(pos = 90; pos <= 100; pos += 1) 
  {                                 
    myservo.write(pos);               
    delay(15);                       
  } 
  for(pos = 100; pos <= 110; pos += 1) 
  {                                 
    myservo.write(pos);               
    delay(15);                       
  } 
  for(pos = 110; pos <= 120; pos += 1) 
  {                                 
    myservo.write(pos);               
    delay(15);                       
  } 
  for(pos = 120; pos <= 130; pos += 1) 
  {                                 
    myservo.write(pos);               
    delay(15);                       
  } 
  for(pos = 130; pos <= 140; pos += 1) 
  {                                 
    myservo.write(pos);               
    delay(15);                       
  } 
  for(pos = 140; pos <= 150; pos += 1) 
  {                                 
    myservo.write(pos);               
    delay(15);                       
  } 

  for(pos = 150; pos <= 160; pos += 1) 
  {                                 
    myservo.write(pos);               
    delay(15);                       
  } 
  for(pos = 160; pos <= 170; pos += 1) 
  {                                 
    myservo.write(pos);               
    delay(15);                       
  } 
  for(pos = 170; pos <= 180; pos += 1) 
  {                                 
    myservo.write(pos);               
    delay(15);                       
  } 
  for(pos = 180; pos <= 0; pos -= 1) 
  {                                 
    myservo.write(pos);               
    delay(15);                       
  } 
} 

And here’s the video of the successful function, although I’m not happy that that’s all it does. I wanted it to function like an old yard sprinkler.

Thanks for looking!

Will