I'm writing a library to make a two-wheeled, two-servo robot move forwards. I'm just trying to see if I can write a library, before writing one that's more complex. Here's the code:
// 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();
}
My code compiles and uploads, but it makes the robot move in a circle, not straight ahead. Yet when I run basically the same code directly from a sketch, the robot moves straight ahead just fine! Any ideas what's going on? 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);
}