// Created by: Nader Mostafa
// Youtube channel: Arabic Arduino lessons
#include <Servo.h>
#define SERVO_X_PIN 2 // Arduino pin connected to Servo motor 1
#define SERVO_Y_PIN 3 // Arduino pin connected to Servo motor 2
#define VRX_PIN A0 // Arduino pin connected to VRX pin
#define VRY_PIN A1 // Arduino pin connected to VRY pin
Servo xServo; // create servo object to control a servo 1
Servo yServo;
const int XStepPin=5; // Connect X Step Pin with Ardunio
const int XDirPin=2; // Connect X Direction Pin with Ardunio
int VRX=A0;
int VRY=A1;
int SW=A5;
void setup() {
pinMode(XStepPin,OUTPUT); // identify Pin Mode (INPUT or OUTPU)
pinMode(XDirPin,OUTPUT); // identify Pin Mode (INPUT or OUTPU)
pinMode(VRX,INPUT);
pinMode(VRY,INPUT);
pinMode(SW,INPUT);
Serial.begin(9600) ;
xServo.attach(SERVO_X_PIN);
yServo.attach(SERVO_Y_PIN);
}
void loop() {
int xValue = analogRead(VRX_PIN);
int yValue = analogRead(VRY_PIN);
int xAngle = map(xValue, 0, 1023, 0, 180); // scale it to the servo's angle (0 to 180)
int yAngle = map(yValue, 0, 1023, 0, 180); // scale it to the servo's angle (0 to 180)
xServo.write(xAngle); // rotate servo motor 1
yServo.write(yAngle); // rotate servo motor 2
int Xvalue = analogRead(VRX);
int Yvalue = analogRead(VRY);
if(Xvalue<800){
digitalWrite(XDirPin,HIGH); // High Signal to rotat the stepper motor clockwise
digitalWrite(XStepPin,HIGH); // High Signal for the pulse
delayMicroseconds(500); // Waiting time between each pulse
digitalWrite(XStepPin,LOW); // LOW Signal for the pulse
delayMicroseconds(500); // Waiting time between each pulse
}
if (Xvalue>200){
digitalWrite(XDirPin,LOW); // LOW Signal to rotat the stepper motor Counter clockwise
digitalWrite(XStepPin,HIGH); // High Signal for the pulse
delayMicroseconds(500); // Waiting time between each pulse
digitalWrite(XStepPin,LOW); // LOW Signal for the pulse
delayMicroseconds(500); // Waiting time between each pulse
}
}