This circuit involves an Arduino UNO microcontroller interfacing with multiple servos and joystick modules. The Arduino UNO reads analog signals from the joysticks and controls the servos based on these inputs. Additionally, the circuit includes a 5V power connector to supply power to the components.
Arduino UNO
Servo (x7)
KY-023 Dual Axis Joystick Module (x3)
Connector 5V
#include <Servo.h>
// Declaration of servo objects
Servo servo_0, servo_1, servo_2, servo_3, servo_4, servo_5, servo_6;
// Joystick analog pins
const int joystick1_x = A0, joystick1_y = A1;
const int joystick2_x = A2, joystick2_y = A3;
const int joystick3_x = A4, joystick3_y = A5;
// Switch pins
const int switchPin1 = 9; // Switch for joystick 1
const int switchPin2 = 10; // Switch for joystick 2
const int switchPin3 = 11; // Switch for joystick 3
void setup() {
Serial.begin(9600); // Initialize serial communication
// Initialize switch pins as input with internal pull-up resistors
pinMode(switchPin1, INPUT_PULLUP);
pinMode(switchPin2, INPUT_PULLUP);
pinMode(switchPin3, INPUT_PULLUP);
// Attach servos to pins
servo_0.attach(2);
servo_1.attach(3);
servo_2.attach(4);
servo_3.attach(5);
servo_4.attach(6);
servo_5.attach(7);
servo_6.attach(8);
}
void loop() {
// Handle joystick control
handleJoystickControl();
// Handle serial input
if (Serial.available() > 0) {
handleSerialInput();
}
// Small delay to avoid overwhelming the servos
delay(20);
}
void handleJoystickControl() {
// Read the switch states
bool switchState1 = digitalRead(switchPin1) == LOW;
bool switchState2 = digitalRead(switchPin2) == LOW;
bool switchState3 = digitalRead(switchPin3) == LOW;
if (switchState1) {
// Read joystick 1 values and control servo_0 and servo_1
int servo_0_pos = map(analogRead(joystick1_x), 0, 1023, 0, 180);
int servo_1_pos = map(analogRead(joystick1_y), 0, 1023, 0, 180);
servo_0.write(servo_0_pos);
servo_1.write(servo_1_pos);
}
if (switchState2) {
// Read joystick 2 values and control servo_2 and servo_3
int servo_2_pos = map(analogRead(joystick2_x), 0, 1023, 0, 180);
int servo_3_pos = map(analogRead(joystick2_y), 0, 1023, 0, 180);
servo_2.write(servo_2_pos);
servo_3.write(servo_3_pos);
}
if (switchState3) {
// Read joystick 3 values and control servo_4, servo_5, and servo_6
int servo_4_pos = map(analogRead(joystick3_x), 0, 1023, 0, 180);
int servo_5_pos = map(analogRead(joystick3_y), 0, 1023, 0, 180);
int servo_6_pos = 180 - servo_4_pos; // Synchronize servo_6 with servo_4
servo_4.write(servo_4_pos);
servo_5.write(servo_5_pos);
servo_6.write(servo_6_pos);
}
}
void handleSerialInput() {
// Read the data string from the serial input
String input = Serial.readStringUntil('\n');
int servoIndex = input.substring(0, 1).toInt(); // Get the servo index
int servoValue = input.substring(2).toInt(); // Get the servo value
// Write the value to the corresponding servo
switch (servoIndex) {
case 1:
servo_0