Plinko Token Drop

Hello, I work at a rehab center and am working to set up a system that releases a little token into a plinko board. Currently I am using a stepper motor to move a contraption back and forth and then 2 mini servos motors that open and release the token. Upon pushing a button, the stepper moves a random number of steps and then the 2 servos open 90 degrees. I am trying to figure out how to either set up limit switches or one homing switch for the stepper motor. I am using a 5V 28BYJ-48 with a ULN2003 and Arduino uno. I attached my code, in the set up I added a second button because I have been playing around with different things.

#include <Servo.h>
#include <Stepper.h>                           // include stepper library
int revolution = 2038;              // the number of steps in one revolution of your motor (28BYJ-48)
Stepper stepper(revolution, 8, 10, 9, 11); 
int buttonPin=3;
int buttonPin1=5;

Servo servo1;
Servo servo2;

int buttonState = 0;      
int buttonState1 = 0;

void setup() {
  Serial.begin(9600);  
    servo1.attach(4);
  servo2.attach(7);
    pinMode(8, OUTPUT);
  pinMode(10, OUTPUT);
  pinMode(9, OUTPUT);
  pinMode(11, OUTPUT);
servo1.write(0);
servo2.write(180);
delay(2000);
  pinMode(buttonPin, INPUT_PULLUP);
  pinMode(buttonPin1,INPUT_PULLUP);
 
}

void loop() {
  delay(100);
  buttonState = digitalRead(buttonPin); 
  buttonState1 = digitalRead(buttonPin1); 

  uint8_t i;
  
if((buttonState==LOW)){
    stepper.setSpeed(10);       
  stepper.step(random(-5000, 5000));    
  delay(1000); 
    Serial.println("i entered");
   servo1.write(90);
  servo2.write(90);
  delay(1000);
servo1.write(0);
servo2.write(180);
delay(1000); 
}               

else{
}
}


You are using pin 7 to control a servo2.
But pin 7 is not PWM, so it doesn't control the servo.

Sorry. my mistake.

Say that back to yourself, but slowly.

Treat it like a pushbutton.

.
.
    stepper.step(random(-5000, 5000));
    if((buttonState1==LOW)){ // limit switch is button1
      // something
    }
.
.

this seems to not do anything. but maybe my code is wrong.

#include <Servo.h>
#include <Stepper.h>                           // include stepper library
int revolution = 2038;              // the number of steps in one revolution of your motor (28BYJ-48)
Stepper stepper(revolution, 8, 10, 9, 11); 
int buttonPin=3;
int buttonPin1=5;

Servo servo1;
Servo servo2;

int buttonState = 0;      
int buttonState1 = 0;

void setup() {
  Serial.begin(9600);  
    servo1.attach(4);
  servo2.attach(7);
    pinMode(8, OUTPUT);
  pinMode(10, OUTPUT);
  pinMode(9, OUTPUT);
  pinMode(11, OUTPUT);
servo1.write(0);
servo2.write(180);
delay(2000);
  pinMode(buttonPin, INPUT_PULLUP);
  pinMode(buttonPin1,INPUT_PULLUP);

}

void loop() {
  delay(100);
  buttonState = digitalRead(buttonPin); 
  buttonState1 = digitalRead(buttonPin1); 

  uint8_t i;
  
if((buttonState==LOW)){
    stepper.setSpeed(10);       
  stepper.step(random(-5000, 5000));    
if((buttonState1==LOW)){
  stepper.setSpeed(10);
  stepper.step(random(-50,-2038));
  }
  delay(1000); 
    Serial.println("i entered");
   servo1.write(90);
  servo2.write(90);
  delay(1000);
servo1.write(0);
servo2.write(180);
delay(1000); 
}               

else{
}
}

When you start the steppermotor using the step() command, the steppermotor continues to receive step pulses until your random steps is reached. I think you should try this:

  1. Get a random number and direction, as you did random (-5000, 5000) but as a variable
  2. Step your motor ONE step
  3. Check the if limit switch, then stop
  4. Check if steps is equal to random number, then stop
  5. Go to 2.

