Driving 2 Servo Motors with a Joy Stick issue

Hello, I am fairly new to this coding and I attempting to do the following: Drive two Servo Motors using a Inland XYB Joystick I got in a Inland Projects kit. I am also using a Arduino Mega 2560 and am trying to run currently two 9 gram servos. My worked for running one servo off of one axis the X axis. However when I attempted to add in a second Servo for the Y axis I get error. Below is the code as well as the Error message.

/*  This code is a test for a 3D printed Eye Mechanism and Eye Brow Mechanism this is my first really ever code writing Project I am using youtube.
Tutorials as well as the Stan Winston School tutorials. My Name is Bucky.
*/
// Servo1 is the First servo
#include <Servo.h> // This Command accesses a pre-exsisting Library of Code for Driving the Servo 
int ServoPin = 9; 
Servo Servo1; // This Command Creates Servo 1 X Axis
Servo Servo2; // This Command Creates Servo 2 Y Axis
int joystickyaxis = A1;// Assigns The Y axis of the joy stick to the analog in Pin to Pin 1
int joystickxaxis = A0; // Assigns  The X axis of the Joy Stick to the Analog In Pin to spot 0
void setup() {
  Servo1.attach (9); // Attaches Servo to Servo1 to PWM pin 9 for Signal
Servo2.attach (10) ; // Attaches Servo2 to PWM Pin 10 for signal 
}

void loop() {
int reading = analogRead(joystickxaxis); // tells Arduino to Read the Analog input Value From the joystickxaxis 
int angle = map (reading, 0,1023,0,180); // Tells The Arduino to convert the Analog Signal to a given rotational Degree for the servo to understand
Servo1.write(angle); // This takes the signal from the previous interger annd the angle its converted to and tells it send it to Servo1

int reading = analogRead(joystickyaxis); // Tells Arduino To read the Analog input from the joystickyaxis
int angle = map (reading, 0,1023,0,180); // Tells Arduino to convert Y axis joystick input into rotational angle for servo
Servo2.write(angle); // Takes Signal from Previous Interger sends it to the servo 
}

And for the Error code I get a "Compilation Error: Redeclaraction of 'int reading'


C:\Users\BuckyL\Documents\Arduino\sketch_Printed_Eye_Mech_Test\sketch_Printed_Eye_Mech_Test.ino: In function 'void loop()':
C:\Users\BuckyL\Documents\Arduino\sketch_Printed_Eye_Mech_Test\sketch_Printed_Eye_Mech_Test.ino:21:5: error: redeclaration of 'int reading'
 int reading = analogRead(joystickxaxis); // tells Arduino to Read the Analog input Value From the joystickxaxis
     ^~~~~~~
C:\Users\BuckyL\Documents\Arduino\sketch_Printed_Eye_Mech_Test\sketch_Printed_Eye_Mech_Test.ino:17:7: note: 'int reading' previously declared here
   int reading = analogRead(joystickyaxis); // Tells Arduino To read the Analog input from the joystickyaxis
       ^~~~~~~
C:\Users\BuckyL\Documents\Arduino\sketch_Printed_Eye_Mech_Test\sketch_Printed_Eye_Mech_Test.ino:22:5: error: redeclaration of 'int angle'
 int angle = map (reading, 0,1023,0,180); // Tells The Arduino to convert the Analog Signal to a given rotational Degree for the servo to understand
     ^~~~~
C:\Users\BuckyL\Documents\Arduino\sketch_Printed_Eye_Mech_Test\sketch_Printed_Eye_Mech_Test.ino:18:5: note: 'int angle' previously declared here
 int angle = map (reading, 0,1023,0,180); // Tells Arduino to convert Y axis joystick input into rotational angle for servo
     ^~~~~

exit status 1

Compilation error: redeclaration of 'int reading'

If anyone can explain me what is going on coding wise as well as give me a solution I will be greatly appreciative of that.

The error message is self explanatory. Within a function or elsewhere, declare a variable only once. In this case, that means drop the "int" the next time you use the variable.

Just in case: avoid making the common beginner mistake of trying to use the Mega 5V output to power servos. Use a separate 4.8 to 6V power supply, capable of providing at least 2 Amperes, and connect the grounds.

I thank you for such a quick response. I will look more into your power supply suggestion.

A 4xAA alkaline battery pack with fresh cells should be able to handle two micro servos. For larger servos like the MG996R, a power supply capable of providing 2.5 Amperes per servo is required.

You WILL damage the Arduino, and possibly even the computer it is connected to, if you try to use the 5V output for such purposes. Avoid all tutorials that suggest it is OK to do so.

so, powering two 9 Gram Servos using the on board Arduino 5V power supply will damage the Mega board? By the End of my project I intend to power about 10 9 gram servos.

Yes, you will damage the Arduino doing that.

The Arduino is designed to provide power for 1-2 sensor modules and a couple of LEDs (obey the Arduino manufacturer's recommendations).

Servos take far more current, and they generate severe electrical noise that causes resets and other malfunctions. For 10 small servos, experienced users plan on a 5V, 10 Ampere (50 watt) power supply.

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.