#include<Stepper.h>
// define stepper motor control pins
#define IN1 2
#define IN2 3
#define IN3 5
#define IN4 6
int STEPS=2048;
// initialize stepper library
Stepper stepper(STEPS, 2, 5, 3, 6);
//Servo
#include <Servo.h>
Servo Xservo;
int Xpin=A0;
int Ypin=A1;
int XSpin=13;
int WVx;
int WVy;
int Xval;
int Yval;
int dt=600;
//servo end
void setup()
{
Serial.begin(9600);
//Servo
// put your setup code here, to run once:
pinMode(Xpin,INPUT);
pinMode(Ypin,INPUT);
pinMode(XSpin,OUTPUT);
Xservo.attach(XSpin);
//Servo End
}
void loop()
{
// read analog value from the potentiometer
int val = analogRead(Ypin);
// if the joystic is in the middle ===> stop the motor
if( (val > 495) && (val < 520) )
{
digitalWrite(IN1, LOW);
digitalWrite(IN2, LOW);
digitalWrite(IN3, LOW);
digitalWrite(IN4, LOW);
}
else
{
// move the motor in the first direction
while (val >= 520)
{
// map the speed between 1 and 17 rpm
int speed_ = map(val, 520, 1023, 1, 17);
// set motor speed
stepper.setSpeed(speed_);
// move the motor (1 step)
stepper.step(1);
val = analogRead(Ypin);
}
// move the motor in the other direction
while (val <= 495)
{
// map the speed between 1 and 17 rpm
int speed_ = map(val, 495, 0, 1, 17);
// set motor speed
stepper.setSpeed(speed_);
// move the motor (1 step)
stepper.step(-1);
val = analogRead(Ypin);
}
}
//Servo
Xval=analogRead(Xpin);
Xval=map(Xval,0,1023,0,180);
Xservo.write(Xval);
Serial.print("Y Axis ");
Serial.println(val);
Serial.print("x Axis ");
Serial.println(Xval);
Serial.print("X axis volts");
Serial.println(Xval*(1023/180));
delay(600);
}