Go Down

Topic: sweeping servo (Read 934 times) previous topic - next topic

akpeter

Hi I am trying to sweep a servo pause for a period of time and sweep again or a series of sweeps.

This is what i have written.
Servo myservo;  // create servo object to control a servo
                // a maximum of eight servo objects can be created

int pos = 0;    // variable to store the servo position

void setup()
{
  myservo.attach(9);  // attaches the servo on pin 9 to the servo object
}
 
void loop()
{
  {
   servo();
   delay(200);
  }
 
void servo();
  for(pos = 0; pos < 180; pos += 1)  // goes from 0 degrees to 180 degrees
  {                                  // in steps of 1 degree
    myservo.write(pos);              // tell servo to go to position in variable 'pos'
    delay(15);                       // waits 15ms for the servo to reach the position
  }
  for(pos = 180; pos>=1; pos-=1)     // goes from 180 degrees to 0 degrees
  {                               
    myservo.write(pos);              // tell servo to go to position in variable 'pos'
    delay(15);                       // waits 15ms for the servo to reach the position
  }
}

I am getting an error message servo was not declared in this scope.  help

retrolefty

I think you are missing a:

#include <Servo.h>

at the start of your sketch, so the IDE can locate the servo library and include it with your sketch.

Lefty

akpeter

Lefty ,it is there i just failed to copy it when i pasted the sketch. It is highlighting thethe first servo after void loop and telling me it was not declared in this scope.

Peter

AWOL


I'd  lose the semicolon
Code: [Select]
void servo()[glow=red,2,300];[/glow]
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

akpeter

loosing the semi-colon added another error message. and adding back cleared that one . I am not telling it something about this state and I do not now what, how do you declare.

Peter

AWOL

#5
Feb 06, 2011, 10:21 pm Last Edit: Feb 06, 2011, 10:26 pm by AWOL Reason: 1
Look at the how "loop" is declared, then look at how you have declared "servo"
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

akpeter

AWOL ihave followed your lead and taken out the void semi-colon and gotten another error message and returned it and gotten rid of that one, renamed the loops sweeps thinking I had a conflict in the names. still getting the "name" not declared in this scope.

Peter

akpeter

this is how its writtin currently
#include <Servo.h>

Servo myservo;  // create servo object to control a servo
                // a maximum of eight servo objects can be created

int pos = 0;    // variable to store the servo position

void setup()
{
  myservo.attach(9);  // attaches the servo on pin 9 to the servo object
}
 
void loop()
{
   sweep(0);
   delay(200);

void sweep(int duration);
  for(pos = 0; pos < 180; pos += 1)  // goes from 0 degrees to 180 degrees
  {                                  // in steps of 1 degree
    myservo.write(pos);              // tell servo to go to position in variable 'pos'
    delay(15);                       // waits 15ms for the servo to reach the position
  }
  for(pos = 180; pos>=1; pos-=1)     // goes from 180 degrees to 0 degrees
  {                               
    myservo.write(pos);              // tell servo to go to position in variable 'pos'
    delay(15);                       // waits 15ms for the servo to reach the position
  }
}

Just the one error message

PaulS

Code: [Select]
void loop()
{
   sweep(0);
   delay(200);

void sweep(int duration);

The sweep function that you are trying to define is all wrong. First, it is inside of the loop() function. Functions CAN NOT be nested.

Second, function declarations do not end with a semicolon.

This code will at least compile.
Code: [Select]
#include <Servo.h>

Servo myservo;  // create servo object to control a servo
                // a maximum of eight servo objects can be created

int pos = 0;    // variable to store the servo position

void setup()
{
  myservo.attach(9);  // attaches the servo on pin 9 to the servo object
}

void loop()
{
   sweep(0);
   delay(200);
}

void sweep(int duration)
{
  for(pos = 0; pos < 180; pos += 1)  // goes from 0 degrees to 180 degrees
  {                                  // in steps of 1 degree
    myservo.write(pos);              // tell servo to go to position in variable 'pos'
    delay(15);                       // waits 15ms for the servo to reach the position
  }
  for(pos = 180; pos>=1; pos-=1)     // goes from 180 degrees to 0 degrees
  {                               
    myservo.write(pos);              // tell servo to go to position in variable 'pos'
    delay(15);                       // waits 15ms for the servo to reach the position
  }
}

Go Up