myservo.zip (467Bytes)
Hi there,
I'm having an issue with encapsulating servo functionality in its own class. What I want to do is creating a class that contains a couple of methods that control a servo more or less autonomously, in such a way that just requires me to set the context and then in the loop calling myservo.update() to have the myservo class take care off its own. Very OOP indeed :-)
For some reason I just can't get it to work. Brought down to the bare minimum, I have three source files, main.ino (off course), myservo.h and myservo.cpp:
myservo.h contains this:
class myservo
{
private:
Servo servo;
public:
myservo(int pin);
void write(int pos);
};
myservo.cpp contains:
#include "myservo.h"
myservo::myservo(int pin)
{
servo.attach(pin);
servo.write(90);
}
void myservo::write(int pos)
{
servo.write(pos);
}
And finally, main.ino:
#include <Servo.h>
#include "myservo.h"
myservo servo(4);
void setup()
{
}
void loop()
{
servo.write(60);
delay(100);
servo.write(120);
delay(100);
}
With the servo properly connected to the arduino (yes, I double checked, even succesfully ran a test sketch using a servo object in the main file, old-skool :-) ) nothing seems to happen, except from the centering in the myservo constructor. After that, no movement at all from the servo.
Is there anybody that can shed a light on this? Any suggestion or hint will be greatly appreciated!
Regards,
Erik