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

Dependencies:   GoalCounter WS2812 PixelArray

Files at this revision

API Documentation at this revision

Comitter:
nxf46245
Date:
Sat Jan 12 18:50:49 2019 +0000
Parent:
2:f04959a6fd61
Child:
4:5a11a84fac69
Commit message:
Updated GoalCounter class

Changed in this revision

GoalCounter.cpp Show annotated file Show diff for this revision Revisions of this file
GoalCounter.h Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/GoalCounter.cpp	Fri Jan 11 18:29:58 2019 +0000
+++ b/GoalCounter.cpp	Sat Jan 12 18:50:49 2019 +0000
@@ -1,52 +1,78 @@
 #include "GoalCounter.h"
 
-GoalCounter::GoalCounter(PinName pin) : _interrupt(pin) {
-    // Set callback functions to timers
-    _interrupt.fall(callback(this, &GoalCounter::tstart));
-    _interrupt.rise(callback(this, &GoalCounter::tstop));
-//    _balltimes = {0};
-    _score= 0;
+GoalCounter::GoalCounter(PinName pin1, PinName pin2) : _interrupt1(pin1) {
+    _interrupt1.fall(callback(this, &GoalCounter::tstart1));
+    _interrupt1.rise(callback(this, &GoalCounter::tstop1));
+    
+//    _interrupt2.fall(callback(this, &GoalCounter::tstart2));
+//    _interrupt2.rise(callback(this, &GoalCounter::tstop2));
+    
+    Timer t1;
+    Timer t2;
+    
+    _score1 = 0;
+    _score2 = 0;
+    
+    _time1 = 0;
+    _time2 = 0;
+    
+//    _balltimes1 = {0};
+//    _balltimes2 = {0};
+    
+    is_goal1 = 0;
+    is_goal2 = 0;
+    
 }
          
-void GoalCounter::tstart() {
-    t.start();
+void GoalCounter::tstart1() {
+    t1.start();
 }
 
-void GoalCounter::tstop() {
-    t.stop();
-    _time = t.read();
-    t.reset();
+void GoalCounter::tstop1() {
+    t1.stop();
+    _time1 = t1.read();
+    t1.reset();
     
-    if ( _time > 0 && _time < 2 && _score < 10) {
-       _balltimes[++_score] = _time;
-       is_goal = 1;
-//       wait(1);
-        }     
+    if ( _time1 > 0 && _time1 < 2 && _score1 < 10) {
+       _balltimes1[++_score1] = _time1;
+       is_goal1 = 1;
+    }     
 }
 
-void GoalCounter::goal() {
-    is_goal = 1;
+void GoalCounter::tstart2() {
+    t2.start();
+}
+
+void GoalCounter::tstop2() {
+    t2.stop();
+    _time2 = t2.read();
+    t2.reset();
+    
+    if ( _time2 > 0 && _time2 < 2 && _score2 < 10) {
+       _balltimes2[++_score2] = _time2;
+       is_goal2 = 1;
+    }     
 }
 
 uint8_t GoalCounter::get_score() {
-    return _score;
+    return _score1;
 }
 
 float GoalCounter::get_balltime(uint8_t score) {
     if (score <= 10 && score > 0)
-        return _balltimes[score];
+        return _balltimes1[score];
     else
         return -1;   
 }
 
 float GoalCounter::get_balltime() {
-    return _balltimes[_score];
+    return _balltimes1[_score1];
  
 }
 
 float GoalCounter::get_ballspeed(uint8_t score) {
     if (score <= 10 && score > 0) {
-        float speed = 0.034f/_balltimes[score]*3.6f;
+        float speed = 0.034f/_balltimes1[score]*3.6f;
         return speed;
     }
     else
@@ -54,15 +80,10 @@
 }
 
 float GoalCounter::get_ballspeed() {
-    float speed = 0.034f/_balltimes[_score]*3.6f;
+    float speed = 0.034f/_balltimes1[_score1]*3.6f;
     return speed;
 }
 
-//void GoalCounter:print_info() {
-//    pc.printf("Score : %d \n\r", _score);
-//    pc.printf("Time of ball pass : %f seconds\n\r", this.get_balltime());
-//    pc.printf("Speed of ball (34 mm diameter) : %f kph\n\r", this.get_ballspeed());
-//}
 
 
     
--- a/GoalCounter.h	Fri Jan 11 18:29:58 2019 +0000
+++ b/GoalCounter.h	Sat Jan 12 18:50:49 2019 +0000
@@ -2,24 +2,31 @@
 
 class GoalCounter {
 public:
-    GoalCounter(PinName pin);  
-    void tstart();
-    void tstop();
+    GoalCounter(PinName pin1, PinName pin2);  
+    void tstart1();
+    void tstop1();
+    void tstart2();
+    void tstop2();
     uint8_t get_score();
     float get_balltime(uint8_t score);
     float get_balltime();
     float get_ballspeed(uint8_t score);
     float get_ballspeed();
-    void goal();
-    char is_goal;
+    uint8_t is_goal1;
+    uint8_t is_goal2;
 
 
 private:
-    InterruptIn _interrupt;
-    volatile uint8_t _score;
-    Timer t;
-    float _balltimes[11];
-    float _time; // 
+    InterruptIn _interrupt1;
+//    InterruptIn _interrupt2;
+    volatile uint8_t _score1;
+    volatile uint8_t _score2;
+    Timer t1;
+    Timer t2;
+    float _balltimes1[11];
+    float _time1;
+    float _balltimes2[11];
+    float _time2; 
       
 };
 
--- a/main.cpp	Fri Jan 11 18:29:58 2019 +0000
+++ b/main.cpp	Sat Jan 12 18:50:49 2019 +0000
@@ -9,6 +9,12 @@
 #define COLOR 0x2f2020 
 #define DEBUG 1
 
+#define MBED_CONF_PLATFORM_FORCE_NON_COPYABLE_ERROR 0
+
+//Initialize instances of Timers
+//Timer t1;
+//Timer t2;
+
 // Initialize instace of a pixel array for LED strip 1 ar 2
 PixelArray px1(WS2812_BUF);
 PixelArray px2(WS2812_BUF);
@@ -17,9 +23,14 @@
 WS2812 ws1(D4, WS2812_BUF, 0, 5, 5, 0);
 WS2812 ws2(D5, WS2812_BUF, 0, 5, 5, 0);
 
+// Initialize interrupt classes
+//InterruptIn i1(D2);
+//InterruptIn i2(D3);
 // Initialize instances of GoalCounter classes for counting goals
-GoalCounter gc1(D2);
-GoalCounter gc2(D3);
+//GoalCounter gc1(D2, i1);
+//GoalCounter gc2(D3, i2);
+
+GoalCounter gc(D2, D3);
 
 // Initialize instances of Ticker classes for handling goal counters
 //Ticker bank1;
@@ -59,7 +70,7 @@
     *score = gc.get_score();
     *balltime = gc.get_balltime();
     *speed = gc.get_ballspeed();
-    gc.is_goal = 0;
+    gc.is_goal1 = 0;
 }
 
 int main()
@@ -74,17 +85,17 @@
 //    bank2.attach(&print_info, 0.5);
     
     while(1) {
-        if (gc1.is_goal) {
-            get_info(&score1, &balltime1, &speed1, gc1);
+        if (gc.is_goal1) {
+            get_info(&score1, &balltime1, &speed1, gc);
             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);
-        }
+//        else if (gc2.is_goal) {
+//            get_info(&score2, &balltime2, &speed2, gc2);
+//            set_score(score2, px2, ws2);
+//            if (DEBUG)
+//                print_score(score2, balltime2, speed2);
+//        }
     }
 }