Uploaded by Gintoki Sakata

Robot arm Code

advertisement
#include <Servo.h>
// Define servo objects for each servo
Servo servoX;
Servo servoY;
Servo servoPot;
// Define
const int
const int
const int
const int
const int
const int
pin numbers for each servo and joystick/potentiometer inputs
servoXPin = 9;
servoYPin = 10;
servoPotPin = 11;
joyXPin = A0;
joyYPin = A1;
potPin = A2;
// Define joystick and potentiometer values
int joyXValue = 0;
int joyYValue = 0;
int potValue = 0;
void setup() {
// Attach servo objects to their respective pins
servoX.attach(servoXPin);
servoY.attach(servoYPin);
servoPot.attach(servoPotPin);
// Set initial position of servos
servoX.write(90);
servoY.write(90);
servoPot.write(90);
}
void loop() {
// Read joystick and potentiometer values
joyXValue = analogRead(joyXPin);
joyYValue = analogRead(joyYPin);
potValue = analogRead(potPin);
// Map joystick and potentiometer values to servo angles
int servoXAngle = map(joyXValue, 0, 1023, 0, 180);
int servoYAngle = map(joyYValue, 0, 1023, 0, 180);
int servoPotAngle = map(potValue, 0, 1023, 0, 180);
// Set servo positions based on joystick and potentiometer values
servoX.write(servoXAngle);
servoY.write(servoYAngle);
servoPot.write(servoPotAngle);
// Wait a short time before reading joystick and potentiometer values again
delay(10);
}
Arduino that uses the Servo library to control 3 servos, where 2 of them are controlled by an
XY joystick and one of them is controlled by a potentiometer:
The specific pin to use for each servo and input may depend on the Arduino board
you are using. Here is a general guideline for the pin connections in the code I
provided:






Connect the X-axis of the joystick to analog input pin A0.
Connect the Y-axis of the joystick to analog input pin A1.
Connect the potentiometer to analog input pin A2.
Connect the signal wire of the servo for X-axis control to digital pin 9.
Connect the signal wire of the servo for Y-axis control to digital pin 10.
Connect the signal wire of the servo for potentiometer control to digital pin
11.
Of course, you can modify these pin assignments in the code to match the specific
pins you are using for your project. Just make sure that the pin numbers you use in
the code match the actual physical pins you have connected to on the Arduino board.
Download