I am new to programming microcontrollers and currently am having problems with using an ATMega168 ardupilot board to control some servos. I am using the servo library and the code compiles and print lines occur (eg Servos centred) as it runs but the servos do not move. Can anyone help me figure out my problem? I have connected my servos to what I think are pins 9 and 10 (pins 15 and 16 on the ATMega168) The code is:
#include <Servo.h>
Servo servo1, servo2;
int pos1 = 180;
int pos2 = 0;
int read1 = 0;
int read2 = 0;
Well the first obvious problem I see is that you're not considering that the loop() function continues to run forever - not just one time. In your code you're moving the servos and then detaching them. The next loop you'd be attempting to write to detached servos.
Also the "while(pos1 != pos2)" is kind of scary because it assumes that the two values will be equal before terminating the loop. Depending on your start values and increments you could easily get stuck in an infinite loop.
That said, I don't see anything that would prevent the servos from at least twitching. Maybe you have a wiring problem.
Have you tried the "sweep" example to see if you can get one moving?
Thanks for your advice. I tried the sweep example but nothing moves. However, I am unsure if there is a wiring problem given that when I initially plug in the servos into the powered board there is an initial recognition twitch. If this twitch is indeed indicative of correct wiring then I am uncertain as to what is causing the problem.
If the servo(s) do not move when commanded to by the Sweep sketch code, then there must be a wiring error. Make sure you have got the servo's control signal(s) wired to the correct pin(s) on the Arduino.