#include <IFX9201_XMC1300_StepperMotor.h>
#define DIR_PIN IFX9201 STEPPERMOTOR_STD_DIR // Pin 9 is standard DIR Pin
#define STP_PIN IFX9201 STEPPERMOTOR_STD_STP // Pin 10 is standard STP Pin
#define DIS_PIN IFX9201 STEPPERMOTOR_STD_DIS // Pin 11 is standard DIS Pin
const int StepsPerRevolution = 200; // počet kroku motoru na otáčku
// Stepper motor object
Stepper_motor MyStepperMotor = Stepper_motor(StepsPerRevolution, DIR_PIN, STP_PIN, DIS_PIN);
volatile boolean Turn_detected; // Interrupts
volatile boolean Rotation_direction; // CW nebo CCW rotace
//--Rotary encoder
const int CLK_pin = 2;
const int DT_pin = 3;
const int SW_pin = 13;
int direction; // CW nebo CCW rotace
int Kroky = 0;
//----------------------------
void Rotary_detect () {
delay(50);
if (digitalRead(CLK_pin)){
Rotation_direction = digitalRead(DT_pin);
} else {
Rotation_direction = digitalRead(DT_pin);
Turn_detected = true;
}
}
void setup() {
Serial.begin(9600);
MyStepperMotor.begin();
MyStepperMotor.setSpeed(120); // rychlost na 10 ot/min: 125 max
MyStepperMotor.reset_motor(); //reset motor
pinMode (CLK_pin, INPUT);
pinMode (DT_pin, INPUT);
pinMode (SW_pin, OUTPUT);
digitalWrite(SW_pin, HIGH);
attachInterrupt (0, Rotary_detect, FALLING);
}
void loop() {
if (!(digitalRead(SW_pin))) {
MyStepperMotor.begin();
MyStepperMotor.setSpeed(100);
MyStepperMotor.step(Kroky);
delay(Kroky + 500);
Kroky = 0;
MyStepperMotor.end();
}
if (Turn_detected) {
Turn_detected = false;
if (Rotation_direction) {
Kroky = Kroky - 20;
}
if (!Rotation_direction) {
Kroky = Kroky + 20;
}
MyStepperMotor.end();
}
}