0% found this document useful (0 votes)
4 views2 pages

#Include Servo

This document contains an Arduino sketch for controlling a robotic arm with 4 or 5 degrees of freedom (DOF) using a joystick. It initializes servo motors, reads joystick inputs, and allows mode switching to control different pairs of servos based on joystick movements. The code includes provisions for button debouncing and angle constraints for smooth operation.

Uploaded by

abdinc99
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views2 pages

#Include Servo

This document contains an Arduino sketch for controlling a robotic arm with 4 or 5 degrees of freedom (DOF) using a joystick. It initializes servo motors, reads joystick inputs, and allows mode switching to control different pairs of servos based on joystick movements. The code includes provisions for button debouncing and angle constraints for smooth operation.

Uploaded by

abdinc99
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

#include <Servo.

h>

// Define the number of degrees of freedom (set to 4 or 5)


#define DOF 5 // Change to 4 for a 4-DOF arm

// Calculate the number of modes (each mode controls up to 2 joints)


const int modes = (DOF + 1) / 2;

// Servo pin assignments (PWM pins)


const int servoPins[DOF] = {3, 5, 6, 9, 10}; // For DOF=5; use {3, 5, 6, 9} for DOF=4

// Joystick pin assignments


const int joyX = A0; // X-axis (analog)
const int joyY = A1; // Y-axis (analog)
const int joyButton = 2; // Button (digital)

// Servo objects and angle tracking


Servo servos[DOF];
int angles[DOF];

// Mode and button state variables


int currentMode = 0;
int lastButtonState = HIGH;

void setup() {
// Initialize servos and set initial positions
for (int i = 0; i < DOF; i++) {
servos[i].attach(servoPins[i]);
angles[i] = 90; // Start at mid-position
servos[i].write(angles[i]);
}

// Configure joystick button pin with internal pull-up resistor


pinMode(joyButton, INPUT_PULLUP);
}

void loop() {
// Read joystick inputs
int xVal = analogRead(joyX);
int yVal = analogRead(joyY);
int buttonState = digitalRead(joyButton);

// Switch modes when button is pressed


if (buttonState == LOW && lastButtonState == HIGH) {
currentMode = (currentMode + 1) % modes;
delay(50); // Simple debounce
}
lastButtonState = buttonState;

// Determine which servos to control based on current mode


int servoIndex1 = currentMode * 2; // First servo in the pair
int servoIndex2 = currentMode * 2 + 1; // Second servo in the pair

// Joystick parameters
const int center = 512; // Approximate center value of joystick
const int deadZone = 100; // Dead zone to prevent drift
const int maxSpeed = 5; // Maximum angle change per loop

// Control first servo with X-axis


if (servoIndex1 < DOF) {
if (xVal < center - deadZone) {
int speed = map(xVal, 0, center - deadZone, -maxSpeed, 0);
angles[servoIndex1] += speed;
} else if (xVal > center + deadZone) {
int speed = map(xVal, center + deadZone, 1023, 0, maxSpeed);
angles[servoIndex1] += speed;
}
angles[servoIndex1] = constrain(angles[servoIndex1], 0, 180);
servos[servoIndex1].write(angles[servoIndex1]);
}

// Control second servo with Y-axis, if it exists


if (servoIndex2 < DOF) {
if (yVal < center - deadZone) {
int speed = map(yVal, 0, center - deadZone, -maxSpeed, 0);
angles[servoIndex2] += speed;
} else if (yVal > center + deadZone) {
int speed = map(yVal, center + deadZone, 1023, 0, maxSpeed);
angles[servoIndex2] += speed;
}
angles[servoIndex2] = constrain(angles[servoIndex2], 0, 180);
servos[servoIndex2].write(angles[servoIndex2]);
}

delay(20); // Control loop speed for smooth movement


}

You might also like