-1

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;

}
0

The event of a button press is not deterministic in your case. Generally, your delay()-approach combined with reading the current button state is not how sth. like this works. Instead, you have to detect when a button is pressed/released. One way to do that is the following:

#define buttonPin 8
bool last_state=false;

void loop(){
  if(!digitalRead(buttonPin)) {
    if(last_state) {
      //button has been released
      //increment your counter here
    }
    last_state=false;
  } else {
    last_state=true;
  }
}
| improve this answer | |
0

First understand that myStepper.step(x) method is relative steps not absolute. So if you want the motor to do two rotations of 45 steps back to back you should write

myStepper.step(45);
myStepper.step(45);    /// NOT myStepper.step(90)  <-- that's 90 MORE steps

Second, it looks like your code will increase steps taken by 45 while HOLDING the button down but should also rotate the motor 45 steps for each button press as long as the subsequent presses do not occur while the motor is in mid rotation.

Furthermore, your can delete the following extra else if statements and it should still work.

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);
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.