So i am using attempting to program a leg for a walker, but I am still a fairly new programmer and am not understanding why something is happening.
When i attach the servo within the setup and write to it within the loop everythign behaves exactly as I would expect. However, I have been trying to make everything a little more robust and portable between projects so I have been creating a class called Leg, from this class I create and write the servo object. This class is called within the main loop function. The class functions are bellow, they are held within a .h n .cpp file called Leg.
The problem that is occuring is that the two servo's that are attached will vibrate and not follow any predictable manner when i use the functions setS1 or setS2 in the main loop.
Is there something in the way servo.h works that stops it from working successfully when called from within another class? Any ideas? I am guessing this is the problem but frankly I am stumped.
Do you know when your constructor is called, relative to when the pins that you attach them to are ready to have servos attached to them? Probably nor. Most of what you are doing in the constructor should be done later, in a begin() method that you call from setup().
That static variable, legNumber, is declared in the wrong place, if you want the variable shared by all instances of the class. It should be in the header file, not in the constructor.
Finally, you are using it to define which 2 pins to use. Why is it then incremented by 3?
Hey guys! Thanks for the reply, you solves it. The problem was that I was attaching the servos in the constructore. Sorry, still learning. I have changed it so there is now an attachServo function and it now behaves perfectly.
John: I was not using a serial connection to talk to the servos at this point though i might talk to a Servo controller later. I was just using the Servo library which I think utilises a software driven interupt (though i am not sure).
Paul: Thanks, it was was the contstructor. Thanks for the comment on the variable lebNumber, i am no longer using it with the new function, however, it is good advice for future programming. I was incrementing it by three because the leg actually ahs three servo motors, however I thought it best to get the functions working in 2D first and then work my way up. Was just leaving a little reminder to myself.
Yes, I am indeed building something very power draining. The whole unit has not been built yet and as such the leg that I have in front of me is running straight from the Arduino which is still being powered from it's USB connection. It seems that this provides sufficient power for the moment. When I build the whole unit I will run a seperate power supply for it.
The current plan is to build a walking quadruped with four legs, each comprised of three seperate servos allowing a relatively large degree of freedom in three dimensions. Currently I am focusing on just getting all of the basic functions working for a single leg and with just two servo's such that there is only freedom to move on a 2D plane.
I read through all of the first link and it is very informative, thank you for writing it. I am glad to know how Servo.h works.
Hi,
I would suggest that you start working from a separate power supply now before you join the 'My Arduino wont connect to USB Anymore' club.
Its crazy to slowly destroy a USB Port in an expensive computer when all you need is some AA batteries especially as you know you will need external power eventually.
Just a suggestion,
EDIT : I speak from experience having writting off a USB port in the past two weeks due to a short circuit in a PCB.
Thanks for the advice, I will deffinately follow it. I have an old computer which I may harvest the power supply from (I think i read it is a decent idea to do this from either your page or one stemming from your page).
What is a PCB?
Yeah i run everything through a breadboard and am currently changing everything fairly often so I guess shor circuts are a real risk.
PCB is printed circuit board, I was not quite using a pcb but strip board which is a prototyping board that is between bread board and a proper PCB, it let's you take a bread board design and solder it as a long term project.
With regards to servo power, here is a quick demonstration of what happens to you arduino when your servos get going -
I have seen that before when I was operating my arduino from a battery rather than the USB. The whole thing shut down for very short periods whenever a load to the servo was applied.
Setting up a power supply is one of my tasks on this project but i am not sure what the best way is. Is there a configuration of resistors and capacitors which is regarded as especially good?