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.