Go Down

Topic: problem controlling servo's with analog pins on arduino Mega 2560 (Read 1 time) previous topic - next topic

chri291

I am troubled trying to control 4 servo's using analog pins. When i write up some code to control them the pin stays inactive. For example in the code

Code: [Select]
#include <Servo.h>

Servo x1;
Servo x2;
Servo x3;
Servo x4;
void setup() {
  // put your setup code here, to run once:
x1.attach(A12);
x2.attach(A13);
x3.attach(A14);
x4.attach(A15);
}

void loop() {
  // put your main code here, to run repeatedly:
x1.write(25);
x2.write(90);
x3.write(50);
x4.write(90);
}


all of the analog pins remain unchanged. I have tried using the digital pin equivalent for them(meaning pin A12 would become 66) but it still didn't work.
The analog pins still work since i tried giving them manual orders to see if they react.

CrossRoads

Do you need something like  Servo.begin(); in setup()?
Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

slipstick

servo.attach() followed by servo.write() works for me but I've only ever used it with ordinary digital pins.

OTOH I'm so new to this I didn't even know the analog input pins could be reconfigured for digital output.

Steve

ardy_guy

But you can't really tell from that code if any of the servos are being controlled, since you never move them.

For testing, perhaps move only servo x1 in loop() with a delay between like so:


Code: [Select]

void loop() {
  // put your main code here, to run repeatedly:
x1.write(25);
delay(100);
x1.write(90);
delay(100);
x1.write(50);
delay(100);
x1.write(90);
delay(100);
}



chri291

After some more testing i discovered that for this piece of code
Code: [Select]
#include <Servo.h>

Servo mc;

void setup() {
  // put your setup code here, to run once:
mc.attach(A12);
Serial.begin(9600);
}

void loop() {
  // put your main code here, to run repeatedly:
mc.write(90);
delay(500);
mc.write(50);
delay(500);
}

A servo connected to pin 2 (not pin A12 as i attached it to) will move according to commands given. I have no idea what to think of this

Go Up