Hello, I hope someone will read and help... I'm some what new to arduinos, and most of programs have been on the UNO. But since I upgraded to a mega 2560, I've been running onto trouble. Mostly pin based, hopefully.
I am connecting a small EMAX servo to the 2560, with the red connected to 5V, the brown connected to gnd, and the orange connected to analog A0. The program is below. The servo makes the little noise when I connect power, so I assume the 5V and gnd is right... I'm looking at the schematic and I think the pin number is 78. It worked on the UNO with pin 0, so why not now??? =(
#include <Servo.h>
Servo test;
int pos = 0; // Try floats next time.
int PIN = 78;
I believe you are associating the wrong digital pin number for the analog input pin 0 number as used in mega boards.
So instead of int PIN = 78;
you should be using int PIN = 54; if wired to the A0 pin.
But better yet and to avoid errors in the future using the analog pins for digital purposes simply
use int PIN = A0;
Here is the association of analog pin names to there corresponding digital pin names:
And not that there is anything wrong with it, but why are you trying to use an analog input pin for digital servo output in the first place? A pin is a pin, and properly set up, it doesn't really matter, but you seemed to pick it specifically for some purpose. Just curious.
To start, let me just say I'm new to forums and wow. Just wow. I left for dinner and so many replies. I'm really surprised and kinda touched. Thank you all who replied. Now back to business.
retrolefty: That worked. Thank you. But out of curiosity, what keeps arduino from freaking out about A0 being used as an int?
holmes4: Thanks for the reply. But the "int PIN = A0;" worked. But curious, why not use serial i/o? Sorry if this is a silly question.
AWOL: I'm trying (eventually) to use the servo as a yaw vane for a UAV and I might need a angle that wasn't an integer. I don't think it makes a difference, but I thought I'd try it. It does work, so good to know.
retroplayer: No real reason for analog vs digital. At least none I know. Its the way the tutorial I was basing my code from was setup. Here's the site if interested. Arduino Playground - SingleServoExample
gln6fgsl:
To start, let me just say I'm new to forums and wow. Just wow. I left for dinner and so many replies. I'm really surprised and kinda touched. Thank you all who replied. Now back to business.
retrolefty: That worked. Thank you. But out of curiosity, what keeps arduino from freaking out about A0 being used as an int?
Because A0 is an int compatible constant which was already assigned the value 54 in that code snippet I posted earlier.
Lefty
holmes4: Thanks for the reply. But the "int PIN = A0;" worked. But curious, why not use serial i/o? Sorry if this is a silly question.
AWOL: I'm trying (eventually) to use the servo as a yaw vane for a UAV and I might need a angle that wasn't an integer. I don't think it makes a difference, but I thought I'd try it. It does work, so good to know.
retroplayer: No real reason for analog vs digital. At least none I know. Its the way the tutorial I was basing my code from was setup. Here's the site if interested. Arduino Playground - HomePage
gln6fgsl:
To start, let me just say I'm new to forums and wow. Just wow. I left for dinner and so many replies. I'm really surprised and kinda touched. Thank you all who replied. Now back to business.
retrolefty: That worked. Thank you. But out of curiosity, what keeps arduino from freaking out about A0 being used as an int?
Because A0 is an int compatible constant which was already assigned the value 54 in that code snippet I posted earlier. A0 is the name of the constant, not it's value.
Lefty
holmes4: Thanks for the reply. But the "int PIN = A0;" worked. But curious, why not use serial i/o? Sorry if this is a silly question.
AWOL: I'm trying (eventually) to use the servo as a yaw vane for a UAV and I might need a angle that wasn't an integer. I don't think it makes a difference, but I thought I'd try it. It does work, so good to know.
Actually the servo.write(degrees) is a servo library abstraction for command movement as most/many servos are capable of better resolution then 0-180 degrees. There is a servo.writeMicroseconds(xxxx) command that will allow use of better servo positing command. All servos will respond to xxxx = to 1000 to 2000 range, but most will allow 'over travel' in both lower and upper values, but that is servo model specific and until you test your servo you won't know what it's largest possible physical travel limits are. But to be sure servo.writeMicroseconds(xxxx) is the better method to use with servos if best resolution results are needed.
Lefty
retroplayer: No real reason for analog vs digital. At least none I know. Its the way the tutorial I was basing my code from was setup. Here's the site if interested. Arduino Playground - HomePage
The A0 worked wonderfully. But when I was playing around with it, I noticed the program works for A0-A9, but not A10-A15. So now I'm wondering why not? I don't need it for my project, but I am curious.
gln6fgsl:
The A0 worked wonderfully. But when I was playing around with it, I noticed the program works for A0-A9, but not A10-A15. So now I'm wondering why not? I don't need it for my project, but I am curious.
Maybe the servo library being written for the Uno doesn't expect these pins to be present? There is no logical reason they wouldn't work unless you broke something in your code trying to switch to those pins, or the library doesn't acknowledge those pins for some reason.
gln6fgsl:
The A0 worked wonderfully. But when I was playing around with it, I noticed the program works for A0-A9, but not A10-A15. So now I'm wondering why not? I don't need it for my project, but I am curious.
Maybe the servo library being written for the Uno doesn't expect these pins to be present? There is no logical reason they wouldn't work unless you broke something in your code trying to switch to those pins, or the library doesn't acknowledge those pins for some reason.
Yes, I suspect the answer is buried in the details of the servo library as to which pins a mega can use to drive servos. The reference page only gives partial information:
The Servo library supports up to 12 motors on most Arduino boards and 48 on the Arduino Mega. On boards other than the Mega, use of the library disables analogWrite() (PWM) functionality on pins 9 and 10, whether or not there is a Servo on those pins. On the Mega, up to 12 servos can be used without interfering with PWM functionality; use of 12 to 23 motors will disable PWM on pins 11 and 12.