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

Dependencies:   GoalCounter WS2812 PixelArray

main.cpp

Committer:
nxf46245
Date:
2019-01-10
Revision:
1:ffac56d434b3
Parent:
0:24f846b68c77
Child:
2:f04959a6fd61

File content as of revision 1:ffac56d434b3:

#include "mbed.h"
#include "WS2812.h"
#include "PixelArray.h"
#include "GoalCounter.h"

#define WS2812_BUF 10
#define NUM_COLORS 1
#define NUM_LEDS_PER_COLOR 2
#define COLOR 0x2f2020 

// Initialize instace of a pixel array for LED strip 1 ar 2
PixelArray px1(WS2812_BUF);
PixelArray px2(WS2812_BUF);

// 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);

// 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;
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 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()
{
    
    ws1.useII(WS2812::GLOBAL); // use global intensity scaling
    ws2.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) {
        print_info();
        wait(0.5);
    }
}