Simple implementation of goal counter for table football using laser diode and phototransistor.
Dependencies: GoalCounter WS2812 PixelArray
Revision 1:ffac56d434b3, committed 2019-01-10
- Comitter:
- nxf46245
- Date:
- Thu Jan 10 15:48:39 2019 +0000
- Parent:
- 0:24f846b68c77
- Child:
- 2:f04959a6fd61
- Commit message:
- Code refactoring
Changed in this revision
| main.cpp | Show annotated file Show diff for this revision Revisions of this file |
--- a/main.cpp Thu Jan 10 14:48:03 2019 +0000
+++ b/main.cpp Thu Jan 10 15:48:39 2019 +0000
@@ -8,90 +8,68 @@
#define NUM_LEDS_PER_COLOR 2
#define COLOR 0x2f2020
-// Initialize instace of a pixel array for LED strip
-PixelArray px(WS2812_BUF);
+// Initialize instace of a pixel array for LED strip 1 ar 2
+PixelArray px1(WS2812_BUF);
+PixelArray px2(WS2812_BUF);
-// The given numbers are for the K64F MCU
-WS2812 ws(D4, WS2812_BUF, 0, 5, 5, 0);
-GoalCounter gc(D2);
+// 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);
-//// Detector for the
-//InterruptIn detector1(D3);
-//InterruptIn detector2(D2);
-//
-//DigitalOut led1(LED1);
-//DigitalOut led2(LED2);
-//
-//Timer t;
+// 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;
+
+// For debug use
Serial pc(USBTX, USBRX);
-//
+
float balltime = 0;
float speed = 0;
+
uint8_t score_1 = 0;
-//
-void print_time() {
- pc.printf("The time taken was %f microseconds\n", balltime);
+uint8_t score_2 = 0;
+
+void set_score(uint8_t score, PixelArray px, WS2812 ws) {
+ for (int i=0; i < score; i++) {
+ px.Set(i, COLOR);
+ }
+ for (int i = score; i <= 10; i++) {
+ px.Set(i, 0);
+ }
+
+ px.SetAllI(0x0f);
+ ws.write(px.getBuf());
}
-//
-//void tstart() {
-// t.start();
-//}
-//
-//void tstop() {
-// t.stop();
-// balltime = t.read();
-// t.reset();
-//}
-//
-//void set_score(uint8_t score) {
-// for (int i=0; i < score; i++) {
-// px.Set(i, COLOR);
-// }
-// for (int i = score; i <= 10; i++) {
-// px.Set(i, 0);
-// }
-//
-// px.SetAllI(0x0f);
-//// ws.write_offsets(px.getBuf(),0,0,0);
-// 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;
+ }
+ }
int main()
{
- // Optional: set mode as PullUp/PullDown/PullNone/OpenDrain
- //mypin.mode(PullNone);
-//
-// detector1.fall(&tstart);
-// detector2.rise(&tstop);
+
+ ws1.useII(WS2812::GLOBAL); // use global intensity scaling
+ ws2.useII(WS2812::GLOBAL); // use global intensity scaling
- ws.useII(WS2812::GLOBAL); // use global intensity scaling
+ // Attach Ticker to functions
+
+// bank1.attach(&print_info, 0.5);
+// bank2.attach(&print_info, 0.5);
while(1) {
-
- // if (balltime != 0) {
-// pc.printf("Time of ball pass : %f seconds\n\r", balltime);
-// float speed = 0.035/balltime*3.6;
-// pc.printf("Speed of ball (3.5 cm diameter) : %f kph\n\r", speed);
-//
-// score_1++;
-//
-// if (score_1 > 10)
-// score_1 = 0;
-//
-// set_score(score_1);
-// balltime = 0;
-// }
-//
-// led1 = detector1; // toggle led based on value of the pin
-
- if (gc.goal) {
- score_1 = gc.get_score();
- balltime = gc.get_balltime();
- speed = gc.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);
- gc.goal = 0;
- }
+ print_info();
+ wait(0.5);
}
}