I just got all my new Arduino stuff in and immediately went to work on getting a servo working.
I'm starting out with the Arduino Mega (ATmega1280) and a simple 10K Potentiometer I picked up from RatShack. I loaded the default example for the Servo Knob, made sure my pins all matched and put the Servo on a separate power supply. I also added some debug statements on the serial monitor to see what values I am getting.
myservo.attach(9) returns 0 [failed]
The potentiometer returns the correct values however. I can see the values change when I turn it back and forth.
I added an if (myservo.attached()) in the loop which seems to execute even though the attach() in setup() returned 0 (that's confusing).
I moved to the Nano v3 (ATmega328P) and tried it there. With no pin 9 or 10, I used pin 1 for the Servo data cable. Same results as the Mega.
The only time I even hear the Servo is when I am plugging in the positive or the data wire, it jiggles for .5 second; this is only once in a while.
I'm using the software Arduino v0018. I ensured the correct "Tools -> Board" were selected:
Arduino Mega (or) Arduino Duemilanove or Nano w/ ATmega328
"attach" will return zero if it is the first servo to be attached (it returns the index of the servo in it's internal tables)- why do you think it failed?
(C programs and function often return zero to indicate success)
Is this a different library than what comes default in the Arduino installation?
The other reason I would believe it failed is because it's not doing anything when running Knob and/or Sweep. Again -- I verified all pins were correct and my printed output shows that it "seems" to be setting the value, but the servo just sits there.
// Get Potentiometer
pos = analogRead(potpin); // reads the value of the potentiometer (value between 0 and 1023)
pos = map(pos, 0, 1023, 0, 179); // scale it to use it with the servo (value between 0 and 180)
Serial.print("Val from PoT: ");
Serial.println(pos); // this works; changes correctly as I turn it
myservo.write(pos); // sets the servo position according to the scaled value
pos = myservo.read();
Serial.print("Val from myservo: ");
Serial.println(pos); // this always prints what it was just set too
I have 4 servos (same kind) and none of them are working, so I have to assume it is not the servo nor the example codes that I've been using:
#include <SoftwareServo.h>
SoftwareServo myservo; // create servo object to control a servo
int potpin = 0; // analog pin used to connect the potentiometer
int val; // variable to read the value from the analog pin
void setup()
{
myservo.attach(2); // attaches the servo on pin 2 to the servo object
}
void loop()
{
val = analogRead(potpin); // reads the value of the potentiometer (value between 0 and 1023)
val = map(val, 0, 1023, 0, 179); // scale it to use it with the servo (value between 0 and 180)
myservo.write(val); // sets the servo position according to the scaled value
delay(15); // waits for the servo to get there
SoftwareServo::refresh();
}
Does anyone have an Arduino Mega ATmega1280 working w/ their Servos? I'd like to hear how you connected it!
Sorry, there are no "of course"s here - I have to ask
So true -- never assume anything as that will be the answer to the issue...
I've looked pretty hard on Mega-related. It seems that a while ago (circa 06 software) that the servo library did not work or was limited to only working on pin 9 & 10. I ran into something that said it was moved to pins 10 & 11. I've tested all pins so it's not that.
I've also found that +5v may not be enough Umf for Servos which is why I used an external power source. I've tried plugging the servo directly into the Mega and removing everything else just to see if it would work -- nope. If using an external power source and the only pin used on the Mega is the data pin, I don't have to use or do anything with the Mega's power, right?
Although I can't think of any, would there be a reason to use a resistor (especially since servos demand more power)?
Regarding attach() -- It would figure the document for the function is incorrect. I've always used if (val<0) for error checking (as 0+ is success and status codes). Because 0 (zero) equates to a false boolean, lots of folks gear code to use it as an error code instead of a success code.
I'll keep looking but I don't think I'm going to find much more..
Would you believe that the problem was the PWM ground wire was clipped a little too short and was not making contact? It took me all freaking day to realize this (ugh). All servo's are working now. Thanks for your time and patience!