Pages: [1]   Go Down
Author Topic: MegaServo.h read servo position  (Read 443 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 34
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 smiley-wink

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:
#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  :smiley
« Last Edit: June 07, 2009, 07:20:56 pm by fujii » Logged

SG
Offline Offline
Sr. Member
****
Karma: 7
Posts: 419
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
#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);
}
 



Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 34
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Code:
servo1.read(y)

thank you  smiley
Logged

Pages: [1]   Go Up
Jump to: