Hi Arduino community,
I wrote this sketch the move a stepper with a laser on it. I used 32 if statements to get this working.
Is there a way to do this on a less time consuming and a more elegant way the next time?
Kind regards,
MB
#include <Stepper.h>
#define STEPS 2038 // the number of steps in one revolution of your motor (28BYJ-48)
Stepper stepper(STEPS, 8, 10, 9, 11);
#define JOY_X A0
#define JOY_PUSH 3
int X_VALUE;
bool PUSH=false;
void setup() {
Serial.begin(9600);
pinMode(JOY_PUSH,INPUT_PULLUP);
}
void loop() {
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> Joystick PUSH BUTTON
if(!digitalRead(JOY_PUSH) & !PUSH )
{
PUSH=true;
delay(200);
}
if(!digitalRead(JOY_PUSH) & PUSH )
{
PUSH=false;
delay(200);
}
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> JOYStick X axis clockwise
X_VALUE=analogRead(JOY_X);
//1
if(X_VALUE >= 520 && X_VALUE < 551 & !PUSH )
{
stepper.setSpeed(1);
stepper.step(1);
}
//2
if(X_VALUE >= 551 && X_VALUE < 582 & !PUSH)
{
stepper.setSpeed(2);
stepper.step(1);
}
//3
if(X_VALUE >= 582 && X_VALUE < 613 & !PUSH)
{
stepper.setSpeed(3);
stepper.step(1);
}
//4
if(X_VALUE >= 613 && X_VALUE < 644 & !PUSH)
{
stepper.setSpeed(4);
stepper.step(1);
}
//5
if(X_VALUE >= 644 && X_VALUE < 675 & !PUSH)
{
stepper.setSpeed(5);
stepper.step(1);
}
//6
if(X_VALUE >= 675 && X_VALUE < 706 & !PUSH)
{
stepper.setSpeed(6);
stepper.step(1);
}
//7
if(X_VALUE >= 706 && X_VALUE < 737 & !PUSH)
{
stepper.setSpeed(7);
stepper.step(1);
}
//8
if(X_VALUE >= 737 && X_VALUE < 768 & !PUSH)
{
stepper.setSpeed(8);
stepper.step(1);
}
//9
if(X_VALUE >= 768 && X_VALUE < 799 & !PUSH)
{
stepper.setSpeed(9);
stepper.step(1);
}
//10
if(X_VALUE >= 799 && X_VALUE < 830 & !PUSH)
{
stepper.setSpeed(10);
stepper.step(1);
}
//11
if(X_VALUE >= 830 && X_VALUE < 861 & !PUSH)
{
stepper.setSpeed(11);
stepper.step(1);
}
//12
if(X_VALUE >= 861 && X_VALUE < 892 & !PUSH)
{
stepper.setSpeed(12);
stepper.step(1);
}
//13
if(X_VALUE >= 892 && X_VALUE < 923 & !PUSH)
{
stepper.setSpeed(13);
stepper.step(1);
}
//14
if(X_VALUE >= 923 && X_VALUE < 954 & !PUSH)
{
stepper.setSpeed(14);
stepper.step(1);
}
//15
if(X_VALUE >= 954 && X_VALUE < 985 & !PUSH)
{
stepper.setSpeed(15);
stepper.step(1);
}
//16
if(X_VALUE >= 985 && X_VALUE < 1023 & !PUSH)
{
stepper.setSpeed(16);
stepper.step(1);
}
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> JOYStick X axis counter-clockwise
//1
if(X_VALUE <= 506 && X_VALUE > 475 & !PUSH)
{
stepper.setSpeed(1);
stepper.step(-1);
}
//2
if(X_VALUE <= 475 && X_VALUE > 444 & !PUSH)
{
stepper.setSpeed(2);
stepper.step(-1);
}
//3
if(X_VALUE <= 444 && X_VALUE > 413 & !PUSH)
{
stepper.setSpeed(3);
stepper.step(-1);
}
//4
if(X_VALUE <= 413 && X_VALUE > 382 & !PUSH)
{
stepper.setSpeed(4);
stepper.step(-1);
}
//5
if(X_VALUE <= 382 && X_VALUE > 351 & !PUSH)
{
stepper.setSpeed(5);
stepper.step(-1);
}
//6
if(X_VALUE <= 351 && X_VALUE > 320 & !PUSH)
{
stepper.setSpeed(6);
stepper.step(-1);
}
//7
if(X_VALUE <= 320 && X_VALUE > 289 & !PUSH)
{
stepper.setSpeed(7);
stepper.step(-1);
}
//8
if(X_VALUE <= 289 && X_VALUE > 258 & !PUSH)
{
stepper.setSpeed(8);
stepper.step(-1);
}
//9
if(X_VALUE <= 258 && X_VALUE > 227 & !PUSH)
{
stepper.setSpeed(9);
stepper.step(-1);
}
//10
if(X_VALUE <= 227 && X_VALUE > 196 & !PUSH)
{
stepper.setSpeed(10);
stepper.step(-1);
}
//11
if(X_VALUE <= 196 && X_VALUE > 165 & !PUSH)
{
stepper.setSpeed(11);
stepper.step(-1);
}
//12
if(X_VALUE <= 165 && X_VALUE > 134 & !PUSH)
{
stepper.setSpeed(12);
stepper.step(-1);
}
//13
if(X_VALUE <= 134 && X_VALUE > 103 & !PUSH)
{
stepper.setSpeed(13);
stepper.step(-1);
}
//14
if(X_VALUE <= 103 && X_VALUE > 72 & !PUSH)
{
stepper.setSpeed(14);
stepper.step(-1);
}
//15
if(X_VALUE <= 72 && X_VALUE > 41 & !PUSH)
{
stepper.setSpeed(15);
stepper.step(-1);
}
//16
if(X_VALUE <= 41 & !PUSH & !PUSH)
{
stepper.setSpeed(16);
stepper.step(-1);
}
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> Joystick slowest movement when button is pushed
if(X_VALUE >= 737 & PUSH )
{
stepper.setSpeed(1);
stepper.step(1);
delay(150);
}
if(X_VALUE <= 289 & PUSH )
{
stepper.setSpeed(1);
stepper.step(-1);
delay(150);
}
//Serial.println(X_VALUE);
}