Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed keypadLib TextLCD
Diff: main.cpp
- Revision:
- 3:fd1353986910
- Parent:
- 2:bd0c735e81d6
- Child:
- 4:23472c2b246b
diff -r bd0c735e81d6 -r fd1353986910 main.cpp
--- a/main.cpp Fri May 18 23:15:11 2018 +0000
+++ b/main.cpp Sun May 20 23:19:17 2018 +0000
@@ -31,13 +31,15 @@
// Reference for Low/Min Voltage battery
#define VBAT_MIN 7.2
-
-// Modalità di funzionamento (in attesa della tastiera)
+// Different function modes
+// Switch among them using the USER button
+// (waiting for the keypad to work properly)
enum modes {
LABYRINTH,
SPEED
} mode = SPEED;
+// Human-readable time helper
typedef struct time_screen {
int cents;
int seconds;
@@ -45,13 +47,13 @@
} measured_time;
-// Defines the pins connected to the rows
-Keypad keypad(PA_0 , PA_1 , PA_4 , PB_0,PA_13 , PA_14 , PC_2 , PC_3 );
+// Keypad connection (4 columns, 4 rows)
+Keypad keypad(PA_0 , PA_1 , PA_4 , PB_0, PA_13 , PA_14 , PC_2 , PC_3 );
// Configures the serial port
Serial pc( USBTX , USBRX );
- // read Voltage battery
+// Heartbeat LED
DigitalOut heartbeat(LED1);
// read Voltage battery, analog pin used PC_3 (Pin 37 of Morpho layout)
@@ -60,23 +62,28 @@
// User button pressure
InterruptIn user_button(USER_BUTTON);
-// Sensor connected to digital input D9, alias PC7 on Morpho layout
-// Now p29 and p31
-InterruptIn gateStart(PA_15);
-InterruptIn gateEnd(PB_7);
+// Gates connected to digital input PA_15 and PB_7
+InterruptIn gateStart(PA_15), gateEnd(PB_7);
// LCD Display (RS, E, D4, D5, D6, D7);
TextLCD lcd(D2,D3,D4,D5,D6,D7);
+// Timer for the chrono function
Timer t;
-int lap = 0;
+// Number of laps
+int lap = -1;
+// Best lap
+int best_lap = 0;
+// Last time read
int last_read = 0;
+// Last lap time
int lap_time = 0;
-
+// Best lap time
+int best_time = -1;
+// Pointer to the loop function (depending on the selected mode)
void (*loopMethod)(void);
-
//Conversione da millisecondi a mm:ss:cc
measured_time human_read(int ms){
measured_time read;
@@ -93,37 +100,32 @@
return read;
}
-void measure_time(){
+// Invoked when startGate triggered.
+// Start the timer or read the timer and store lap and best time
+void measure_time() {
int read = t.read_ms();
-
- if(lap == 0){
+
+ if(lap == -1){
t.start();
lap++;
}else{
//Dabouncing per evitare problemi
if(read - last_read > 1000){
+ lap++;
lap_time = read - last_read;
+ if (best_time < 0 || lap_time < best_time) {
+ best_time = lap_time;
+ best_lap = lap;
+ }
- if(lap >= NUM_LAP){
+ if(lap >= NUM_LAP) {
t.stop();
- lap++;
- }else{
- lap++;
}
last_read = read;
}
- }
-
-}
-
-void reset_measure(){
- t.stop();
- t.reset();
- lap = 0;
- last_read = 0;
- lcd.cls();
+ }
}
@@ -136,10 +138,17 @@
lcd.printf("Totale %02d:%02d:%02d",time.minutes,time.seconds,time.cents);
//Gestione dei parziali
- if(lap > 1){
+ if(lap > 0) {
time = human_read(lap_time);
lcd.locate(0,1);
- lcd.printf("Giro %d %02d:%02d:%02d",lap - 1,time.minutes,time.seconds,time.cents);
+ lcd.printf("Giro %d %02d:%02d:%02d",lap,time.minutes,time.seconds,time.cents);
+ }
+ if (lap >= NUM_LAP) {
+ time = human_read(best_time);
+ lcd.locate(0,1);
+ wait(1);
+ lcd.printf("Best %d %02d:%02d:%02d",best_lap,time.minutes,time.seconds,time.cents);
+ wait(1);
}
}
@@ -155,14 +164,15 @@
measured_time time = human_read(read);
- lcd.locate(0,1);
- lcd.printf(" ");
+ lcd.cls();
+ lcd.locate(0,0);
+ lcd.printf("Ended.");
lcd.locate(0,1);
lcd.printf("Totale %02d:%02d:%02d",time.minutes,time.seconds,time.cents);
}
void labyrinthLoop() {
- if (!lap) {
+ if (lap == 0) {
int read = t.read_ms();
measured_time time = human_read(read);
@@ -176,18 +186,47 @@
switch(mode) {
case LABYRINTH:
gateStart.rise(&start_time);
+ gateEnd.enable_irq();
gateEnd.rise(&stop_time);
loopMethod = &labyrinthLoop;
+ lcd.cls();
+ lcd.locate(0,0);
+ lcd.printf("Mode: LABYRINTH ");
+ wait(1);
+ lcd.cls();
break;
case SPEED:
default:
gateStart.rise(&measure_time);
+ gateEnd.disable_irq();
loopMethod = &speedLoop;
+ lcd.cls();
+ lcd.locate(0,0);
+ lcd.printf("Mode: SPEED ");
+ wait(1);
+ lcd.cls();
break;
}
}
+void reset_measure(){
+ t.stop();
+ t.reset();
+ lap = -1;
+ last_read = 0;
+ best_lap = 0;
+ best_time = -1;
+ lcd.cls();
+}
+
+void switchMode() {
+ mode = mode == SPEED ? LABYRINTH : SPEED;
+ reset_measure();
+ configMode();
+
+}
+
//------------------------------------------------------------
//
// Main body
@@ -195,7 +234,7 @@
//------------------------------------------------------------
int main() {
char key;
- user_button.fall(&reset_measure);
+ user_button.fall(&switchMode);
gateStart.mode(PullDown);
gateEnd.mode(PullDown);
@@ -206,22 +245,20 @@
heartbeat = !heartbeat;
loopMethod();
wait(0.1);
+ /*
key = keypad.getKey();
if (key != KEY_RELEASED) {
if(key == 'A') {
- lcd.locate(0,0);
- lcd.printf("Mode: LABYRINTH ");
mode = LABYRINTH;
configMode();
} else if(key == 'B') {
- lcd.locate(0,0);
- lcd.printf("Mode: SPEED ");
mode = SPEED;
configMode();
} else if(key == '*') {
reset_measure();
}
}
+ */
}
}