Essentially the game is based on each player having his own lane to roll small balls into holes (like in this game Fascination (game) - Wikipedia). In my game the holes gives points that translates into movement of a horse that competes against the horses of the other players. The horses have to "run" a certain distance and the first to reach the end, wins. Thus, the player most skilled in hitting the holes that moves the horse the most, wins.
My idea is to have a small nano on each lane that registers which hole has been hit. This is sent another arduino (Mega) and the horse is moved (by using a stepper motor).
Here is the problem: It will very likely happen that a horse for one lane is being moved while another one registers a hole. Right now I am just executing in a loop but of course I don't want it to ignore new players getting a ball in a hole just because the arduino is presently moving a horse (and there occupied). Is there any way to make it do several things at the same time? Or queue movement commands maybe? I can't se how to do this in a loop so maybe there is a way to accomplish this I haven't thought of?
I have thought of letting each nano control its own horse, but then the other lanes won't know the race is over, when another horse is at the finish line which is not good. The game should start and stop at the same time on all lanes.
Note how each function runs very briefly and returns to loop() so the next one can be called. None of the functions tries to complete a task in one call.
Thanks for input. I have 16 holes on each lane, but I have grouped 6 of them to save ports on the nano.
I have looked at the example but I am not sure I understand how calling functions will help me?
To clarify: Lets say each stepper motor needs to run for 1 second when a ball is sunk - will it make arduino listen for new commands while the motor is running if I put it in a function? That is the exact functionality I need I just don't understand why it would help to put it in a separate function? Won't arduino have to complete the 1 second motor run before it can listen for new events?
Lets say each stepper motor needs to run for 1 second when a ball is sunk.
Won't arduino have to complete the 1 second motor run before it can listen for new events?
No, the stepper needs to advance the horse a given number of steps when the ball is sunk. It can take those steps one at a time, checking "am I there yet", and then look at other inputs and move other horses if necessary before it moves another step.
At 16MHz the Arduino is very fast on the human scale of rolling balls into holes and seeing horses move towards a finish line.
As long as you do your programming properly it can look for balls falling through holes while moving all horses at different speed and for different duration. No problem there.
Thanks guys! I looked into it and think I understand that I need to divide the action up in small chunks and execute them serially which will give the effect of multitasking. I am unsure if I can pull it off in code but I will give it a go.
I may return with actual code example for help but for now you put me on the right track!
You write your functions to be non-blocking: so no while() loops or delay() calls; use millis() for timing. Likewise you also write your functions knowing that they will be called frequently.