I'm very new to Arduino, currently undertaking a uni project in which my team wish to create a sort of slot machine-esque device that uses a step motor to rotate a drum (like one of the cylinders in a slot machine).
What I've currently tried to do is merge some code that uses a counter variable and button to enable a servo to rotate round multiple times with a single button, with code I found that i think is used to test a step motor? In my mind the code I have should allow me to rotate my stepper by 45 degrees after each button press, but I have no idea if my code makes any sense. Could you guys tell me if im close or is it a load of rubbish. if it does work ok, id look to set the speed higher and have the motor perform multiple full rotations before each stop, to make it work more like an actual slot machine spinner. any advice you could offer would be great, apologies in advance if i don't quite understand certain things you may explain as I am new to coding, as I mentioned.
Here's the code:
#include <Stepper.h>
//variables
int counter = 0;
const int buttonPin = 8;
const int stepsPerRevolution = 90;
Stepper myStepper(stepsPerRevolution,1,2,3,4);
void setup() {
pinMode(buttonPin, INPUT);
myStepper.setSpeed(60);
}
void loop() {
int buttonState;
buttonState = digitalRead(buttonPin);
if (buttonState == LOW)
{
counter++;
delay(150);
}
if(counter == 0)
myStepper.step(0);
else if(counter == 1)
myStepper.step(45);
else if(counter == 2)
myStepper.step(90);
else if(counter == 3)
myStepper.step(135);
else if(counter == 4)
myStepper.step(180);
else if(counter == 5)
myStepper.step(225);
else if(counter == 6)
myStepper.step(270);
else if(counter == 7)
myStepper.step(315);
else if(counter == 8)
myStepper.step(360);
else
counter = 0;
}