-
11Step 11
Connect boards through Serial communication)
Each of the 5 Uno's from the stepper motors needs to be connected to the finish Mega.
-
12Step 12
Program the horse boards to run the race that the mega sends it)
As mentioned above, the finish board will send a 5 digit code for the next race the horses should run. Break out that information and run the appropriate race.
#include <stepper.h> Stepper motor(800, 13, 12, 11, 10); boolean firstrace = true; boolean getrace = false; boolean stage = false; boolean race = false; long x = 0; int racenum = 0; int racestep = 0; int rn = 0; void setup() { pinMode(7, INPUT); pinMode(6, INPUT); Serial.begin(9600); motor.setSpeed(70); } void loop() { // move horse to finish switch while (firstrace == true) { motor.step(200); delay(200); if (digitalRead(7) == HIGH) { firstrace = false; getrace = true; } } // wait and get signal while (getrace == true) { while (Serial.available() < 1) { delay(1); } if (Serial.available() != x) { x = Serial.available(); x = Serial.parseInt(); // 12345 } racenum = x * .001; // 12345 = 12.345 racenum = int(racenum); // 12 racenum = racenum * 1000; // 12000 racenum = x - racenum; // 12345 - 12000 = 345 racenum = racenum * .1; // 34.5 racenum = int(racenum); // 34 if (racenum > 0) { rn = racenum; if (rn == 26 || rn == 32 || rn == 38 || rn == 47 || rn == 53 || rn == 59 || rn == 77) { racenum = 1; } else if (rn == 20 || rn == 27 || rn == 33 || rn == 39 || rn == 44 || rn == 56 || rn == 57 || rn == 66 || rn == 72 || rn == 78) { racenum = 2; } else if (rn == 37 || rn == 67 || rn == 73 || rn == 79 || rn == 36 || rn == 74 || rn == 68 || rn == 50 || rn == 35 || rn == 29 || rn == 23 || rn == 41 || rn == 65 || rn == 71 || rn == 45 || rn == 21) { racenum = 3; } else if (rn == 46 || rn == 52 || rn == 58 || rn == 76 || rn == 42 || rn == 75 || rn == 69 || rn == 60 || rn == 54 || rn == 48 || rn == 30 || rn == 22 || rn == 24 || rn == 28 || rn == 34 || rn == 40) { racenum = 4; } else { racenum = 5; } getrace = false; stage = true; } } while (stage == true) { while (digitalRead(7) == HIGH) { delay(1); } motor.step(100); delay(50); if (digitalRead(6) == HIGH) { stage = false; race = true; } } while (race == true) { while (digitalRead(6) == HIGH) { delay(1); } if (racenum == 1) // 22.75 { while (race == true) { motor.step(400); delay(100); if (digitalRead(7) == HIGH) { race = false; getrace = true; } } } if (racenum == 2) // 23.72 { racestep = 38; while (racestep > 0) { motor.step(400); delay(100); racestep--; } while (race == true) { motor.step(200); delay(150); if (digitalRead(7) == HIGH) { race = false; getrace = true; } } } if (racenum == 3) // 24.75 { racestep = 20; while (racestep > 0) { motor.step(400); delay(200); racestep--; } while (race == true) { motor.step(400); delay(100); if (digitalRead(7) == HIGH) { race = false; getrace = true; } } } if (racenum == 4) // 26.58 { racestep = 20; while (racestep > 0) { motor.step(200); delay(100); racestep--; } racestep = 25; while (racestep > 0) { motor.step(400); delay(100); racestep--; } while (race == true) { motor.step(200); delay(200); if (digitalRead(7) == HIGH) { race = false; getrace = true; } } } if (racenum == 5) // 27.3 { racestep = 30; while (racestep > 0) { motor.step(400); delay(100); racestep--; } while (race == true) { motor.step(200); delay(200); if (digitalRead(7) == HIGH) { race = false; getrace = true; } } } } } </stepper.h>
This is my horse 5 code, the code for horses 1-4 will have different race code for the appropriate times.
At this time your horses should be running timed races.
-
13Step 13
Build an odds display)
Make a display 48" wide, and cut 10 square holes big enough for 2x16lcds for the odds, and 1 for the count-down clock.
-
14Step 14
Wire odds display)
I hooked the left 5 2x16lcds to one Mega, and the right 5 2x16lcds to another Mega. The countdown clock is hooked up to an Uno. All three board will need to be grounded to the other boards and hooked up to the serial communications to the Mega finish switch board.
-
15Step 15
Program odds display)
The board will receive the same 5 digit signal as the other boards, and break out the first two digits to print the odds.
Left 5 odds display:
#include <liquidcrystal.h> int h45odds = 0; int h35odds = 0; int h34odds = 0; int h25odds = 0; int h24odds = 0; int h12odds = 0; int h13odds = 0; int h14odds = 0; int h15odds = 0; int h23odds = 0; long lastrace = 0; long x = 0; long y = 0; long count = 0; long count1 = 0; byte LT[8] = { B00111, B01111, B11111, B11111, B11111, B11111, B11111, B11111 }; byte UB[8] = { B11111, B11111, B11111, B00000, B00000, B00000, B00000, B00000 }; byte RT[8] = { B11100, B11110, B11111, B11111, B11111, B11111, B11111, B11111 }; byte LL[8] = { B11111, B11111, B11111, B11111, B11111, B11111, B01111, B00111 }; byte LB[8] = { B00000, B00000, B00000, B00000, B00000, B11111, B11111, B11111 }; byte LR[8] = { B11111, B11111, B11111, B11111, B11111, B11111, B11110, B11100 }; byte MB[8] = { B11111, B11111, B11111, B00000, B00000, B00000, B11111, B11111 }; byte block[8] = { B11111, B11111, B11111, B11111, B11111, B11111, B11111, B11111 }; LiquidCrystal horse23(23, 22, 25, 24, 27, 26); LiquidCrystal horse15(29, 28, 31, 30, 33, 32); LiquidCrystal horse14(35, 34, 37, 36, 39, 38); LiquidCrystal horse13(41, 40, 43, 42, 45, 44); LiquidCrystal horse12(47, 46, 49, 48, 51, 50); void setup() { Serial.begin(9600); horse23.createChar(0,LT); horse23.createChar(1,UB); horse23.createChar(2,RT); horse23.createChar(3,LL); horse23.createChar(4,LB); horse23.createChar(5,LR); horse23.createChar(6,MB); horse23.createChar(7,block); horse15.createChar(0,LT); horse15.createChar(1,UB); horse15.createChar(2,RT); horse15.createChar(3,LL); horse15.createChar(4,LB); horse15.createChar(5,LR); horse15.createChar(6,MB); horse15.createChar(7,block); horse14.createChar(0,LT); horse14.createChar(1,UB); horse14.createChar(2,RT); horse14.createChar(3,LL); horse14.createChar(4,LB); horse14.createChar(5,LR); horse14.createChar(6,MB); horse14.createChar(7,block); horse13.createChar(0,LT); horse13.createChar(1,UB); horse13.createChar(2,RT); horse13.createChar(3,LL); horse13.createChar(4,LB); horse13.createChar(5,LR); horse13.createChar(6,MB); horse13.createChar(7,block); horse12.createChar(0,LT); horse12.createChar(1,UB); horse12.createChar(2,RT); horse12.createChar(3,LL); horse12.createChar(4,LB); horse12.createChar(5,LR); horse12.createChar(6,MB); horse12.createChar(7,block); horse23.begin(16, 2); horse15.begin(16, 2); horse14.begin(16, 2); horse13.begin(16, 2); horse12.begin(16, 2); pinMode(8, OUTPUT); pinMode(9, OUTPUT); pinMode(10, OUTPUT); pinMode(11, OUTPUT); pinMode(12, OUTPUT); digitalWrite(8, HIGH); digitalWrite(9, HIGH); digitalWrite(10, HIGH); digitalWrite(11, HIGH); digitalWrite(12, HIGH); } void custom0() { // uses segments to build the number 0 horse23.setCursor(0+x,0); // set cursor to column 0, line 0 (first row) horse23.write(8); // call each segment to create horse23.write(1); // top half of the number horse23.write(2); horse23.setCursor(0+x, 1); // set cursor to colum 0, line 1 (second row) horse23.write(3); // call each segment to create horse23.write(4); // bottom half of the number horse23.write(5); } void custom1() { horse23.setCursor(0+x,0); horse23.write(1); horse23.write(2); horse23.setCursor(0+x,1); horse23.write(4); horse23.write(7); horse23.write(4); } void custom2() { horse23.setCursor(0+x,0); horse23.write(6); horse23.write(6); horse23.write(2); horse23.setCursor(0+x, 1); horse23.write(3); horse23.write(4); horse23.write(4); } void custom3() { horse23.setCursor(0+x,0); horse23.write(6); horse23.write(6); horse23.write(2); horse23.setCursor(0+x, 1); horse23.write(4); horse23.write(4); horse23.write(5); } void custom4() { horse23.setCursor(0+x,0); horse23.write(3); horse23.write(4); horse23.write(7); horse23.setCursor(2+x, 1); horse23.write(7); } void custom5() { horse23.setCursor(0+x,0); horse23.write(3); horse23.write(6); horse23.write(6); horse23.setCursor(0+x, 1); horse23.write(4); horse23.write(4); horse23.write(5); } void custom6() { horse23.setCursor(0+x,0); horse23.write(8); horse23.write(6); horse23.write(6); horse23.setCursor(0+x, 1); horse23.write(3); horse23.write(4); horse23.write(5); } void custom7() { horse23.setCursor(0+x,0); horse23.write(1); horse23.write(1); horse23.write(2); horse23.setCursor(2+x, 1); horse23.write(7); } void custom8() { horse23.setCursor(0+x,0); horse23.write(8); horse23.write(6); horse23.write(2); horse23.setCursor(0+x, 1); horse23.write(3); horse23.write(4); horse23.write(5); } void custom9() { horse23.setCursor(0+x,0); horse23.write(8); horse23.write(6); horse23.write(2); horse23.setCursor(2+x, 1); horse23.write(7); } // count 2 void custom02() { // uses segments to build the number 0 horse15.setCursor(0+x,0); // set cursor to column 0, line 0 (first row) horse15.write(8); // call each segment to create horse15.write(1); // top half of the number horse15.write(2); horse15.setCursor(0+x, 1); // set cursor to colum 0, line 1 (second row) horse15.write(3); // call each segment to create horse15.write(4); // bottom half of the number horse15.write(5); } void custom12() { horse15.setCursor(0+x,0); horse15.write(1); horse15.write(2); horse15.setCursor(0+x,1); horse15.write(4); horse15.write(7); horse15.write(4); } void custom22() { horse15.setCursor(0+x,0); horse15.write(6); horse15.write(6); horse15.write(2); horse15.setCursor(0+x, 1); horse15.write(3); horse15.write(4); horse15.write(4); } void custom32() { horse15.setCursor(0+x,0); horse15.write(6); horse15.write(6); horse15.write(2); horse15.setCursor(0+x, 1); horse15.write(4); horse15.write(4); horse15.write(5); } void custom42() { horse15.setCursor(0+x,0); horse15.write(3); horse15.write(4); horse15.write(7); horse15.setCursor(2+x, 1); horse15.write(7); } void custom52() { horse15.setCursor(0+x,0); horse15.write(3); horse15.write(6); horse15.write(6); horse15.setCursor(0+x, 1); horse15.write(4); horse15.write(4); horse15.write(5); } void custom62() { horse15.setCursor(0+x,0); horse15.write(8); horse15.write(6); horse15.write(6); horse15.setCursor(0+x, 1); horse15.write(3); horse15.write(4); horse15.write(5); } void custom72() { horse15.setCursor(0+x,0); horse15.write(1); horse15.write(1); horse15.write(2); horse15.setCursor(2+x, 1); horse15.write(7); } void custom82() { horse15.setCursor(0+x,0); horse15.write(8); horse15.write(6); horse15.write(2); horse15.setCursor(0+x, 1); horse15.write(3); horse15.write(4); horse15.write(5); } void custom92() { horse15.setCursor(0+x,0); horse15.write(8); horse15.write(6); horse15.write(2); horse15.setCursor(2+x, 1); horse15.write(7); } // count 3 void custom03() { // uses segments to build the number 0 horse14.setCursor(0+x,0); // set cursor to column 0, line 0 (first row) horse14.write(8); // call each segment to create horse14.write(1); // top half of the number horse14.write(2); horse14.setCursor(0+x, 1); // set cursor to colum 0, line 1 (second row) horse14.write(3); // call each segment to create horse14.write(4); // bottom half of the number horse14.write(5); } void custom13() { horse14.setCursor(0+x,0); horse14.write(1); horse14.write(2); horse14.setCursor(0+x,1); horse14.write(4); horse14.write(7); horse14.write(4); } void custom23() { horse14.setCursor(0+x,0); horse14.write(6); horse14.write(6); horse14.write(2); horse14.setCursor(0+x, 1); horse14.write(3); horse14.write(4); horse14.write(4); } void custom33() { horse14.setCursor(0+x,0); horse14.write(6); horse14.write(6); horse14.write(2); horse14.setCursor(0+x, 1); horse14.write(4); horse14.write(4); horse14.write(5); } void custom43() { horse14.setCursor(0+x,0); horse14.write(3); horse14.write(4); horse14.write(7); horse14.setCursor(2+x, 1); horse14.write(7); } void custom53() { horse14.setCursor(0+x,0); horse14.write(3); horse14.write(6); horse14.write(6); horse14.setCursor(0+x, 1); horse14.write(4); horse14.write(4); horse14.write(5); } void custom63() { horse14.setCursor(0+x,0); horse14.write(8); horse14.write(6); horse14.write(6); horse14.setCursor(0+x, 1); horse14.write(3); horse14.write(4); horse14.write(5); } void custom73() { horse14.setCursor(0+x,0); horse14.write(1); horse14.write(1); horse14.write(2); horse14.setCursor(2+x, 1); horse14.write(7); } void custom83() { horse14.setCursor(0+x,0); horse14.write(8); horse14.write(6); horse14.write(2); horse14.setCursor(0+x, 1); horse14.write(3); horse14.write(4); horse14.write(5); } void custom93() { horse14.setCursor(0+x,0); horse14.write(8); horse14.write(6); horse14.write(2); horse14.setCursor(2+x, 1); horse14.write(7); } // count 4 void custom04() { // uses segments to build the number 0 horse13.setCursor(0+x,0); // set cursor to column 0, line 0 (first row) horse13.write(8); // call each segment to create horse13.write(1); // top half of the number horse13.write(2); horse13.setCursor(0+x, 1); // set cursor to colum 0, line 1 (second row) horse13.write(3); // call each segment to create horse13.write(4); // bottom half of the number horse13.write(5); } void custom14() { horse13.setCursor(0+x,0); horse13.write(1); horse13.write(2); horse13.setCursor(0+x,1); horse13.write(4); horse13.write(7); horse13.write(4); } void custom24() { horse13.setCursor(0+x,0); horse13.write(6); horse13.write(6); horse13.write(2); horse13.setCursor(0+x, 1); horse13.write(3); horse13.write(4); horse13.write(4); } void custom34() { horse13.setCursor(0+x,0); horse13.write(6); horse13.write(6); horse13.write(2); horse13.setCursor(0+x, 1); horse13.write(4); horse13.write(4); horse13.write(5); } void custom44() { horse13.setCursor(0+x,0); horse13.write(3); horse13.write(4); horse13.write(7); horse13.setCursor(2+x, 1); horse13.write(7); } void custom54() { horse13.setCursor(0+x,0); horse13.write(3); horse13.write(6); horse13.write(6); horse13.setCursor(0+x, 1); horse13.write(4); horse13.write(4); horse13.write(5); } void custom64() { horse13.setCursor(0+x,0); horse13.write(8); horse13.write(6); horse13.write(6); horse13.setCursor(0+x, 1); horse13.write(3); horse13.write(4); horse13.write(5); } void custom74() { horse13.setCursor(0+x,0); horse13.write(1); horse13.write(1); horse13.write(2); horse13.setCursor(2+x, 1); horse13.write(7); } void custom84() { horse13.setCursor(0+x,0); horse13.write(8); horse13.write(6); horse13.write(2); horse13.setCursor(0+x, 1); horse13.write(3); horse13.write(4); horse13.write(5); } void custom94() { horse13.setCursor(0+x,0); horse13.write(8); horse13.write(6); horse13.write(2); horse13.setCursor(2+x, 1); horse13.write(7); } // count 5 void custom05() { // uses segments to build the number 0 horse12.setCursor(0+x,0); // set cursor to column 0, line 0 (first row) horse12.write(8); // call each segment to create horse12.write(1); // top half of the number horse12.write(2); horse12.setCursor(0+x, 1); // set cursor to colum 0, line 1 (second row) horse12.write(3); // call each segment to create horse12.write(4); // bottom half of the number horse12.write(5); } void custom15() { horse12.setCursor(0+x,0); horse12.write(1); horse12.write(2); horse12.setCursor(0+x,1); horse12.write(4); horse12.write(7); horse12.write(4); } void custom25() { horse12.setCursor(0+x,0); horse12.write(6); horse12.write(6); horse12.write(2); horse12.setCursor(0+x, 1); horse12.write(3); horse12.write(4); horse12.write(4); } void custom35() { horse12.setCursor(0+x,0); horse12.write(6); horse12.write(6); horse12.write(2); horse12.setCursor(0+x, 1); horse12.write(4); horse12.write(4); horse12.write(5); } void custom45() { horse12.setCursor(0+x,0); horse12.write(3); horse12.write(4); horse12.write(7); horse12.setCursor(2+x, 1); horse12.write(7); } void custom55() { horse12.setCursor(0+x,0); horse12.write(3); horse12.write(6); horse12.write(6); horse12.setCursor(0+x, 1); horse12.write(4); horse12.write(4); horse12.write(5); } void custom65() { horse12.setCursor(0+x,0); horse12.write(8); horse12.write(6); horse12.write(6); horse12.setCursor(0+x, 1); horse12.write(3); horse12.write(4); horse12.write(5); } void custom75() { horse12.setCursor(0+x,0); horse12.write(1); horse12.write(1); horse12.write(2); horse12.setCursor(2+x, 1); horse12.write(7); } void custom85() { horse12.setCursor(0+x,0); horse12.write(8); horse12.write(6); horse12.write(2); horse12.setCursor(0+x, 1); horse12.write(3); horse12.write(4); horse12.write(5); } void custom95() { horse12.setCursor(0+x,0); horse12.write(8); horse12.write(6); horse12.write(2); horse12.setCursor(2+x, 1); horse12.write(7); } void loop() { if (Serial.available() > 0 && Serial.available() != lastrace) { lastrace = Serial.available(); x = Serial.parseInt(); // 12345 y = int(x * .1); // 1234 y = y * 10; // 12340 y = x - y; // 12345 - 12340 = 5 if (y != 4) { horse23.clear(); digitalWrite(8, LOW); } if (y != 3) { horse15.clear(); digitalWrite(9, LOW); } if (y != 2) { horse14.clear(); digitalWrite(10, LOW); } if (y != 1) { horse13.clear(); digitalWrite(11, LOW); } if (y != 0) { horse12.clear(); digitalWrite(12, LOW); } delay(4000); digitalWrite(8, HIGH); digitalWrite(9, HIGH); digitalWrite(10, HIGH); digitalWrite(11, HIGH); digitalWrite(12, HIGH); horse23.clear(); horse15.clear(); horse14.clear(); horse13.clear(); horse12.clear(); x = int(x * .001); // 12345 = 12 switch(x) { case 10: h12odds = 14; h13odds = 9; h14odds = 39; h15odds = 5; h23odds = 11; h24odds = 47; h25odds = 6; h34odds = 23; h35odds = 3; h45odds = 12; break; case 11: h12odds = 9; h13odds = 21; h14odds = 11; h15odds = 36; h23odds = 6; h24odds = 3; h25odds = 10; h34odds = 7; h35odds = 23; h45odds = 12; break; case 12: h12odds = 19; h13odds = 3; h14odds = 6; h15odds = 25; h23odds = 9; h24odds = 18; h25odds = 76; h34odds = 4; h35odds = 17; h45odds = 34; break; case 13: h12odds = 10; h13odds = 7; h14odds = 6; h15odds = 4; h23odds = 23; h24odds = 20; h25odds = 13; h34odds = 14; h35odds = 9; h45odds = 8; break; case 14: h12odds = 2; h13odds = 34; h14odds = 8; h15odds = 5; h23odds = 49; h24odds = 12; h25odds = 7; h34odds = 200; h35odds = 119; h45odds = 29; break; case 15: h12odds = 95; h13odds = 4; h14odds = 9; h15odds = 37; h23odds = 21; h24odds = 49; h25odds = 200; h34odds = 2; h35odds = 8; h45odds = 19; break; case 16: h12odds = 59; h13odds = 5; h14odds = 23; h15odds = 18; h23odds = 9; h24odds = 42; h25odds = 33; h34odds = 4; h35odds = 3; h45odds = 14; break; case 17: h12odds = 32; h13odds = 44; h14odds = 109; h15odds = 25; h23odds = 5; h24odds = 12; h25odds = 3; h34odds = 17; h35odds = 4; h45odds = 10; break; case 18: h12odds = 4; h13odds = 8; h14odds = 13; h15odds = 5; h23odds = 10; h24odds = 16; h25odds = 6; h34odds = 33; h35odds = 12; h45odds = 20; break; case 19: h12odds = 2; h13odds = 7; h14odds = 5; h15odds = 8; h23odds = 15; h24odds = 11; h25odds = 17; h34odds = 38; h35odds = 61; h45odds = 43; break; case 20: h12odds = 38; h13odds = 2; h14odds = 7; h15odds = 24; h23odds = 16; h24odds = 60; h25odds = 200; h34odds = 3; h35odds = 10; h45odds = 37; break; case 21: h12odds = 8; h13odds = 161; h14odds = 12; h15odds = 24; h23odds = 26; h24odds = 2; h25odds = 4; h34odds = 40; h35odds = 80; h45odds = 6; break; case 22: h12odds = 6; h13odds = 12; h14odds = 3; h15odds = 36; h23odds = 16; h24odds = 4; h25odds = 49; h34odds = 8; h35odds = 11; h45odds = 24; break; case 23: h12odds = 8; h13odds = 29; h14odds = 200; h15odds = 9; h23odds = 6; h24odds = 42; h25odds = 2; h34odds = 159; h35odds = 7; h45odds = 47; break; case 24: h12odds = 2; h13odds = 47; h14odds = 108; h15odds = 18; h23odds = 17; h24odds = 6; h25odds = 3; h34odds = 20; h35odds = 16; h45odds = 19; break; case 25: h12odds = 8; h13odds = 43; h14odds = 108; h15odds = 18; h23odds = 17; h24odds = 6; h25odds = 3; h34odds = 20; h35odds = 16; h45odds = 19; break; case 26: h12odds = 2; h13odds = 8; h14odds = 4; h15odds = 15; h23odds = 14; h24odds = 7; h25odds = 26; h34odds = 29; h35odds = 107; h45odds = 88; break; case 27: h12odds = 5; h13odds = 7; h14odds = 28; h15odds = 14; h23odds = 3; h24odds = 12; h25odds = 6; h34odds = 16; h35odds = 8; h45odds = 33; break; case 28: h12odds = 3; h13odds = 19; h14odds = 2; h15odds = 21; h23odds = 38; h24odds = 4; h25odds = 48; h34odds = 9; h35odds = 99; h45odds = 24; break; case
16Step 16Program the countdown clock)
The countdown clock also needs to have the common ground, and pin 13 (the backlight) is hooked also connected to pin 2 on the start Mega switch board.
#include <liquidcrystal.h> LiquidCrystal counter(12,11,10,9,8,7); int count = 30; int count1 = 0; int x = 0; long lastrace = 0; byte LT[8] = { B00111, B01111, B11111, B11111, B11111, B11111, B11111, B11111 }; byte UB[8] = { B11111, B11111, B11111, B00000, B00000, B00000, B00000, B00000 }; byte RT[8] = { B11100, B11110, B11111, B11111, B11111, B11111, B11111, B11111 }; byte LL[8] = { B11111, B11111, B11111, B11111, B11111, B11111, B01111, B00111 }; byte LB[8] = { B00000, B00000, B00000, B00000, B00000, B11111, B11111, B11111 }; byte LR[8] = { B11111, B11111, B11111, B11111, B11111, B11111, B11110, B11100 }; byte MB[8] = { B11111, B11111, B11111, B00000, B00000, B00000, B11111, B11111 }; byte block[8] = { B11111, B11111, B11111, B11111, B11111, B11111, B11111, B11111 }; void setup() { // put your setup code here, to run once: pinMode(13, OUTPUT); digitalWrite(13, LOW); counter.begin(16,2); Serial.begin(9600); counter.createChar(0,LT); counter.createChar(1,UB); counter.createChar(2,RT); counter.createChar(3,LL); counter.createChar(4,LB); counter.createChar(5,LR); counter.createChar(6,MB); counter.createChar(7,block); } void loop() { if (Serial.available() > 0 && Serial.available() != lastrace) { lastrace = Serial.available(); digitalWrite(13, HIGH); while (count > 0) { if (count < 100 && count > 9) { // first digit 99 - 10 count1 = count * .1; // 34 = 3.4 count1 = (int)count1; // 3 x = 5; if (count1 == 0) { custom0(); } if (count1 == 1) { custom1(); } if (count1 == 2) { custom2(); } if (count1 == 3) { custom3(); } if (count1 == 4) { custom4(); } if (count1 == 5) { custom5(); } if (count1 == 6) { custom6(); } if (count1 == 7) { custom7(); } if (count1 == 8) { custom8(); } if (count1 == 9) { custom9(); } // second digit 99 - 10 count1 = count * .1; // 34 = 3.4 count1 = (int)count1; // 3.4 = 3 count1 = count1 * 10; // 3 = 30 count1 = count - count1;// 34 - 30 = 4 x = 9; if (count1 == 0) { custom0(); } if (count1 == 1) { custom1(); } if (count1 == 2) { custom2(); } if (count1 == 3) { custom3(); } if (count1 == 4) { custom4(); } if (count1 == 5) { custom5(); } if (count1 == 6) { custom6(); } if (count1 == 7) { custom7(); } if (count1 == 8) { custom8(); } if (count1 == 9) { custom9(); } } else if (count < 10) { x = 6; if (count == 0) { custom0(); } if (count == 1) { custom1(); } if (count == 2) { custom2(); } if (count == 3) { custom3(); } if (count == 4) { custom4(); } if (count == 5) { custom5(); } if (count == 6) { custom6(); } if (count == 7) { custom7(); } if (count == 8) { custom8(); } if (count == 9) { custom9(); } } delay(1000); counter.clear(); count--; } } digitalWrite(13, LOW); delay(5000); count = 30; } void custom0() { // uses segments to build the number 0 counter.setCursor(0+x,0); // set cursor to column 0, line 0 (first row) counter.write(8); // call each segment to create counter.write(1); // top half of the number counter.write(2); counter.setCursor(0+x, 1); // set cursor to colum 0, line 1 (second row) counter.write(3); // call each segment to create counter.write(4); // bottom half of the number counter.write(5); } void custom1() { counter.setCursor(0+x,0); counter.write(1); counter.write(2); counter.setCursor(0+x,1); counter.write(4); counter.write(7); counter.write(4); } void custom2() { counter.setCursor(0+x,0); counter.write(6); counter.write(6); counter.write(2); counter.setCursor(0+x, 1); counter.write(3); counter.write(4); counter.write(4); } void custom3() { counter.setCursor(0+x,0); counter.write(6); counter.write(6); counter.write(2); counter.setCursor(0+x, 1); counter.write(4); counter.write(4); counter.write(5); } void custom4() { counter.setCursor(0+x,0); counter.write(3); counter.write(4); counter.write(7); counter.setCursor(2+x, 1); counter.write(7); } void custom5() { counter.setCursor(0+x,0); counter.write(3); counter.write(6); counter.write(6); counter.setCursor(0+x, 1); counter.write(4); counter.write(4); counter.write(5); } void custom6() { counter.setCursor(0+x,0); counter.write(8); counter.write(6); counter.write(6); counter.setCursor(0+x, 1); counter.write(3); counter.write(4); counter.write(5); } void custom7() { counter.setCursor(0+x,0); counter.write(1); counter.write(1); counter.write(2); counter.setCursor(2+x, 1); counter.write(7); } void custom8() { counter.setCursor(0+x,0); counter.write(8); counter.write(6); counter.write(2); counter.setCursor(0+x, 1); counter.write(3); counter.write(4); counter.write(5); } void custom9() { counter.setCursor(0+x,0); counter.write(8); counter.write(6); counter.write(2); counter.setCursor(2+x, 1); counter.write(7); }
17Step 17Build betting station)
The betting station needs to have 10 buttons, 10 7-segment leds with shift registers, and I have a LCD display on mine. If you plan on using it as a coin machine you will also need a coin in, and cash out. I have two versions of code: one for fun gaming with no coins and one for actual betting.
I will share the fun gaming version.
18Step 18Program the betting station)
The betting station needs to know the odds of the current race to know what to pay out if you win. It also needs to know the race winner.
Once again this is the fun version, so there is no code here for coin in, or cash out.
#include <liquidcrystal.h> LiquidCrystal lcd(12, 13, 11, 10, 9, 8); byte dec_digits[] = {0b11000000,0b11111001,0b10100100,0b10110000,0b10011001,0b10010010,0b10000011,0b11111000,0b10000000,0b10011000 }; // button tests boolean h12 = false; boolean h13 = false; boolean h14 = false; boolean h15 = false; boolean h23 = false; boolean h24 = false; boolean h25 = false; boolean h34 = false; boolean h35 = false; boolean h45 = false; // odds on horse combination int h12bet = 0; int h13bet = 0; int h14bet = 0; int h15bet = 0; int h23bet = 0; int h24bet = 0; int h25bet = 0; int h34bet = 0; int h35bet = 0; int h45bet = 0; int racenumber = 0; // bets on horse combination int bet12 = 0; int bet13 = 0; int bet14 = 0; int bet15 = 0; int bet23 = 0; int bet24 = 0; int bet25 = 0; int bet34 = 0; int bet35 = 0; int bet45 = 0; long lastrace = 0; long x = 0; long y = 0; int credits = 75; void setup() { pinMode(22, OUTPUT); pinMode(23, OUTPUT); pinMode(24, OUTPUT); pinMode(25, OUTPUT); pinMode(26, OUTPUT); pinMode(27, OUTPUT); pinMode(28, OUTPUT); pinMode(29, OUTPUT); pinMode(30, OUTPUT); pinMode(31, OUTPUT); pinMode(32, OUTPUT); pinMode(33, OUTPUT); pinMode(34, OUTPUT); pinMode(35, OUTPUT); pinMode(36, OUTPUT); pinMode(37, OUTPUT); pinMode(38, OUTPUT); pinMode(39, OUTPUT); pinMode(40, OUTPUT); pinMode(41, OUTPUT); pinMode(42, OUTPUT); pinMode(43, OUTPUT); pinMode(44, OUTPUT); pinMode(45, OUTPUT); pinMode(46, OUTPUT); pinMode(47, OUTPUT); pinMode(48, OUTPUT); pinMode(49, OUTPUT); pinMode(50, OUTPUT); pinMode(51, OUTPUT); pinMode(6, INPUT); pinMode(5, INPUT); pinMode(4, INPUT); pinMode(3, INPUT); pinMode(2, INPUT); pinMode(55, INPUT); pinMode(56, INPUT); pinMode(57, INPUT); pinMode(58, INPUT); pinMode(59, INPUT); pinMode(7, INPUT); // signal from countdown clock pinMode(53, INPUT); // cashout button pinMode(54, OUTPUT); // cashout signal to turn on hopper pinMode(21, INPUT); // coin in break beam pinMode(20, INPUT); // cashout sensor digitalWrite(54, LOW); lcd.begin(16,2); lcd.setCursor(0,0); lcd.print("Fun Mode Race"); lcd.setCursor(0,4); lcd.print(racenumber); lcd.print(" of 25"); lcd.setCursor(10,2); clearscreen(); Serial.begin(9600); } void clearscreen() { digitalWrite(23, LOW); shiftOut(22, 24, MSBFIRST, dec_digits[0]); digitalWrite(23, HIGH); digitalWrite(26, LOW); shiftOut(25, 27, MSBFIRST, dec_digits[0]); digitalWrite(26, HIGH); digitalWrite(29, LOW); shiftOut(28, 30, MSBFIRST, dec_digits[0]); digitalWrite(29, HIGH); digitalWrite(32, LOW); shiftOut(31, 33, MSBFIRST, dec_digits[0]); digitalWrite(32, HIGH); digitalWrite(35, LOW); shiftOut(34, 36, MSBFIRST, dec_digits[0]); digitalWrite(35, HIGH); digitalWrite(38, LOW); shiftOut(37, 39, MSBFIRST, dec_digits[0]); digitalWrite(38, HIGH); digitalWrite(41, LOW); shiftOut(40, 42, MSBFIRST, dec_digits[0]); digitalWrite(41, HIGH); digitalWrite(44, LOW); shiftOut(43, 45, MSBFIRST, dec_digits[0]); digitalWrite(44, HIGH); digitalWrite(47, LOW); shiftOut(46, 48, MSBFIRST, dec_digits[0]); digitalWrite(47, HIGH); digitalWrite(50, LOW); shiftOut(49, 51, MSBFIRST, dec_digits[0]); digitalWrite(50, HIGH); bet12 = 0; bet13 = 0; bet14 = 0; bet15 = 0; bet23 = 0; bet24 = 0; bet25 = 0; bet34 = 0; bet35 = 0; bet45 = 0; } void loop() { if (Serial.available() > 0 && Serial.available() != lastrace) { x = Serial.parseInt(); lastrace = x; // get digit 5 for last race y = int(x * .1); // 12345 = 1234.5 y = y * 10; // 12340 y = x - y; //12345 - 12340 = 5 // pay out for last race if (y == 0) { lcd.clear(); lcd.setCursor(0,0); lcd.print("1-2 Winner "); lcd.print(h12bet * bet12); lcd.setCursor(0,2); credits = credits + (h12bet * bet12); lcd.print("Credits = "); lcd.setCursor(10,2); lcd.print(credits); } else if (y == 1) { lcd.clear(); lcd.setCursor(0,0); lcd.print("1-3 Winner "); lcd.print(h13bet * bet13); lcd.setCursor(0,2); credits = credits + (h13bet * bet13); lcd.print("Credits = "); lcd.setCursor(10,2); lcd.print(credits); } else if (y == 2) { lcd.clear(); lcd.setCursor(0,0); lcd.print("1-4 Winner "); lcd.print(h14bet * bet14); lcd.setCursor(0,2); credits = credits + (h12bet * bet12); lcd.print("Credits = "); lcd.setCursor(10,2); lcd.print(credits); } else if (y == 3) { lcd.clear(); lcd.setCursor(0,0); lcd.print("1-5 Winner "); lcd.print(h15bet * bet15); lcd.setCursor(0,2); credits = credits + (h15bet * bet15); lcd.print("Credits = "); lcd.setCursor(10,2); lcd.print(credits); } else if (y == 4) { lcd.clear(); lcd.setCursor(0,0); lcd.print("2-3 Winner "); lcd.print(h23bet * bet23); lcd.setCursor(0,2); credits = credits + (h23bet * bet23); lcd.print("Credits = "); lcd.setCursor(10,2); lcd.print(credits); } else if (y == 5) { lcd.clear(); lcd.setCursor(0,0); lcd.print("2-4 Winner "); lcd.print(h24bet * bet24); lcd.setCursor(0,2); credits = credits + (h24bet * bet24); lcd.print("Credits = "); lcd.setCursor(10,2); lcd.print(credits); } else if (y == 6) { lcd.clear(); lcd.setCursor(0,0); lcd.print("2-5 Winner "); lcd.print(h25bet * bet25); lcd.setCursor(0,2); credits = credits + (h25bet * bet25); lcd.print("Credits = "); lcd.setCursor(10,2); lcd.print(credits); } else if (y == 7) { lcd.clear(); lcd.setCursor(0,0); lcd.print("3-4 Winner "); lcd.print(h34bet * bet34); lcd.setCursor(0,2); credits = credits + (h34bet * bet34); lcd.print("Credits = "); lcd.setCursor(10,2); lcd.print(credits); } else if (y == 8) { lcd.clear(); lcd.setCursor(0,0); lcd.print("3-5 Winner "); lcd.print(h35bet * bet35); lcd.setCursor(0,2); credits = credits + (h35bet * bet35); lcd.print("Credits = "); lcd.setCursor(10,2); lcd.print(credits); } else if (y == 9) { lcd.clear(); lcd.setCursor(0,0); lcd.print("4-5 Winner "); lcd.print(h45bet * bet45); lcd.setCursor(0,2); credits = credits + (h45bet * bet45); lcd.print("Credits = "); lcd.setCursor(10,2); lcd.print(credits); } delay(2000); racenumber++; lcd.clear(); lcd.setCursor(0,0); lcd.print("Number "); lcd.print(racenumber); lcd.print(" of 25"); lcd.setCursor(0,2); lcd.print("Credits = "); lcd.setCursor(10,2); lcd.print(credits); int nextraceodds = int(x * .001); // 12345 = 12 switch(nextraceodds) { case 10: h12bet = 14; h13bet = 9; h14bet = 39; h15bet = 5; h23bet = 11; h24bet = 47; h25bet = 6; h34bet = 23; h35bet = 3; h45bet = 12; break; case 11: h12bet = 9; h13bet = 21; h14bet = 11; h15bet = 36; h23bet = 6; h24bet = 3; h25bet = 10; h34bet = 7; h35bet = 23; h45bet = 12; break; case 12: h12bet = 19; h13bet = 3; h14bet = 6; h15bet = 25; h23bet = 9; h24bet = 18; h25bet = 76; h34bet = 4; h35bet = 17; h45bet = 34; break; case 13: h12bet = 10; h13bet = 7; h14bet = 6; h15bet = 4; h23bet = 23; h24bet = 20; h25bet = 13; h34bet = 14; h35bet = 9; h45bet = 8; break; case 14: h12bet = 2; h13bet = 34; h14bet = 8; h15bet = 5; h23bet = 49; h24bet = 12; h25bet = 7; h34bet = 200; h35bet = 119; h45bet = 29; break; case 15: h12bet = 95; h13bet = 4; h14bet = 9; h15bet = 37; h23bet = 21; h24bet = 49; h25bet = 200; h34bet = 2; h35bet = 8; h45bet = 19; break; case 16: h12bet = 59; h13bet = 5; h14bet = 23; h15bet = 18; h23bet = 9; h24bet = 42; h25bet = 33; h34bet = 4; h35bet = 3; h45bet = 14; break; case 17: h12bet = 32; h13bet = 44; h14bet = 109; h15bet = 25; h23bet = 5; h24bet = 12; h25bet = 3; h34bet = 17; h35bet = 4; h45bet = 10; break; case 18: h12bet = 4; h13bet = 8; h14bet = 13; h15bet = 5; h23bet = 10; h24bet = 16; h25bet = 6; h34bet = 33; h35bet = 12; h45bet = 20; break; case 19: h12bet = 2; h13bet = 7; h14bet = 5; h15bet = 8; h23bet = 15; h24bet = 11; h25bet = 17; h34bet = 38; h35bet = 61; h45bet = 43; break; case 20: h12bet = 38; h13bet = 2; h14bet = 7; h15bet = 24; h23bet = 16; h24bet = 60; h25bet = 200; h34bet = 3; h35bet = 10; h45bet = 37; break; case 21: h12bet = 8; h13bet = 161; h14bet = 12; h15bet = 24; h23bet = 26; h24bet = 2; h25bet = 4; h34bet = 40; h35bet = 80; h45bet = 6; break; case 22: h12bet = 6; h13bet = 12; h14bet = 3; h15bet = 36; h23bet = 16; h24bet = 4; h25bet = 49; h34bet = 8; h35bet = 11; h45bet = 24; break; case 23: h12bet = 8; h13bet = 29; h14bet = 200; h15bet = 9; h23bet = 6; h24bet = 42; h25bet = 2; h34bet = 159; h35bet = 7; h45bet = 47; break; case 24: h12bet = 2; h13bet = 47; h14bet = 108; h15bet = 18; h23bet = 17; h24bet = 6; h25bet = 3; h34bet = 20; h35bet = 16; h45bet = 19; break; case 25: h12bet = 8; h13bet = 43; h14bet = 108; h15bet = 18; h23bet = 17; h24bet = 6; h25bet = 3; h34bet = 20; h35bet = 16; h45bet = 19; break; case 26: h12bet = 2; h13bet = 8; h14bet = 4; h15bet = 15; h23bet = 14; h24bet = 7; h25bet = 26; h34bet = 29; h35bet = 107; h45bet = 88; break; case 27: h12bet = 5; h13bet = 7; h14bet = 28; h15bet = 14; h23bet = 3; h24bet = 12; h25bet = 6; h34bet = 16; h35bet = 8; h45bet = 33; break; case 28: h12bet = 3; h13bet = 19; h14bet = 2; h15bet = 21; h23bet = 38; h24bet = 4; h25bet = 48; h34bet = 9; h35bet = 99; h45bet = 24; break; case 29: h12bet = 27; h13bet = 9; h14bet = 136; h15bet = 13; h23bet = 4; h24bet = 61; h25bet = 6; h34bet = 20; h35bet = 2; h45bet = 3; break; case 30: h12bet = 2; h13bet = 7; h14bet = 63; h15bet = 12; h23bet = 3; h24bet = 26; h25bet = 5; h34bet = 21; h35bet = 4; h45bet = 36; break; case 31: h12bet = 11; h13bet = 14; h14bet = 6; h15bet = 43; h23bet = 7; h24bet = 3; h25bet = 21; h34bet = 4; h35bet = 28; h45bet = 12; break; case 32: h12bet = 8; h13bet = 86; h14bet = 18; h15bet = 17; h23bet = 19; h24bet = 3; h25bet = 4; h34bet = 30; h35bet = 40; h45bet = 6; break; case 33: h12bet = 14; h13bet = 29; h14bet = 6; h15bet = 109; h23bet = 9; h24bet = 2; h25bet = 34; h34bet = 4; h35bet = 70; h45bet = 15; break; case 34: h12bet = 5; h13bet = 9; h14bet = 17; h15bet = 16; h23bet = 4; h24bet = 8; h25bet = 7; h34bet = 14; h35bet = 13; h45bet = 25; break; } // set 7 segment leds back to 0 clearscreen(); } if (racenumber == 25) { lcd.clear(); lcd.setCursor(0,0); lcd.print("You finished with "); lcd.setCursor(0,2); lcd.print("Credits = "); lcd.setCursor(10,2); lcd.print(credits); delay(2000); lcd.clear(); lcd.setCursor(0,0); lcd.print("How did you do?"); lcd.setCursor(0,2); lcd.print("Credits = "); lcd.setCursor(10,2); lcd.print(credits); delay(3000); lcd.clear(); lcd.setCursor(0,0); lcd.print("New Game Starts"); lcd.setCursor(0,2); lcd.print("Next Race"); racenumber = 0; credits = 75; } else if (digitalRead(7) == HIGH) { // start else racenumber < 25 // h12 button test if (h12 == true && digitalRead(6) == LOW) { h12 = false; } if (digitalRead(6) == HIGH && bet12 < 9 && credits > 0 && h12 == false) { bet12++; digitalWrite(23, LOW); shiftOut(22, 24, MSBFIRST, dec_digits[bet12]); digitalWrite(23, HIGH); credits--; lcd.clear(); lcd.setCursor(0,0); lcd.print("Bet 1-2 @ "); lcd.print(h12bet); lcd.setCursor(0,2); lcd.print("Credits = "); lcd.setCursor(10,2); lcd.print(credits); h12 = true; } // h13 button test if (h13 == true && digitalRead(5) == LOW) { h13 = false; } if (digitalRead(5) == HIGH && bet13 < 9 && credits > 0 && h13 == false) { bet13++; digitalWrite(26, LOW); shiftOut(25, 27, MSBFIRST, dec_digits[bet13]); digitalWrite(26, HIGH); credits--; lcd.clear(); lcd.setCursor(0,0); lcd.print("Bet 1-3 @ "); lcd.print(h13bet); lcd.setCursor(0,2); lcd.print("Credits = "); lcd.setCursor(10,2); lcd.print(credits); h13 = true; } // h14 button test if (h14 == true && digitalRead(4) == LOW) { h14 = false; } if (digitalRead(4) == HIGH && bet14 < 9 && credits > 0 && h14 == false) { bet14++; digitalWrite(29, LOW); shiftOut(28, 30, MSBFIRST, dec_digits[bet14]); digitalWrite(29, HIGH); credits--; lcd.clear(); lcd.setCursor(0,0); lcd.print("Bet 1-4 @ "); lcd.print(h14bet); lcd.setCursor(0,2); lcd.print("Credits = "); lcd.setCursor(10,2); lcd.print(credits); h14 = true; } // h15 button test if (h15 == true && digitalRead(3) == LOW) { h15 = false; } if (digitalRead(3) == HIGH && bet15 < 9 && credits > 0 && h15 == false) { bet15++; digitalWrite(32, LOW); shiftOut(31, 33, MSBFIRST, dec_digits[bet15]); digitalWrite(32, HIGH); credits--; lcd.clear(); lcd.setCursor(0,0); lcd.print("Bet 1-5 @ "); lcd.print(h15bet); lcd.setCursor(0,2); lcd.print("Credits = "); lcd.setCursor(10,2); lcd.print(credits); h15 = true; } // h23 button test if (h23 == true && digitalRead(2) == LOW) { h23 = false; } if (digitalRead(2) == HIGH && bet23 < 9 && credits > 0 && h23 == false) { bet23++; digitalWrite(35, LOW); shiftOut(34, 36, MSBFIRST, dec_digits[bet23]); digitalWrite(35, HIGH); credits--; lcd.clear(); lcd.setCursor(0,0); lcd.print("Bet 2-3 @ "); lcd.print(h23bet); lcd.setCursor(0,2); lcd.print("Credits = "); lcd.setCursor(10,2); lcd.print(credits); h23 = true; } // h24 button test if (h24 == true && digitalRead(55) == LOW) { h24 = false; } if (digitalRead(55) == HIGH && bet24 < 9 && credits > 0 && h24 == false) { bet24++; digitalWrite(38, LOW); shiftOut(37, 39, MSBFIRST, dec_digits[bet24]); digitalWrite(38, HIGH); credits--; lcd.clear(); lcd.setCursor(0,0); lcd.print("Bet 2-4 @ "); lcd.print(h24bet); lcd.setCursor(0,2); lcd.print("Credits = "); lcd.setCursor(10,2); lcd.print(credits); h24 = true; } // h25 button test if (h25 == true && digitalRead(56) == LOW) { h25 = false; } if (digitalRead(56) == HIGH && bet25 < 9 && credits > 0 && h25 == false) { bet25++; digitalWrite(41, LOW); shiftOut(40, 42, MSBFIRST, dec_digits[bet25]); digitalWrite(41, HIGH); credits--; lcd.clear(); lcd.setCursor(0,0); lcd.print("Bet 2-5 @ "); lcd.print(h25bet); lcd.setCursor(0,2); lcd.print("Credits = "); lcd.setCursor(10,2); lcd.print(credits); h25 = true; } // h34 button test if (h34 == true && digitalRead(57) == LOW) { h34 = false; } if (digitalRead(57) == HIGH && bet34 < 9 && credits > 0 && h34 == false) { bet34++; digitalWrite(44, LOW); shiftOut(43, 45, MSBFIRST, dec_digits[bet34]); digitalWrite(44, HIGH); credits--; lcd.clear(); lcd.setCursor(0,0); lcd.print("Bet 3-4 @ "); lcd.print(h34bet); lcd.setCursor(0,2); lcd.print("Credits = "); lcd.setCursor(10,2); lcd.print(credits); h34 = true; } // h35 button test if (h35 == true && digitalRead(58) == LOW) { h35 = false; } if (digitalRead(58) == HIGH && bet35 < 9 && credits > 0 && h35 == false) { bet35++; digitalWrite(47, LOW); shiftOut(46, 48, MSBFIRST, dec_digits[bet35]); digitalWrite(47, HIGH); credits--; lcd.clear(); lcd.setCursor(0,0); lcd.print("Bet 3-5 @ "); lcd.print(h35bet); lcd.setCursor(0,2); lcd.print("Credits = "); lcd.setCursor(10,2); lcd.print(credits); h35 = true; } // h45 button test if (h45 == true && digitalRead(59) == LOW) { h45 = false; } if (digitalRead(59) == HIGH && bet45 < 9 && credits > 0 && h45 == false) { bet45++; digitalWrite(50, LOW); shiftOut(49, 51, MSBFIRST, dec_digits[bet45]); digitalWrite(50, HIGH); credits--; lcd.clear(); lcd.setCursor(0,0); lcd.print("Bet 4-5 @ "); lcd.print(h45bet); lcd.setCursor(0,2); lcd.print("Credits = "); lcd.setCursor(10,2); lcd.print(credits); h45 = true; } } // end else racenumber < 25 }
19Step 19
Discussions
Become a Hackaday.io Member
Create an account to leave a comment. Already have an account? Log In.