Trying to sweep a servo 10 times but my loop isn't working?

I'm trying to sweep it from 20-120 degrees 10 times but it just keeps running indefinitely. Can anyone help me out?

#include <Servo.h>
Servo myservo;
int pos = 0;
byte i;

void setup(){
myservo.attach(8);
Serial.begin(9600);
}

void loop(){
for (byte i; i < 11;i++){
for (pos = 20; pos <= 120; pos += 1){
myservo.write(pos);
delay(15);
}
for (pos = 120; pos >= 20; pos -= 1){
myservo.write(pos);
delay(15);
}
}
}

Can you control a servo with the smiley face pin?

If you want the for loop to happen only once, move the code that you have in loop() to setup().

WhosAGoodBoy:

for (byte i; i < 11;i++){

That for loop uses an uninitialized variable i.

Since i is declared global, it is initialized to 0 by default. If i were declared as a local variable with no initializer it could be anything (whatever was in the assigned memory location).

groundFungus:
Since i is declared global, it is initialized to 0 by default. If i were declared as a local variable with no initializer it could be anything (whatever was in the assigned memory location).

Isn't for (byte i,
declaring a different i than the global i ?

groundFungus:
Since i is declared global, it is initialized to 0 by default.

It is true that the global i is implicitly initialized to zero.

However, the OP's for cycle declares and uses its own local i, which has no relation to the global i whatsoever. And that local `i' is left uninitialized.

So, once again, the for cycle relies on an ininitialized variable.

for (byte i,

declaring a different i than the global i ?

Right, sorry, need more coffee.

Okay so I also needed it to run only when I press enter something into the serial monitor but it's not doing anything when I give it an input. Any ideas?

#include <Servo.h>
Servo myservo;
int pos = 0;
int i = 0;
byte bytefromKbd;

void setup(){
myservo.attach(8);
Serial.begin(9600);
Serial.println("Hit a key plus the [Enter] key to execute...:");
if(Serial.available()){
for (i = 0; i < 11; i++){
for(pos = 20; pos <= 120; pos += 1){
myservo.write(pos);
delay(15);
}
for(pos = 120; pos >= 20; pos -= 1){
myservo.write(pos);
delay(15);
}
}
}
}

void loop(){

}

Why do you still have the smiley face in your code?

Hint: read the post describing how to post your code AKA Read this before posting a programming question ...

 while (!Serial.available()){}

maybe.

  if(Serial.available()){
    for (i = 0; i < 11; i++){
      for(pos = 20; pos <= 120; pos += 1){
        myservo.write(pos);
        delay(15);
      }
      for(pos = 120; pos >= 20; pos -= 1){
        myservo.write(pos);
        delay(15);
      }
    }
  }

Once the serial data arrives, you never bother to read it. Why not.

The if statement is not going to wait for serial data to arrive.