Arduino Forum

Using Arduino => Programming Questions => Topic started by: WhosAGoodBoy on Apr 18, 2019, 05:14 pm

Title: Trying to sweep a servo 10 times but my loop isn't working?
Post by: WhosAGoodBoy on Apr 18, 2019, 05:14 pm
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);
    }
  }
}
Title: Re: Trying to sweep a servo 10 times but my loop isn't working?
Post by: groundFungus on Apr 18, 2019, 05:22 pm
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().
Title: Re: Trying to sweep a servo 10 times but my loop isn't working?
Post by: Montmorency on Apr 18, 2019, 05:28 pm
Code: [Select]
for (byte i; i < 11;i++){
That `for` loop uses an uninitialized variable `i`.
Title: Re: Trying to sweep a servo 10 times but my loop isn't working?
Post by: groundFungus on Apr 18, 2019, 05:42 pm
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).
Title: Re: Trying to sweep a servo 10 times but my loop isn't working?
Post by: evanmars on Apr 18, 2019, 05:48 pm
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
Code: [Select]
for (byte i,
declaring a different i than the global i ?
Title: Re: Trying to sweep a servo 10 times but my loop isn't working?
Post by: Montmorency on Apr 18, 2019, 05:51 pm
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.
Title: Re: Trying to sweep a servo 10 times but my loop isn't working?
Post by: groundFungus on Apr 18, 2019, 06:03 pm
Quote
for (byte i,

declaring a different i than the global i ?
Right, sorry, need more coffee.
Title: Re: Trying to sweep a servo 10 times but my loop isn't working?
Post by: WhosAGoodBoy on Apr 18, 2019, 06:04 pm
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(){
 
}
Title: Re: Trying to sweep a servo 10 times but my loop isn't working?
Post by: evanmars on Apr 18, 2019, 06:11 pm
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 ...
Title: Re: Trying to sweep a servo 10 times but my loop isn't working?
Post by: evanmars on Apr 18, 2019, 06:14 pm
Code: [Select]
while (!Serial.available()){}
maybe.
Title: Re: Trying to sweep a servo 10 times but my loop isn't working?
Post by: PaulS on Apr 22, 2019, 10:44 am
Code: [Select]
  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.