Simple implementation of goal counter for table football using laser diode and phototransistor.

Dependencies:   GoalCounter WS2812 PixelArray

Committer:
nxf46245
Date:
Sun Jan 13 14:51:45 2019 +0000
Revision:
5:95cb2348af01
Parent:
4:5a11a84fac69
Child:
6:a93ee22232f2
Added second instance of a GoalCounter class

Who changed what in which revision?

UserRevisionLine numberNew contents of line
nxf46245 0:24f846b68c77 1 #include "mbed.h"
nxf46245 0:24f846b68c77 2 #include "WS2812.h"
nxf46245 0:24f846b68c77 3 #include "PixelArray.h"
nxf46245 0:24f846b68c77 4 #include "GoalCounter.h"
nxf46245 0:24f846b68c77 5
nxf46245 0:24f846b68c77 6 #define WS2812_BUF 10
nxf46245 0:24f846b68c77 7 #define NUM_COLORS 1
nxf46245 0:24f846b68c77 8 #define NUM_LEDS_PER_COLOR 2
nxf46245 4:5a11a84fac69 9 //#define COLOR 0x2f2020
nxf46245 2:f04959a6fd61 10 #define DEBUG 1
nxf46245 0:24f846b68c77 11
nxf46245 3:d74c47d2e933 12 //Initialize instances of Timers
nxf46245 4:5a11a84fac69 13 Timer timer1;
nxf46245 5:95cb2348af01 14 Timer timer2;
nxf46245 3:d74c47d2e933 15
nxf46245 1:ffac56d434b3 16 // Initialize instace of a pixel array for LED strip 1 ar 2
nxf46245 1:ffac56d434b3 17 PixelArray px1(WS2812_BUF);
nxf46245 5:95cb2348af01 18 PixelArray px2(WS2812_BUF);
nxf46245 0:24f846b68c77 19
nxf46245 1:ffac56d434b3 20 // The given numbers are calculated for the K64F MCU
nxf46245 1:ffac56d434b3 21 WS2812 ws1(D4, WS2812_BUF, 0, 5, 5, 0);
nxf46245 5:95cb2348af01 22 WS2812 ws2(D5, WS2812_BUF, 0, 5, 5, 0);
nxf46245 0:24f846b68c77 23
nxf46245 4:5a11a84fac69 24 // For debug use
nxf46245 4:5a11a84fac69 25 Serial pc(USBTX, USBRX);
nxf46245 3:d74c47d2e933 26
nxf46245 4:5a11a84fac69 27 GoalCounter gc(D2, &timer1);
nxf46245 5:95cb2348af01 28 GoalCounter gc2(D3, &timer2);
nxf46245 1:ffac56d434b3 29
nxf46245 2:f04959a6fd61 30 float balltime1 = 0;
nxf46245 2:f04959a6fd61 31 float speed1 = 0;
nxf46245 1:ffac56d434b3 32
nxf46245 2:f04959a6fd61 33 float balltime2 = 0;
nxf46245 2:f04959a6fd61 34 float speed2 = 0;
nxf46245 2:f04959a6fd61 35
nxf46245 2:f04959a6fd61 36 uint8_t score1 = 0;
nxf46245 2:f04959a6fd61 37 uint8_t score2 = 0;
nxf46245 1:ffac56d434b3 38
nxf46245 4:5a11a84fac69 39 void set_score(uint8_t score, PixelArray * px, WS2812 * ws) {
nxf46245 4:5a11a84fac69 40 px->SetAll(0);
nxf46245 4:5a11a84fac69 41
nxf46245 1:ffac56d434b3 42 for (int i=0; i < score; i++) {
nxf46245 4:5a11a84fac69 43 px->Set(i, 0x2f2020);
nxf46245 1:ffac56d434b3 44 }
nxf46245 1:ffac56d434b3 45
nxf46245 4:5a11a84fac69 46 px->SetAllI(0x0f);
nxf46245 4:5a11a84fac69 47 ws->write(px->getBuf());
nxf46245 0:24f846b68c77 48 }
nxf46245 1:ffac56d434b3 49
nxf46245 2:f04959a6fd61 50 void print_score(uint8_t score, float balltime, float speed) {
nxf46245 2:f04959a6fd61 51 pc.printf("Score : %d \n\r", score);
nxf46245 2:f04959a6fd61 52 pc.printf("Time of ball pass : %f seconds\n\r", balltime);
nxf46245 2:f04959a6fd61 53 pc.printf("Speed of ball (34 mm diameter) : %f kph\n\r", speed);
nxf46245 2:f04959a6fd61 54 }
nxf46245 2:f04959a6fd61 55
nxf46245 4:5a11a84fac69 56 void get_info(uint8_t * score, float * balltime, float * speed, GoalCounter * gc) {
nxf46245 4:5a11a84fac69 57 *score = gc->get_score();
nxf46245 4:5a11a84fac69 58 *balltime = gc->get_balltime();
nxf46245 4:5a11a84fac69 59 *speed = gc->get_ballspeed();
nxf46245 2:f04959a6fd61 60 }
nxf46245 0:24f846b68c77 61
nxf46245 4:5a11a84fac69 62
nxf46245 0:24f846b68c77 63 int main()
nxf46245 0:24f846b68c77 64 {
nxf46245 1:ffac56d434b3 65
nxf46245 1:ffac56d434b3 66 ws1.useII(WS2812::GLOBAL); // use global intensity scaling
nxf46245 5:95cb2348af01 67 ws2.useII(WS2812::GLOBAL); // use global intensity scaling
nxf46245 4:5a11a84fac69 68
nxf46245 4:5a11a84fac69 69 pc.printf("Initialization... \n\r");
nxf46245 0:24f846b68c77 70 while(1) {
nxf46245 4:5a11a84fac69 71 if (gc.is_goal == 1) {
nxf46245 4:5a11a84fac69 72 get_info(&score1, &balltime1, &speed1, &gc);
nxf46245 4:5a11a84fac69 73 set_score(score1, &px1, &ws1);
nxf46245 4:5a11a84fac69 74 print_score(score1, balltime1, speed1);
nxf46245 4:5a11a84fac69 75 gc.is_goal = 0;
nxf46245 2:f04959a6fd61 76 }
nxf46245 5:95cb2348af01 77 else if (gc2.is_goal == 1) {
nxf46245 5:95cb2348af01 78 get_info(&score2, &balltime2, &speed2, &gc2);
nxf46245 5:95cb2348af01 79 set_score(score2, &px2, &ws2);
nxf46245 5:95cb2348af01 80 print_score(score2, balltime2, speed2);
nxf46245 5:95cb2348af01 81 gc2.is_goal = 0;
nxf46245 5:95cb2348af01 82 }
nxf46245 4:5a11a84fac69 83 if (score1 >= 10) {
nxf46245 4:5a11a84fac69 84 pc.printf("Game over \n\r");
nxf46245 4:5a11a84fac69 85 for (int i=1; i<= 10; i++) {
nxf46245 4:5a11a84fac69 86 pc.printf("Game score: \n\r");
nxf46245 4:5a11a84fac69 87 balltime1 = gc.get_balltime(i);
nxf46245 4:5a11a84fac69 88 speed1 = gc.get_ballspeed(i);
nxf46245 4:5a11a84fac69 89 print_score(i, balltime1, speed1);
nxf46245 4:5a11a84fac69 90 }
nxf46245 4:5a11a84fac69 91 return 0;
nxf46245 4:5a11a84fac69 92 }
nxf46245 4:5a11a84fac69 93
nxf46245 0:24f846b68c77 94 }
nxf46245 0:24f846b68c77 95 }