SparkFun Forums 

Where electronics enthusiasts find answers.

For the discussion of Arduino related topics.
By cmjb13
#192144
I have a slot machine bonus wheel that I need to set to stop at random numbers. I push the button once (press & release) and the stepper motor stops, but it's not stopping on random numbers, rather fixed intervals.

The code I have working so far is below...
Code: Select all
#include <Stepper.h>
int x = 0;
//define our variables
const int buttonPin = 3;  // the number of the pushbutton pin
const int stepsPerRevolution = 200;  // change this to fit the number of steps per revolution
int buttonState = 0;  // variable for reading the pushbutton status
Stepper myStepper(stepsPerRevolution, 8, 9, 10, 11); //set stepper pins
 
void setup() { //this code will only run once
  pinMode(3, INPUT); // initialize the pushbutton pin as an input:
  myStepper.setSpeed(60); //set stepper speed
  Serial.begin(4800);  // initialize the SLOW serial port
} //end of setup function
 
void loop() {
  buttonState = digitalRead(buttonPin); // check if the pushbutton is pressed.
  if (buttonState == LOW) { //if button is pressed
      steppermotorfunction();// run the stepper motor function:
  }
  else
  {
  delay(300);//do nothing waiting for button press
  }
} //end of loop function
 
void steppermotorfunction() {
  //This code runs as soon as the button is pressed
   myStepper.step(random(10, 15) * stepsPerRevolution);
   }
I've been told I need to add a state change detection line as well as dealing with a floating pin (see attached diagram.jpg). I seem to be having trouble understanding the state change concept and exactly what code to add. I'm about 90% there to complete randomness. I appreciate any assistance.
You do not have the required permissions to view the files attached to this post.