#include <Stepper.h>
// Cisla pinu pro Joystick
#define Joy_X A1 // Arduino pin connected to VRX pin
#define Joy_Y A2 // Arduino pin connected to VRY pin
const int dirPin = 2;
const int stepPin = 3;
const int stepsPerRevolution = 100;
int xValue = 0; // To store value of the X axis
int yValue = 0; // To store value of the Y axis
int val; // variable to read the value from the analog pin
int previous = 0;
int Pval = 0;
int potVal = 0;
//const int stepsPerRevolution = 100; // change this to fit the number of steps per revolution
// for your motor
// initialize the stepper library on pins 8 through 11:
Stepper myStepper(stepsPerRevolution, 8, 9, 10, 11);
void setup() {
pinMode(stepPin, OUTPUT);
pinMode(dirPin, OUTPUT);
// set the speed at 60 rpm:
myStepper.setSpeed(60);
// initialize the serial port:
Serial.begin(9600) ;
digitalWrite(dirPin,HIGH);
}
void loop() {
//Joystick_reader();
//step_one_revolution();
//Step_motor_A4988();
//Position_with_Joystick();
//Step_motor_A4988_position_with_Joystick();
Step_motor_A4988_position_with_Joystick2();
}
void Joystick_reader() {
// read analog X and Y analog values
xValue = analogRead(Joy_X);
yValue = analogRead(Joy_Y);
// print data to Serial Monitor
Serial.print("x = ");
Serial.print(xValue);
Serial.print(", y = ");
Serial.println(yValue);
delay(200);
}
void step_one_revolution() {
// step one revolution in one direction:
Serial.println("clockwise");
myStepper.step(stepsPerRevolution);
delay(500);
// step one revolution in the other direction:
Serial.println("counterclockwise");
myStepper.step(-stepsPerRevolution);
delay(500);
}
void Position_with_Joystick() {
val = analogRead(Joy_X); // reads the value of the potentiometer (value between 0 and 1023)
val = map(val, 1024, 0, -90, 90); // scale it to use it with the servo (value between 0 and 180)
myStepper.step(val - previous); // sets the servo position according to the scaled value
previous = val;
}
void Step_motor_A4988() {
// Set motor direction clockwise
digitalWrite(dirPin, HIGH);
// Spin motor slowly
for(int x = 0; x < stepsPerRevolution; x++)
{
digitalWrite(stepPin, HIGH);
delayMicroseconds(2000);
digitalWrite(stepPin, LOW);
delayMicroseconds(2000);
Serial.println("Motor+");
}
delay(1000); // Wait a second
// Set motor direction counterclockwise
digitalWrite(dirPin, LOW);
// Spin motor quickly
for(int x = 0; x < stepsPerRevolution; x++)
{
digitalWrite(stepPin, HIGH);
delayMicroseconds(1000);
digitalWrite(stepPin, LOW);
delayMicroseconds(1000);
Serial.println("Motor-");
}
delay(1000); // Wait a second
}
void Step_motor_A4988_position_with_Joystick() {
val = analogRead(Joy_X);
val = map(val, 1024, 0, -90, 90); // scale it to use it with the servo (value between 0 and 180)
xValue = analogRead(Joy_X);
if (xValue > 600){
Step_motor_counterclockwise();
}
else if(xValue < 400){
//val = analogRead(Joy_X);
//val = map(val, 500, 0, 0, 90);
Step_motor_clockwise();
}
previous = val;
}
void Step_motor_A4988_position_with_Joystick2() {
//val = analogRead(Joy_X);
//val = map(val, 1024, 0, -90, 90); // scale it to use it with the servo (value between 0 and 180)
potVal = map(analogRead(A1),0,1024,-90,90);
if (potVal>Pval)
Step_motor_counterclockwise();
if (potVal<Pval)
Step_motor_clockwise();
Pval = potVal;
Serial.println(Pval);
}
// KROKY STEP motoru
void Step_motor_clockwise() {
// Set motor direction clockwise
digitalWrite(dirPin, HIGH);
// Spin motor slowly
for(int x = 0; x < stepsPerRevolution; x++) //(val - previous)
{
digitalWrite(stepPin, HIGH);
delayMicroseconds(1000);
digitalWrite(stepPin, LOW);
delayMicroseconds(1000);
Serial.println("Motor+");
}
}
void Step_motor_counterclockwise() {
// Set motor direction counterclockwise
digitalWrite(dirPin, LOW);
// Spin motor quickly
for(int x = 0; x < stepsPerRevolution; x++)
{
digitalWrite(stepPin, HIGH);
delayMicroseconds(1000);
digitalWrite(stepPin, LOW);
delayMicroseconds(1000);
Serial.println("Motor-");
}
}