kub-kar-timer/src/main.cpp

99 lines
2.6 KiB
C++
Raw Permalink Normal View History

2016-02-12 00:47:57 +00:00
/*
2016-02-13 20:30:31 +00:00
_ __ _ _ __ _____ ___
| |/ _ _| |__ | |/ /__ _ _ __|_ _|_ _|_ __ ___ ___ _ __
| ' | | | | '_ \| ' // _` | '__ | | | || '_ ` _ \ / _ | '__|
| . | |_| | |_) | . | (_| | | | | | || | | | | | __| |
|_|\_\__,_|_.__/|_|\_\__,_|_| |_| |___|_| |_| |_|\___|_|
2016-02-12 00:47:57 +00:00
*/
#include "Arduino.h"
2016-02-12 01:37:26 +00:00
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);
2016-02-12 01:44:25 +00:00
#define PIN_RESET 2
#define PIN_CAR1 3
2016-02-13 20:20:39 +00:00
#define PIN_LED LED_BUILTIN
2016-02-13 20:30:31 +00:00
#define DISPLAY_LINES 4
2016-02-12 00:47:57 +00:00
2016-02-12 02:12:45 +00:00
// assume all cars are on sequential digital pins
2016-02-12 02:02:19 +00:00
#define PIN_CAR_STARTING 3
2016-02-12 02:12:45 +00:00
// number of cars (4 max)
2016-02-13 20:30:31 +00:00
#define CAR_COUNT 4
2016-02-12 02:12:45 +00:00
2016-02-12 02:02:19 +00:00
// buffer for the LCD line
char buffer[40];
2016-02-12 00:47:57 +00:00
void setup()
{
2016-02-12 01:44:25 +00:00
pinMode(PIN_RESET, INPUT_PULLUP);
2016-02-14 14:57:31 +00:00
pinMode(PIN_LED, OUTPUT);
2016-02-12 01:44:25 +00:00
2016-02-12 02:02:19 +00:00
// initialize each car PIN
for(short i=0; i<CAR_COUNT; i++) {
2016-02-14 14:57:31 +00:00
pinMode(PIN_CAR_STARTING + i, INPUT_PULLUP);
2016-02-12 02:02:19 +00:00
}
2016-02-12 02:12:45 +00:00
// initialize LCD
2016-02-13 20:30:31 +00:00
lcd.begin(20, DISPLAY_LINES);
2016-02-12 01:37:26 +00:00
lcd.backlight();
2016-02-12 02:21:28 +00:00
lcd.setCursor(1,1);
lcd.print("Jeff's Inept");
lcd.setCursor(6,2);
lcd.print("Kub Kar Timer");
2016-02-12 00:47:57 +00:00
}
2016-02-12 02:03:42 +00:00
#define STATE_READY 1
#define STATE_RUNNING 2
2016-02-14 14:57:31 +00:00
unsigned long startTime; // start time in ms
2016-02-13 22:28:46 +00:00
unsigned short state = 0;
unsigned short place = 0;
2016-02-14 14:57:31 +00:00
bool finished[CAR_COUNT];
2016-02-12 02:03:42 +00:00
2016-02-12 00:47:57 +00:00
void loop()
{
2016-02-12 02:18:19 +00:00
2016-02-12 01:44:25 +00:00
if (digitalRead(PIN_RESET) == LOW) {
2016-02-12 02:03:42 +00:00
if (state != STATE_READY) {
2016-02-14 14:57:31 +00:00
digitalWrite(PIN_LED, HIGH);
2016-02-12 02:03:42 +00:00
state = STATE_READY;
lcd.clear();
lcd.setCursor(0,0);
2016-02-12 02:18:19 +00:00
lcd.print("Ready...");
2016-02-12 02:03:42 +00:00
}
2016-02-12 01:44:25 +00:00
} else {
2016-02-12 02:12:45 +00:00
if (state == STATE_READY) {
2016-02-13 20:20:39 +00:00
digitalWrite(PIN_LED, LOW);
2016-02-12 02:12:45 +00:00
state = STATE_RUNNING;
lcd.setCursor(0,0);
lcd.print("Running!");
2016-02-12 05:26:21 +00:00
startTime = millis();
2016-02-14 14:57:31 +00:00
for(int i=0; i<CAR_COUNT; i++) {
finished[i] = false;
}
place = 0;
2016-02-12 01:44:25 +00:00
}
2016-02-12 02:12:45 +00:00
if (state == STATE_RUNNING) {
2016-02-12 05:26:21 +00:00
unsigned long elapsedTime = millis() - startTime;
2016-02-12 02:12:45 +00:00
for(short i = 0; i<CAR_COUNT; i++) {
2016-02-14 14:57:31 +00:00
if (!finished[i] && digitalRead(PIN_CAR_STARTING + i) == LOW) {
place++;
finished[i] = true;
if (place <= DISPLAY_LINES) {
2016-02-14 15:00:52 +00:00
// NOTE: Some concern LCD writing takes enough cycles it'll potentially miss other car finishing
// events. Alternative is to draw only once all cars finish but then we need a timeout to handle
// missing or non-finishing cars. Who wants to wait for results?
2016-02-14 14:57:31 +00:00
lcd.setCursor(0,place-1);
sprintf(buffer, "%d%s: CAR%d @ %0.2fs", place, place==1?"st":(place==2?"nd":(place==3?"rd":"th")) , i+1, elapsedTime/1000.0);
lcd.print(buffer);
2016-02-12 02:12:45 +00:00
}
2016-02-12 02:02:19 +00:00
}
}
2016-02-12 01:44:25 +00:00
}
2016-02-12 01:37:26 +00:00
}
2016-02-12 00:47:57 +00:00
}