-
Notifications
You must be signed in to change notification settings - Fork 0
/
LegoRacer.ino
116 lines (93 loc) · 4.59 KB
/
LegoRacer.ino
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
// Include for boolean operations
#include <stdbool.h>
// Includes for the LED display
#include <Wire.h>
#include <Adafruit_GFX.h>
#include "Adafruit_LEDBackpack.h"
// Pins connected to each of the 2 sensors
const int sensorOnePin = 9;
const int sensorTwoPin = 10;
// LED display initialization
Adafruit_7segment matrix = Adafruit_7segment();
// Indicates if you want the dots to be displayed on the LED board (ex. 11:11). Initialized to true in setup().
bool drawDots;
// Each digit on LED display. Skips 2 because digit2 represents the colon in the time.
int dig0, dig1, dig3, dig4;
// State of timer. Can be 1 (timer running), or 2 (Timer stopped). State determined by sensor/button input in loop(). Ommitted state 0
// after it turns on initially (display 00:00) but can be re-included with a reset button as necessary.
int state;
void setup() {
// Sensor 1 initializing
pinMode(sensorOnePin, INPUT); // INPUT
// Sensor 2 initializing
pinMode(sensorTwoPin, INPUT);
// Enable dots in time (ex: 11:11)
drawDots = true;
// I2C communication port for LED display
matrix.begin(0x70);
// Initializing each digit on LED display to 0. Skips 2 because digit2 represents the colon in the time.
dig0 = 0, dig1=0, dig3=0, dig4 = 0;
// Initialize state of timer. Can be 0 (00:00), 1 (timer running), or 2 (Timer stopped).
state = 0;
// Serial monitor for debugging.
Serial.begin(9600);
}
void loop() {
// Change state to 1 (running) if first sensor is triggered.
if(digitalRead(sensorOnePin) == HIGH){
state = 1;
}
if(state==0){ // If the state is 0, display 00:00. Again, can connect a reset button to enter this state. As of now,
// it displays 00:00 only when the Arduino intitially turns on.
resetTimer();
} else if(state==1){ // if the state is 1, run timer.
drawTime(); // The state may become 2 and display end time only in this function.
}else{
displayEndTime(); // If the state is 2, display most recent value stored in dig0, dig1, dig3, dig4. Digits set in drawTime() function.
}
}
// Stopwatch function. If the first sensor is triggered, the program will enter this function.
void drawTime(){
for (uint16_t counter = 0; counter < 9999; counter ++) { // uint16_t is an unsigned 16 bit integer data type
// Storing the most current values of each digit on the LED display. These values are what the stopwatch will "freeze" to when stopped.
// The counter represents the time displayed on the stop watch. The code below extracts each digit from the overal number. For
// example, if the counter is 1456, dig0 will hold 1, dig1 will hold 4, dig3 holds 5, and dig4 holds 6.
dig0=counter/1000;
dig1 = (counter/100) % 10;
dig3= (counter/10) % 10;
dig4=counter % 10;
// Writing each digits to the LED display.
matrix.writeDigitNum(0, (counter / 1000), drawDots);
matrix.writeDigitNum(1, (counter / 100) % 10, drawDots);
matrix.drawColon(drawDots);
matrix.writeDigitNum(3, (counter / 10) % 10, drawDots);
matrix.writeDigitNum(4, counter % 10, drawDots);
matrix.writeDisplay();
delay(8); // This statement is needed or else the timer goes waaay too fast. THIS IS FOR THE FOR-LOOP NOT THE HARDWARE!
// I compared the speed of the program to a real stopwatch, and a delay of 8ms makes the loop closest to 1 loop = 1s.
// If the second sensor is triggered while the stopwatch is runing, increase the state to 2 and break out of the loop.
// When the state is 2, it displays the digits stored in dig0 - dig4 (Freezes the stopwatch). Calls function displayEndTime()
// in the main loop when state is 2.
if(digitalRead(sensorTwoPin) == HIGH) {
state = 2;
break;
}
}
}
// When the state is equal to 2 (ie the sensor was triggered while the stopwatch was running), display the digits stored in dig0dig1:dig3dig4 (freeze the timer).
void displayEndTime(){
matrix.writeDigitNum(0, dig0, drawDots);
matrix.writeDigitNum(1, dig1, drawDots);
matrix.drawColon(drawDots);
matrix.writeDigitNum(3, dig3, drawDots);
matrix.writeDigitNum(4, dig4, drawDots);
matrix.writeDisplay();
}
// When the state is equal to 0, display 00:00. This is the default state and will also display 00:00 after the reset button is triggered,
void resetTimer(){
matrix.writeDigitNum(0, 0, drawDots);
matrix.writeDigitNum(1, 0, drawDots);
matrix.writeDigitNum(3, 0, drawDots);
matrix.writeDigitNum(4, 0, drawDots);
matrix.writeDisplay();
}