Horse race - doing one thing while receiving instructions for the next

Ok, så I am building a game with my son.

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.

Any ideas? :slight_smile:

I am fairly certain you could do everything with the Mega which would make life easier.

How many holes need to be detected for each lane? If the number is small a nano may be able to do everything.

How does the Arduino register that a ball has fallen into a hole?

Have a look at how the code is organized in Several Things at a Time

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.

...R

Hello Robin

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.

I have looked at the example but I am not sure I understand how calling functions will help me?

By having the functions do one thing and quickly return it looks like it is doing all the functions at once.

You have what is known as a state machine, look up what that means.

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!

That's indeed the idea.

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.

See this thread for a fairly extensive tutorial.

Vitcho:
Thanks for input. I have 16 holes on each lane, but I have grouped 6 of them to save ports on the nano.

You have no said how the Arduino detects that a ball has dropped into a hole.

...R