// Arduino Mega 2560 Code for Robot Gripper Arm
// with 2 Joysticks, 3 Servos, and 1 Stepper
// Libraries
#include <Servo.h>
#include <AccelStepper.h>
// --- Pin Definitions ---
// Joystick 1 (X and Y Axis)
const int joy1XPin = A0;
const int joy1YPin = A1;
// Joystick 2 (X and Y Axis)
const int joy2XPin = A2;
const int joy2YPin = A3;
// Servo 1 (Shoulder Joint)
const int servo1Pin = 9;
// Servo 2 (Elbow Joint)
const int servo2Pin = 10;
// Servo 3 (Gripper)
const int servo3Pin = 11;
// Stepper Motor (Base Rotation)
const int stepPin = 5;
const int dirPin = 6;
// --- Global Variables ---
// Servo Objects
Servo servo1;
Servo servo2;
Servo servo3;
// Stepper Motor Object
AccelStepper stepper(AccelStepper::DRIVER, stepPin, dirPin);
// Joystick Readings
int joy1XValue;
int joy1YValue;
int joy2XValue;
int joy2YValue;
// Servo Positions
int servo1Pos;
int servo2Pos;
int servo3Pos;
// Stepper Position
long stepperPos = 0;
// --- Constants ---
// Joystick Calibration Values
const int joy1XCenter = 512;
const int joy1YCenter = 512;
const int joy2XCenter = 512;
const int joy2YCenter = 512;
// Joystick Dead Zone
const int joyDeadZone = 50;
// Servo Position Limits
const int servo1Min = 0;
const int servo1Max = 180;
const int servo2Min = 0;
const int servo2Max = 180;
const int servo3Min = 0;
const int servo3Max = 180;
// Stepper Control
const int stepperMin = -1000; // Adjust as needed for your setup
const int stepperMax = 1000; // Adjust as needed for your setup
const int stepperSpeed = 500;
const int stepperAccel = 200;
// --- Timer Variables ---
unsigned long previousMillis = 0; // Store the last time the joystick was read
const long interval = 20; // Interval at which to read joystick and update (milliseconds)
// --- Setup Function ---
void setup() {
// Initialize Serial Communication
Serial.begin(9600);
// Attach Servo Objects to Pins
servo1.attach(servo1Pin);
servo2.attach(servo2Pin);
servo3.attach(servo3Pin);
// Set Stepper Motor Speed and Acceleration
stepper.setMaxSpeed(stepperSpeed);
stepper.setAcceleration(stepperAccel);
// Initialize Servo Positions
servo1.write(90);
servo2.write(90);
servo3.write(90);
// Initialize Stepper to a known position (e.g., center)
stepper.setCurrentPosition(0);
}
// --- Loop Function ---
void loop() {
// --- Timer Logic ---
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
// Save the last time the update was performed
previousMillis = currentMillis;
// --- Read Joystick Values ---
joy1XValue = analogRead(joy1XPin);
joy1YValue = analogRead(joy1YPin);
joy2XValue = analogRead(joy2XPin);
joy2YValue = analogRead(joy2YPin);
// --- Control Stepper Motor (Base Rotation) ---
if (abs(joy1XValue - joy1XCenter) > joyDeadZone) {
stepperPos = map(joy1XValue, 0, 1023, stepperMax, stepperMin);
stepper.moveTo(stepperPos);
}
// --- Control Servo 1 (Shoulder) ---
if (abs(joy1YValue - joy1YCenter) > joyDeadZone) {
servo1Pos = map(joy1YValue, 0, 1023, servo1Max, servo1Min);
servo1.write(servo1Pos);
}
// --- Control Servo 2 (Elbow) ---
if (abs(joy2XValue - joy2XCenter) > joyDeadZone) {
servo2Pos = map(joy2XValue, 0, 1023, servo2Max, servo2Min);
servo2.write(servo2Pos);
}
// --- Control Servo 3 (Gripper) ---
if (abs(joy2YValue - joy2YCenter) > joyDeadZone) {
servo3Pos = map(joy2YValue, 0, 1023, servo3Max, servo3Min);
servo3.write(servo3Pos);
}
}
// --- Stepper Motor Control (Non-blocking) ---
stepper.run(); // This needs to be called frequently to move the stepper smoothly
}
mega:SCL
mega:SDA
mega:AREF
mega:GND.1
mega:13
mega:12
mega:11
mega:10
mega:9
mega:8
mega:7
mega:6
mega:5
mega:4
mega:3
mega:2
mega:1
mega:0
mega:14
mega:15
mega:16
mega:17
mega:18
mega:19
mega:20
mega:21
mega:5V.1
mega:5V.2
mega:22
mega:23
mega:24
mega:25
mega:26
mega:27
mega:28
mega:29
mega:30
mega:31
mega:32
mega:33
mega:34
mega:35
mega:36
mega:37
mega:38
mega:39
mega:40
mega:41
mega:42
mega:43
mega:44
mega:45
mega:46
mega:47
mega:48
mega:49
mega:50
mega:51
mega:52
mega:53
mega:GND.4
mega:GND.5
mega:IOREF
mega:RESET
mega:3.3V
mega:5V
mega:GND.2
mega:GND.3
mega:VIN
mega:A0
mega:A1
mega:A2
mega:A3
mega:A4
mega:A5
mega:A6
mega:A7
mega:A8
mega:A9
mega:A10
mega:A11
mega:A12
mega:A13
mega:A14
mega:A15
joystick1:VCC
joystick1:VERT
joystick1:HORZ
joystick1:SEL
joystick1:GND
joystick2:VCC
joystick2:VERT
joystick2:HORZ
joystick2:SEL
joystick2:GND
servo1:GND
servo1:V+
servo1:PWM
servo2:GND
servo2:V+
servo2:PWM
servo3:GND
servo3:V+
servo3:PWM
stepper1:A-
stepper1:A+
stepper1:B+
stepper1:B-
drv1:ENABLE
drv1:MS1
drv1:MS2
drv1:MS3
drv1:RESET
drv1:SLEEP
drv1:STEP
drv1:DIR
drv1:GND.1
drv1:VDD
drv1:1B
drv1:1A
drv1:2A
drv1:2B
drv1:GND.2
drv1:VMOT
Stepper Motor for Rotation
vcc1:VCC
gnd1:GND
Gripper
Elbow
Joint
Shoulder
Joint
Base and Shoulder
Elbow and Gripper