#include // define stepper motor control pins #define step_pin_X 7 // Atmega pin PD7(11) connected to Step pin TMC2209 #define dir_pin_X 6 // Atmega pin PD6(10) connected to Dir pin TMC2209 #define joystick_X 18 // Atmega pin PC4(27) connected to joystick Rx #define step_pin_Y 8 // Atmega pin PB0(12) connected to Step pin TMC2209 #define dir_pin_Y 9 // Atmega pin PB1(13) connected to Dir pin TMC2209 #define joystick_Y 19 // Atmega pin PC5(28) connected to joystick Ry #define LED 5 // Status LED #define stepsPerRevolution 25600 //400 steps*64 =25600 stepsPerRevolution int direction; // Variable to set Rotation (CW-CCW) of the motor int steps = 25600; // ZERO position, should be in the middle Stepper stepperX(stepsPerRevolution, step_pin_X, dir_pin_X); Stepper stepperY(stepsPerRevolution, step_pin_Y, dir_pin_Y); void setup() { pinMode(step_pin_X, OUTPUT); pinMode(dir_pin_X, OUTPUT); pinMode(LED, OUTPUT); pinMode(step_pin_Y, OUTPUT); pinMode(dir_pin_Y, OUTPUT); } void loop() { int val = analogRead(joystick_X);// read analog value from the potentiometer Rx int val1 = analogRead(joystick_Y);// read analog value from the potentiometer Ry digitalWrite(LED, HIGH); // turn the LED on if ( (val > 500) && (val < 300) ) //stop the motor if the joystick is in the middle { digitalWrite(step_pin_X, LOW); digitalWrite(dir_pin_X, LOW); } else { while (val >= 550) // move motor X in the first direction { digitalWrite(LED, LOW); // turn the LED on (HIGH is the voltage level) int speed_ = map(val, 523, 1023, 1, 10); // map the speed between 1 and 10 rpm stepperX.setSpeed(speed_); // set motor speed stepperX.step(100); // direction 1 val = analogRead(joystick_X); } while (val <= 350) // move motor X in the other direction { digitalWrite(LED, LOW); // LED status // map the speed between 1 and 10 rpm int speed_ = map(val, 500, 0, 1, 10); // set motor speed stepperX.setSpeed(speed_); stepperX.step(-100); // direction 2 val = analogRead(joystick_X); } } if ( (val1 > 500) && (val1 < 300) ) //stop the motor if the joystick is in the middle { digitalWrite(step_pin_Y, LOW); digitalWrite(dir_pin_Y, LOW); } else { while (val1 >= 550) // move motor Y in the first direction { digitalWrite(LED, LOW); // LED status // map the speed between 1 and 10 rpm int speed_ = map(val1, 523, 1023, 1, 10); // set motor speed stepperY.setSpeed(speed_); stepperY.step(100); val1 = analogRead(joystick_Y); } while (val1 <= 350) // move motor Y in the other direction { digitalWrite(LED, LOW); // LED status // map the speed between 1 and 10 rpm int speed_ = map(val1, 500, 0, 1, 10); // set motor speed stepperY.setSpeed(speed_); // move the motor (1 step) stepperY.step(-100); val1 = analogRead(joystick_Y); } } }