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.
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);
}
}
}
}