// Include the necessary libraries
#include <Stepper.h> // library for stepper motor
#include <Servo.h> // library for Servo motor
Servo myServo; // create a servo object
// Define the stepper motor pins
const int stepPin = 2; // to move hand upward and downward
const int dirPin = 5; // to move hand upward and downward
const int stepPin1 = 3; // to move hand Left and Right
const int dirPin1 = 6; // to move hand Left and Right
const int stepPin2 = 4; // to contract and expand arm
const int dirPin2 = 7; // to contract and expand arm
// Define the joystick pins
const int joyXPin = A0; // to Control stepper Motor
const int joyYPin = A1; // to Control stepper Motor
const int joyXPin1 = A3; // to Control Servo Motor & to Control stepper Motor
const int joyYPin1 = A4; // to Control Servo Motor & to Control stepper Motor
// Create a Stepper object for motor which move hand upward and downward
Stepper myStepper(200, stepPin, dirPin);
// Create a Stepper object for motor which move hand Left and Right
Stepper myStepper1(200, stepPin1, dirPin1);
// Create a Stepper object for motor to contract and expand arm
Stepper myStepper2(200, stepPin2, dirPin2);
void setup() {
// Initialize the stepper motor which move hand upward and downward
myStepper.setSpeed(60);
// Initialize the stepper motor which move hand Left and Right
myStepper1.setSpeed(60);
// Initialize the stepper motor which contract and expand arm
myStepper2.setSpeed(60);
// Pin number where attach servo motor's data pin on arduino board
myServo.attach(9);
// Initialize the joystick pins as inputs for stepper Motor
pinMode(joyXPin, INPUT);
pinMode(joyYPin, INPUT);
// Initialize the joystick pins as inputs for Servo motor
pinMode(joyXPin1, INPUT);
pinMode(joyYPin1, INPUT);
// Initialize the stepper motor pins as outputs which move hand upward and downward
pinMode(stepPin, OUTPUT);
pinMode(dirPin, OUTPUT);
// Initialize the stepper motor pins as outputs which move hand Left and Right
pinMode(stepPin1, OUTPUT);
pinMode(dirPin1, OUTPUT);
// Initialize the stepper motor pins as outputs which contract and expand arm
pinMode(stepPin2, OUTPUT);
pinMode(dirPin2, OUTPUT);
Serial.begin(9600);
}
// This below function is responsible for stepper motor which move upward and downward
void MoveMotorUpwardandDownward()
{
// Read the joystick values for stepper motor
int joyX = analogRead(joyXPin);
if(joyX > 520)
{
// Set the direction of the stepper motor
digitalWrite(dirPin, HIGH); // Clockwise
// Move the stepper motor 1 steps
for (int i = 0; i < 1; i++) {
digitalWrite(stepPin, HIGH);
delayMicroseconds(5);
digitalWrite(stepPin, LOW);
delayMicroseconds(5);
}
}
else if((joyX <520)&&(joyX<300))
{
// Change the direction of the stepper motor
digitalWrite(dirPin, LOW); // Counterclockwise
// Move the stepper motor 1 steps
for (int i = 0; i < 1; i++) {
digitalWrite(stepPin, HIGH);
delayMicroseconds(5);
digitalWrite(stepPin, LOW);
delayMicroseconds(5);
}
}
else
{
digitalWrite(stepPin, LOW);
}
}
void ContractAndExpandMotor()
{
int joyY1 = analogRead(joyYPin1);
if(joyY1 > 520)
{
digitalWrite(dirPin2, HIGH); // Clockwise
for (int i = 0; i < 1; i++) {
digitalWrite(stepPin2, HIGH);
delayMicroseconds(5);
digitalWrite(stepPin2, LOW);
delayMicroseconds(5);
}
}
else if((joyY1 < 520)&&(joyY1<300))
{
digitalWrite(dirPin2, LOW); // Counterclockwise
for (int i = 0; i < 1; i++) {
digitalWrite(stepPin2, HIGH);
delayMicroseconds(5);
digitalWrite(stepPin2, LOW);
delayMicroseconds(5);
}
}
else
{
digitalWrite(stepPin2, LOW);
}
}
// This below function is responsible for stepper motor which move left and right
void MoveMotorLeftandRight()
{
int joyY = analogRead(joyYPin);
if(joyY > 535)
{
// Set the direction of the stepper motor
digitalWrite(dirPin1, HIGH); // Clockwise
// Move the stepper motor 100 steps
for (int i = 0; i < 1; i++) {
digitalWrite(stepPin1, HIGH);
delayMicroseconds(5);
digitalWrite(stepPin1, LOW);
delayMicroseconds(5);
}
}
else if((joyY <535)&&(joyY<300))
{
// Change the direction of the stepper motor
digitalWrite(dirPin1, LOW); // Counterclockwise
// Move the stepper motor 100 steps
for (int i = 0; i < 1; i++) {
digitalWrite(stepPin1, HIGH);
delayMicroseconds(5);
digitalWrite(stepPin1, LOW);
delayMicroseconds(5);
}
}
else
{
digitalWrite(stepPin1, LOW);
}
}
// Responsible for servo Motor's movement
void MoveServoMotor()
{
// Responsible for servo movement
int joyX1 = analogRead(joyXPin1);
int servoPos = map(joyX1, 0, 1023, 0, 180); // map the joystick value to a servo position
myServo.write(servoPos); // set the servo position
delay(15); // wait 15ms for the servo to reach the position
}
void ShowStepperMotorJoystickValue()
{
int joyX = analogRead(joyXPin);
int joyY = analogRead(joyYPin);
Serial.print("The value of X-axis Stepper Motor JoyStick is: ");
Serial.println(joyX); // Print the value of joyX, not joyXPin
Serial.print("The value of Y-axis Stepper Motor JoyStick is: ");
Serial.println(joyY); // Print the value of joyY, not joyYPin
}
void ShowServoMotorJoyStickValues()
{
// You haven't defined joyX1Pin and joyY1Pin, so I assume they should be joyXPin and joyYPin
int joyX1 = analogRead(joyXPin1);
int joyY1 = analogRead(joyYPin1);
Serial.print("The value of X-axis Servo Motor JoyStick is: ");
Serial.println(joyX1); // Print the value of joyX
Serial.print("The value of Y-axis Servo Motor JoyStick is: ");
Serial.println(joyY1); // Print the value of joyY
}
void loop()
{
MoveMotorUpwardandDownward();
MoveMotorLeftandRight();
MoveServoMotor();
ContractAndExpandMotor();
ShowStepperMotorJoystickValue();
ShowServoMotorJoyStickValues();
}