Hello, this is my first time in this thread, but I'll spare the boring details and get right to the point.
I'm trying to program 4 servos using an Arduino Uno. My goal is to control 1 set of servos (2 servos, x and y axis) with the joystick, and control the other set of servos (the other 2) while the joystick is pressed down (the joystick has a button function).
I am using a KEYES KY-023 joystick.
This is the URL to a video tutorial where I got most of the code from: Multiple Servo Motor Control with Joystick and Arduino - YouTube
After attempting to add two additional servos and the button function, I believe my code is incorrect in some way regarding the joystick button, but I'm new to this so I have no clue as to what is incorrect. Here is the code I currently have:
//add the servo library
#include <Servo.h>
//define our servos
Servo servo1;
Servo servo2;
Servo servo3;
Servo servo4;
//define joystick pins (Analog)
int joyX = 0;
int joyY = 1;
int pinZ = 2;
//variable to read the values from the analog pins
int joyVal;
int pinVal;
void setup ()
{
//attaches our servos on pins PWM 3-5-6-9
servo1.attach(3);
servo2.attach(5);
servo3.attach(6);
servo4.attach(9);
}
void loop ()
{
pinVal = analogRead(pinZ);
if (pinVal = 0)
{
//read the value of joystick (between 0-1023)
joyVal = analogRead(joyX);
joyVal = map(joyVal, 0, 1023, 0, 180); //servo value between 0-180
servo1.write(joyVal); //set the servo position according to the joystick value
joyVal = analogRead(joyY);
joyVal = map (joyVal, 0, 1023, 0, 180);
servo2.write(joyVal);
}
else
{
joyVal = analogRead(joyX);
joyVal = map(joyVal, 0, 1023, 0, 180); //servo value between 0-180
servo3.write(joyVal); //set the servo position according to the joystick value
joyVal = analogRead(joyY);
joyVal = map (joyVal, 0, 1023, 0, 180);
servo4.write(joyVal);
}
delay(15);
}
Again I'm new to Arduino and coding in general so any help is appreciated. Thank you!