Stepper motor control

Hello,

I am working on a game similar to the " donkey derby" type of arcade game where you role the balls into rings and make the donkeys run......

Anyway, I am using stepper motors to turn a threaded bar for the mechanism;

e.g. with a switch on each loop;

red loop = 2000 steps
blue loop = 1000 steps etc....

I am using simple if statements looking for a "high" on each switch.

My issue is that if you hit the "red loop" switch it will begin running the stepper, but the if you hit the blue loop quickly afterwards it will revert to the 1000 steps. This means that the quicker you are, you actually lose out. Is there a way of telling the stepper to run on or keep a count of how many it needs to step and remember the values?

Hope this makes sense and thank in advance.

Paul

I am using simple if statements looking for a "high" on each switch.

It would probably be better to look for a change to HIGH.

My issue is that if you hit the "red loop" switch it will begin running the stepper

The code that you posted does NOT show that to be the case.

Is there a way of telling the stepper to run on

No. That is NOT what a stepper does.

or keep a count of how many it needs to step and remember the values?

Possibly. What was the question. I forgot what we were talking about.

Now, if I'd created some variables where I could store stuff, I might not have forgotten...

Pjl83:
Hope this makes sense and thank in advance.

It should certainly be possible but you need to post your program so we can see what you can see.

...R
Stepper Motor Basics
Simple Stepper Code

Thanks Robin, I'll post the code when I'm on my pc at home.

PaulS - I'm not sure if that's helpful or not. You're response seemed a little cryptic. but thanks anyway.

You're response seemed a little cryptic.

Well, it was designed to get you to elaborate. A bit too subtle, I guess.

With regards to my first comment: The state change detection example shows how to determine when a pin HAS BECOME HIGH rather than just IS HIGH. There are situations where you care that the switch IS HIGH, but more often, we care that a switch has become pressed, or has become released, so we take an action only once.

With regards to my second comment: We can NOT help you make your code do what you want when we can not see your code. POST YOUR CODE!

With regards to my third comment: Yes, you can make the Arduino remember things. All you need to do is define some global or static variables that can hold the data you want to remember, and update the values in the variables whenever what you need to remember changes.

Hi,

Here's my code so far. I'd like to get the motor control fixed before I add other features and a second player. Currently as an example; if a player scores 3 "yellow rings" in quick succession then I would like the motor to step 9000 times, but with this code it will effectively reset the 3000 with each switch input.

/*
 The motor is attached to digital pins 8 - 11 of the Arduino.

 */

#include <Stepper.h>                  // call stepper control

const int stepsPerRevolution = 2048;  // number of steps per revolution
const int blueRing = 1000;            // revolutions when player scores a blue ring
const int yellowRing = 3000;          // revolutions when player scores a yellow ring
int switchStateblue = 0;              // blue ring switch
int switchStateyellow = 0;            // yellow ring switch


Stepper myStepper(stepsPerRevolution, 8, 10, 9, 11);  // initialize the stepper library on pins 8 through 11

void setup() {
  
  myStepper.setSpeed(20); // set the speed at 20 rpm
  Serial.begin(9600);  // initialize the serial port
  pinMode(13, INPUT);
}

void loop() {
 switchStateblue=digitalRead(13);   // blue ring switch pin

 if (switchStateblue == HIGH) {
  
  Serial.println("clockwise");
  myStepper.step(blueRing);
 }
   switchStateyellow=digitalRead(4);  // yellow ring switch pin

 if (switchStateyellow == HIGH) {

  Serial.println("clockwise");
  myStepper.step(yellowRing);
  
 }
  
}

Thank you in advance
Paul

Suppose that you need to take 3000 steps to get from home to school. Suppose, part way to school (say 1000 steps into the trip), a friend says "Hey, lets go bowling after school". Suppose that the bowling alley is 2000 steps from school.

Would you say "Hey, man, I can't even think about that now. I need to finish stepping to school"? Of course not.

Can you figure out how many more steps you will need to take to get to the bowling alley?

Each time that some condition is met, you need to ADD some number of steps to the number of steps that still need to be taken.

Each time that you step, you subtract one step from the number steps still needed.

Pjl83:
My issue is that if you hit the "red loop" switch it will begin running the stepper, but the if you hit the blue loop quickly afterwards it will revert to the 1000 steps. This means that the quicker you are, you actually lose out. Is there a way of telling the stepper to run on or keep a count of how many it needs to step and remember the values?

