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?
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.
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);
}
}
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.
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?
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?
/*
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.
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().