I'm trying to make a class work with Arduino, however it doesn't seem to trigger properly. The code is compiling perfectly, and it's supposed to control the leg of an hexapod. Also, the example sweep works on this servo, so no problem here. However, the servo is not actually moving. I'm thinking it has something to do with the order of declaring the variables:
#include <Servo.h>
// Abstract each leg, so only those functions can be accessed
class Leg {
// Actions that can be triggered from outside
public:
Leg(int hipPin, int kneePin);
void up();
// These should be only available for the leg
private:
Servo hip;
Servo knee;
};
Leg::Leg(int hipPin, int kneePin) {
hip.attach(hipPin);
knee.attach(kneePin);
}
// Testing the leg: move it up
void Leg::up() {
for(int pos = 0; pos < 180; pos += 1) {
hip.write(pos);
delay(15);
}
}
// Initialize the leg
Leg topLeft(9, 10);
void setup() {
}
// Test the function up()
void loop() {
topLeft.up();
}
I'm testing only the pin 9 (leaving the 10 empty). It seems in pain, meaning, you can see the servo trembling but not moving really at 0 degrees.