Horse Race Derby Game

I’m a beginner and trying to write a code for a horse race derby game (made out of wood). There are two horses and each is driven by a 360° servo. Each horse is connected to three different sensors. Depending on which sensor is activated the horse should walk 1, 2 or 3 steps, what means, that the servo should make 1, 2 or 3 full rotations. I already wrote a working code using long delays to run the servos. But with this solution there are some issues: Its not possible that both horses walk at the same time and i don’t know how to make the horses return at the exact start position after one player one (one horse arrived at the end of the runway) at this moment the servos should rotate backwards, but each one individually. I thought I could place sensors at the start end the end of the runway, but they wont be activated during the delay.

I’m thankful for every input!

Horsegame.jpg

d1d1:
I already wrote a working code using long delays to run the servos.

Welcome to the forum!

  1. Post your ‘working’ code so we can inspect it. Use the </> button above to do this.

  2. Long delays? Red warning light to most programmers. Servos can move to the required position without delays. Thus, several servos can be active simultaneously.

  3. See point 1! And anything else you may have discovered in the meantime.

If you persevere you will be able to fix this but you must be prepared to do the ‘grunt work’ whilst others here suggest and nudge you in the right direction. After all, this is fun, right?

#include <Servo.h>

Servo servo_pin_9;

void setup()
{
pinMode( 2 , INPUT);
pinMode( 3 , INPUT);
pinMode( 4 , INPUT);
servo_pin_9.attach(9);
}

void loop()
{
if (digitalRead( 2))
{
servo_pin_9.write( 180 );
delay( 2000 );
servo_pin_9.write( 90 );
}
if (digitalRead( 3))
{
servo_pin_9.write( 180 );
delay( 4000 );
servo_pin_9.write( 90 );
}
if (digitalRead( 4))
{
servo_pin_9.write( 180 );
delay( 6000 );
servo_pin_9.write( 90 );
}
}

Thank you for your answer, it is really fun.

Is it also possible to run 360° servos without deley, as they just change speed by changing degrees?

Hi,
5e63738ca94920350ea6fd6f89c250b8ea50a012.jpg
You need to look in the Arduino IDE Examples for “Blink without Delay” .

Tom… :slight_smile:

Hi,

I just wanted to add, that it might be a good ideer to put your servo position into a variable, so that you can add to it.

For instance, you might want to have the servo go to the new position at a certain speed, and therefore you would make it move slower then it would normally do. This could mean that the used has another successful hit, which should be added to the desired servo position.

I hope that made sense. :)

Hi d1d1,

I'm also looking to build this fun horse race game and I wondering how are you detecting the balls going through the yellow, blue and red holes?

/Carlsson