Go Down

Topic: Problem making a Servo library (Read 382 times) previous topic - next topic

zps

I'm writing a library to make a two-wheeled, two-servo robot move forwards.  Dull, I know, but you've got to start somewhere.

When I run a sketch of the idea, the robot moves forwards.  The library compiles and uploads, but it makes the right servo moves more slowly than the left, so the robot just spins around.  

Any ideas what's going on?  There are a lot of posts about using libraries from within libraries in this section, and I wonder if it's related to that.

Here's the sketch that works:

#include <Servo.h>

Servo left;
Servo right;

void setup(){
left.attach(9);
right.attach(10);
}

void loop(){
left.write(180);
right.write(0);
}

Here's the library code that doesn't work:

// Move.h:
#ifndef Move_h
#define Move_h

#include <WProgram.h>
#include <Servo.h>

class Move
{
public:
    Move(int leftPin, int rightPin);
    void fd();
    Servo leftServo;
    Servo rightServo;
};

#endif

// Move.cpp:
#include "Move.h"

Move::Move(int leftPin, int rightPin) {
    leftServo.attach(leftPin);
    rightServo.attach(rightPin);
}
   
void Move::fd(){
    leftServo.write(180);
    rightServo.write(0);
}

// Sketch1.pde:
#include <Servo.h>
#include <Move.h>


Move move(9, 10);

void setup(){
}

void loop(){
move.fd();
}

Go Up