That makes sense. I am still very new to Arduino. Could you help me better understand what the code for that would look like?
I tried this but know it is likely wrong, as it made no changes.

#include <Servo.h>
#include <Stepper.h>                           // include stepper library
int revolution = 2038;              // the number of steps in one revolution of your motor (28BYJ-48)
Stepper stepper(revolution, 8, 10, 9, 11); 
int buttonPin=3;
int buttonPin1=5;
int stepCount = 0;

Servo servo1;
Servo servo2;

int buttonState = 0;      
int buttonState1 = 0;

void setup() {
  Serial.begin(9600);  
    servo1.attach(4);
  servo2.attach(7);
    pinMode(8, OUTPUT);
  pinMode(10, OUTPUT);
  pinMode(9, OUTPUT);
  pinMode(11, OUTPUT);
servo1.write(0);
servo2.write(180);
delay(2000);
  pinMode(buttonPin, INPUT_PULLUP);
  pinMode(buttonPin1,INPUT_PULLUP);

 
}

void loop() {
  delay(100);
  buttonState = digitalRead(buttonPin); 
  buttonState1 = digitalRead(buttonPin1); 

  uint8_t i;
  

  
if((buttonState==LOW)){
    stepper.setSpeed(10);       
  stepper.step(random(-5000, 5000));
  stepCount++;     
if((buttonState1==LOW)){
  stepper.setSpeed(10);
  stepper.step(random(-50,-2038));
  }
  delay(1000); 
    Serial.println("i entered");
   servo1.write(90);
  servo2.write(90);
  delay(1000);
servo1.write(0);
servo2.write(180);
delay(1000); 
}               

Okay... see if this makes sense, and if not, we will try again... BUT FIRST, in reading my writing, I think the order should be changed to:

  1. Get a random number
  2. Check the limit switch
  3. Check if steps taken is equal to random number
  4. Step motor one step
  5. go to 2.

Step 1. You have this...

Try this:

int randomSteps;
.
.
randomSteps = random(-5000, 5000);
stepper.step(randomSteps);

Step 2: You have an empty "else" - maybe that was to be the limit switch test. Try putting that in a function... and call the function

void loop() {
  .
  .
  checkLimitSwitch();
 .
 .
}

void checkLimitSwitch() {
  if (buttonState1 == LOW) { // limit switch (red)
    stepper.setSpeed(0);
    stepper.step(0); // this only stops the steppermotor, but not the sketch, it continues to step
    // while(1); // this is not a good way to stop the stepper, but might help until resolved
    // goHome(); // this could be the function you want if the limit switch is reached
  }
}

Step 3: In step 4 you will step the steppermotor one step, but you need to stop the motor from stepping if it has reached "randomSteps"

if (stepsTaken == randomSteps)
    // goHome(); // this could be the function you want if randomSteps is reached

Step 4:

 else // this 'else' is from failing 'if (stepsTaken == randomSteps)
    stepper.step(1); // steppermotor takes one step

Setp 5: "Wrap" steps 1 through 4 in void loop() where you have the buttons being read

Extras:
z. Move "i entered" into its own function, to be called when stepsTaken equals randomSteps

void ientered() {
  delay(1000);
  Serial.println("i entered");
  servo1.write(90);
  servo2.write(90);
  delay(1000);
  servo1.write(0);
  servo2.write(180);
  delay(1000);
}

a. Make a "goHome" function to send your steppermotor back to the beginning

void goHome() {
  Serial.print("Step to: 0 "); // serial monitor debug information
  stepper.step(randomSteps * -1); // anything "times" minus-one is in the opposite direction
  Serial.println("(Home)"); // serial monitor debug information
}

This is not the only way to make this happen... use your imagination... experience only comes with experiencing... so crash-it-til-you-smash-it.

[edit]
You also have a typo at the top:

// int revolution = 2038; // the number of steps in one revolution of your motor (28BYJ-48)
int revolution = 2048; // the number of steps in one revolution of your motor (28BYJ-48)

[edit]
I have been tinkering with your code...

Thank you! I missed this somehow.

Try your code, revised, in this simulation

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.