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

Dependencies:   GoalCounter WS2812 PixelArray

Revision:
2:f04959a6fd61
Parent:
1:ffac56d434b3
Child:
3:d74c47d2e933
--- a/main.cpp	Thu Jan 10 15:48:39 2019 +0000
+++ b/main.cpp	Fri Jan 11 18:29:58 2019 +0000
@@ -7,6 +7,7 @@
 #define NUM_COLORS 1
 #define NUM_LEDS_PER_COLOR 2
 #define COLOR 0x2f2020 
+#define DEBUG 1
 
 // Initialize instace of a pixel array for LED strip 1 ar 2
 PixelArray px1(WS2812_BUF);
@@ -14,24 +15,27 @@
 
 // The given numbers are calculated for the K64F MCU
 WS2812 ws1(D4, WS2812_BUF, 0, 5, 5, 0);
-WS2812 ws2(D4, WS2812_BUF, 0, 5, 5, 0);
+WS2812 ws2(D5, WS2812_BUF, 0, 5, 5, 0);
 
 // Initialize instances of GoalCounter classes for counting goals
 GoalCounter gc1(D2);
 GoalCounter gc2(D3);
 
 // Initialize instances of Ticker classes for handling goal counters
-Ticker bank1;
-Ticker bank2; 
+//Ticker bank1;
+//Ticker bank2; 
 
 // For debug use 
 Serial pc(USBTX, USBRX);
 
-float balltime = 0;
-float speed = 0;
+float balltime1 = 0;
+float speed1 = 0;
 
-uint8_t score_1 = 0;
-uint8_t score_2 = 0;
+float balltime2 = 0;
+float speed2 = 0;
+
+uint8_t score1 = 0;
+uint8_t score2 = 0;
 
 void set_score(uint8_t score, PixelArray px, WS2812 ws) {
     for (int i=0; i < score; i++) {
@@ -45,17 +49,18 @@
     ws.write(px.getBuf()); 
 }
 
-void print_info() {
-     if (gc1.goal) {
-            score_1 = gc1.get_score();
-            balltime = gc1.get_balltime();
-            speed = gc1.get_ballspeed();
-            pc.printf("Score : %d \n\r", score_1);
-            pc.printf("Time of ball pass : %f seconds\n\r", balltime);
-            pc.printf("Speed of ball (34 mm diameter) : %f kph\n\r", speed);
-            gc1.goal = 0;
-            }
-    }
+void print_score(uint8_t score, float balltime, float speed) {
+    pc.printf("Score : %d \n\r", score);
+    pc.printf("Time of ball pass : %f seconds\n\r", balltime);
+    pc.printf("Speed of ball (34 mm diameter) : %f kph\n\r", speed);
+}
+
+void get_info(uint8_t * score, float * balltime, float * speed, GoalCounter gc) {
+    *score = gc.get_score();
+    *balltime = gc.get_balltime();
+    *speed = gc.get_ballspeed();
+    gc.is_goal = 0;
+}
 
 int main()
 {
@@ -69,7 +74,17 @@
 //    bank2.attach(&print_info, 0.5);
     
     while(1) {
-        print_info();
-        wait(0.5);
+        if (gc1.is_goal) {
+            get_info(&score1, &balltime1, &speed1, gc1);
+            set_score(score1, px1, ws1);
+            if (DEBUG)
+                print_score(score1, balltime1, speed1);
+        }
+        else if (gc2.is_goal) {
+            get_info(&score2, &balltime2, &speed2, gc2);
+            set_score(score2, px2, ws2);
+            if (DEBUG)
+                print_score(score2, balltime2, speed2);
+        }
     }
 }