//Im begginer its not perfect (Im sure).
//ask me / correct me on discord: danonimo22
//My projects:
//https://wokwi.com/projects/400223603331630081 - IRREMOTE stepper motor
//https://wokwi.com/projects/399410650756388865 - Stepper motor buttons
//https://wokwi.com/projects/400972915874953217 - Omori test image display
//https://wokwi.com/projects/401035577171755009 - Omori "game"
//Controls stepper motor by holding down two buttons clockwise or counterclockwise (left, right)
//You can adjust speed, acceleration and amount of steps for controling positions
//You cant press both buttons at the same time since not good (I think)
//You also have LEDs as an indication what is happening
//You should probably include the sleep mode if you dont using it often
#include <AccelStepper.h>
#include <ezButton.h>
#define butPinLeft 5
#define butPinRight 6
#define dirPin 2
#define stepPin 3
#define motorInterfaceType 1
#define stepLimit 100
#define speed 1000
#define accel 1000
ezButton buttonLeft(butPinLeft);
ezButton buttonRight(butPinRight);
AccelStepper stepper = AccelStepper(motorInterfaceType, stepPin, dirPin);
long step = 0;
void setup() {
uint8_t pinLEDS[3]{10,11,12};
for(uint8_t i = 0; i < 3; i++){
pinMode(i, OUTPUT);
digitalWrite(i, 1);
}
stepper.setMaxSpeed(speed); //max motor speed
stepper.setAcceleration(accel); //max motor accelaratin
buttonLeft.setDebounceTime(35); //wait before next press
buttonRight.setDebounceTime(35);
}
void loop() {
buttonLeft.loop();
buttonRight.loop();
//left and right limitation
if(buttonRight.isPressed() && step == 0) digitalWrite(10, 1);
if(buttonLeft.isPressed() && step == stepLimit) digitalWrite(10, 1);
else if(buttonLeft.isReleased() || buttonRight.isReleased()) digitalWrite(10, 0);
//red, right control
if(digitalRead(5)) { //you cant press both buttons at once
while(buttonRight.isPressed() && step >= 0) {
if(digitalRead(6) || step <= 0) return digitalWrite(12, 0);
digitalWrite(12, 1);
step--;
stepper.moveTo(step);
stepper.runToPosition();
}
}
//yellow, left control
if(digitalRead(6)){ //you cant press both buttons at once
while(buttonLeft.isPressed() && step <= stepLimit) {
if(digitalRead(5) || step >= stepLimit) return digitalWrite(11, 0);
digitalWrite(11, 1);
step++;
stepper.moveTo(step);
stepper.runToPosition();
}
}
}