forked from acspike/SlotCar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SlotCar.cpp
80 lines (67 loc) · 1.41 KB
/
SlotCar.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/*
SlotCar.cpp - Library for tracking slot car lap statistics.
Created by Aaron C Spike, June 10, 2012.
Released into the public domain.
*/
#include "WProgram.h"
#include "SlotCar.h"
SlotCar::SlotCar(int pin, int threshold)
{
_pin = pin;
_threshold = threshold;
_over_sensor = false;
_read_level = 0;
_lap_read_time = 0;
_read_time = 0;
_lap_count = 0;
_total_time = 0;
_lap_time = 0;
_best_time = 4294967295;
}
void SlotCar::start()
{
_lap_read_time = millis();
}
void SlotCar::read()
{
_read_time = millis();
_read_level = analogRead(_pin);
if (_read_level > _threshold) {
if (!_over_sensor) {
_over_sensor = true;
_lap_count++;
_lap_time = _read_time - _lap_read_time;
_lap_read_time = _read_time;
_total_time += _lap_time;
_best_time = min(_best_time, _lap_time);
}
} else {
_over_sensor = false;
}
}
int SlotCar::readLevel()
{
return _read_level;
}
unsigned int SlotCar::lapCount()
{
return _lap_count;
}
unsigned long SlotCar::totalTime()
{
return _total_time;
}
float SlotCar::lapTime()
{
return _lap_time / 1000.0;
}
float SlotCar::bestTime()
{
if (_lap_count == 0) return 0.0;
return _best_time / 1000.0;
}
float SlotCar::avgTime()
{
if (_lap_count == 0) return 0.0;
return (_total_time / float(_lap_count)) / 1000.0;
}