Go Down

Topic: MegaServo.h read servo position (Read 570 times) previous topic - next topic

fujii

Jun 08, 2009, 02:16 am Last Edit: Jun 08, 2009, 02:20 am by fujii Reason: 1
hi, i'm new to programming and microcontroller so i got myself a arduino mega to learn, oh and english isn't my native language so...sry about my writing style ;)

about my problem:
how do i read the position from a servo, can someone change this testcode for me so that i understand it?

i want to move a servo to random positions and if the servo has reached a specific position (in this case 90°) a led should light up
Code: [Select]

#include <MegaServo.h>
MegaServo servo1;
int y = 0;
long x;
int LED = 13;

void setup(){
servo1.attach(10);
}

void loop(){
x = random(180);

servo1.write(x);
// add read servopositioncode here please and save position in y
if (y == 90){
           digitalWrite(LED, HIGH);}
          else {
            digitalWrite(LED, HIGH);}
delay(1000);
}


thx for your help
fujii

edit:
i also don't understand the "read_us" and "attached" function, would be nice if someone can write a little examplecode  ::)

BillHo

The methods are:

  MegaServo - Class for manipulating servo motors connected to Arduino pins.

  attach(pin )  - Attaches a servo motor to an i/o pin.
  attach(pin, min, max  ) - Attaches to a pin setting min and max values in microseconds
   default min is 544, max is 2400  

  write(unsigned int)     - Sets the servo pulse width in microseconds or degrees. (0-200 as degrees, greater values are uS)
  read()      - Gets the last written servo pulse width as an angle between 0 and 180.
  read_us()   - Gets the last written servo pulse width in microseconds.
  attached()  - Returns true if there is a servo attached.
  detach()    - Stops an attached servos from pulsing its i/o pin.

Code: [Select]
#include <MegaServo.h>
MegaServo servo1;
int y = 0;
long x;
int LED = 13;

void setup(){
servo1.attach(10);
}

void loop(){
x = random(180);

servo1.write(x);
// add read servopositioncode here please and save position in y
y = servo1.read(); // Gets the last written servo pulse width as an angle between 0 and 180.
if (y == 90){
           digitalWrite(LED, HIGH);}
          else {
            digitalWrite(LED, HIGH);}
delay(1000);
}






fujii

Ahh now i get it. I allways wrote something like:

Code: [Select]
servo1.read(y)

thank you  :)

Go Up