Now that I have looked at your program I realize that I don't understand what you want to happen.

Can you describe the required behaviour with each action on a separate line - something like

check switches
if red switch is pressed
   move RR steps
else if blue switch is pressed
   move BB steps
repeat

I'm quite sure that my list of steps is wrong and impractical - I just wrote them to illustrate the style of presentation which makes it easy to turn the requirement into a program. Note that I made no attempt to write computer code.

...R

OK, so rather than the buttons directly stepping the motor, they should add a value to a variable and the motor should be stepping from the variable?

check switches

if blue switch goes high
add 1000 to variable

if yellow switch goes high
add 3000 to variable

step motor "total variable" steps

for every step minus a step from total

Can anyone tell me the command I'm looking for to add values to a variable?

Thanks
Paul

OK, so rather than the buttons directly stepping the motor, they should add a value to a variable and the motor should be stepping from the variable?

Yes. The number of steps that need to be taken changes as:

  • Events happen that require more steps to be taken (the number of steps still to take goes up
  • The stepper steps (the number of steps still to take goes down)

Can anyone tell me the command I'm looking for to add values to a variable?

It isn't a command. It's an operator. The addition operator.

   numStepsToGo = numStepsToGo + 1000;

or

   numStepsToGo += 1000;

I'll bet you can guess what the operator is to subtract values from a variable.

Thank you.

I'm just confused as to how that sits in the sketch.

Setup;

int numStepsToGo = 0

Loop;

if blue button = high 
then numStepsToGo = numStepsToGo + 1000

if yellow button = high 
then numStepsToGo = numStepsToGo + 3000

if numStepsToGo > 0
then
step motor
numStepsToGo = numStepsToGo - 1

If I step 1000 at a time then it would still be stepping next time the loop comes around so the issue would still exist. A single step and a -1 each loop would work maybe? depending on ccyle time this may need a very small delay to ensure it's complete?

Am I on the right lines here?

Thanks

edit - didnt need steps taken

Pjl83:
If I step 1000 at a time then it would still be stepping next time the loop comes around so the issue would still exist.

That sounds like you have things back to front. loop() should repeat dozens or hundreds of times between each step.

...R

OK, so I tried compiling;

/*
 The motor is attached to digital pins 8 - 11 of the Arduino.

 */

#include <Stepper.h>                  // call stepper control

const int stepsPerRevolution = 2048;  // number of steps per revolution
const int blueRing = 1000;            // revolutions when player scores a blue ring
const int yellowRing = 3000;          // revolutions when player scores a yellow ring
int switchStateblue = 0;              // blue ring switch
int switchStateyellow = 0;            // yellow ring switch


Stepper myStepper(stepsPerRevolution, 8, 10, 9, 11);  // initialize the stepper library on pins 8 through 11

void setup() {
  
  myStepper.setSpeed(20); // set the speed at 20 rpm
  Serial.begin(9600);  // initialize the serial port
  pinMode(13, INPUT);
  pinMode(4, INPUT);

  int numStepsToGo = 0;

  
}

void loop() {
 switchStateblue=digitalRead(13);   // blue ring switch pin

 if (switchStateblue == HIGH) {

 int numStepsToGo = numStepsToGo + blueRing;   // add blue ring value to numStepsToGo variable

 }
   switchStateyellow=digitalRead(4);  // yellow ring switch pin

 if (switchStateyellow == HIGH); {   

int numStepsToGo = numStepsToGo + yellowRing;  // add yellow ring value to numStepsToGo variable

 }

 [color=red]if (int numStepsToGo > 0); {[/color]

Serial.println("clockwise");
  myStepper.step(1);

int numStepsToGo = numStepsToGo - 1;

 }
  
}

I have an error on the red line;

error: expected primary-expression before 'int'

  if (int numStepsToGo > 1); {

      ^

cheap_tat_derby:46: error: expected ')' before 'int'

exit status 1
expected primary-expression before 'int'

Any thoughts on the code and the error?

EDIT - I removed int and it cleared the error. Would still appreciate any thoughts on the logic.

Thanks
Paul

When you put int on a line like this

int numStepsToGo

it creates a new variable every time loop() repeats and the previous value is lost. And the variable created in setup() is completely different from the one created in loop()

The simplest way to get things working is to declare all your variables above setup() and then remove all the ints from all the lines in setup() and loop().

...R

  if (int numStepsToGo > 1); {

If statements rarely have semicolons. Those that do, do not have curly braces after them.