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
main.cpp@2:bd0c735e81d6, 2018-05-18 (annotated)
- Committer:
- peps
- Date:
- Fri May 18 23:15:11 2018 +0000
- Revision:
- 2:bd0c735e81d6
- Parent:
- 1:26bcd89c18e5
- Child:
- 3:fd1353986910
Versione iniziale
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Ghiri | 0:1a92e4a37697 | 1 | //############################################################## |
Ghiri | 0:1a92e4a37697 | 2 | //## |
Ghiri | 0:1a92e4a37697 | 3 | //## Event: RoboVal - Robot Race |
Ghiri | 0:1a92e4a37697 | 4 | //## |
Ghiri | 0:1a92e4a37697 | 5 | //## Chronometer double use |
Ghiri | 0:1a92e4a37697 | 6 | //## |
Ghiri | 0:1a92e4a37697 | 7 | //## Version 1.99A |
Ghiri | 0:1a92e4a37697 | 8 | //## |
Ghiri | 0:1a92e4a37697 | 9 | //## Hardware platform used: ST NUCLEO-F401RE |
Ghiri | 0:1a92e4a37697 | 10 | //## |
Ghiri | 0:1a92e4a37697 | 11 | //## Software IDE used: mbed online version |
Ghiri | 0:1a92e4a37697 | 12 | //## |
Ghiri | 0:1a92e4a37697 | 13 | //## Organizzation: Verona FabLab |
Ghiri | 0:1a92e4a37697 | 14 | //## |
Ghiri | 0:1a92e4a37697 | 15 | //## Date creation: 2018.01.01 |
Ghiri | 0:1a92e4a37697 | 16 | //## |
Ghiri | 0:1a92e4a37697 | 17 | //## Software developpers: FdF,GV, AG |
Ghiri | 0:1a92e4a37697 | 18 | //## |
Ghiri | 0:1a92e4a37697 | 19 | //## Base on original version of by FdF |
Ghiri | 0:1a92e4a37697 | 20 | //## |
Ghiri | 0:1a92e4a37697 | 21 | //############################################################## |
Ghiri | 0:1a92e4a37697 | 22 | |
Ghiri | 0:1a92e4a37697 | 23 | #include <stdlib.h> |
Ghiri | 0:1a92e4a37697 | 24 | #include "mbed.h" |
Ghiri | 0:1a92e4a37697 | 25 | #include "TextLCD.h" |
peps | 2:bd0c735e81d6 | 26 | #include "keypad.h" |
peps | 2:bd0c735e81d6 | 27 | |
Ghiri | 0:1a92e4a37697 | 28 | // Default Number Lap |
Ghiri | 0:1a92e4a37697 | 29 | #define NUM_LAP 3 |
Ghiri | 0:1a92e4a37697 | 30 | |
Ghiri | 0:1a92e4a37697 | 31 | // Reference for Low/Min Voltage battery |
Ghiri | 0:1a92e4a37697 | 32 | #define VBAT_MIN 7.2 |
peps | 2:bd0c735e81d6 | 33 | |
peps | 2:bd0c735e81d6 | 34 | |
peps | 2:bd0c735e81d6 | 35 | // Modalità di funzionamento (in attesa della tastiera) |
peps | 2:bd0c735e81d6 | 36 | enum modes { |
peps | 2:bd0c735e81d6 | 37 | LABYRINTH, |
peps | 2:bd0c735e81d6 | 38 | SPEED |
peps | 2:bd0c735e81d6 | 39 | } mode = SPEED; |
peps | 2:bd0c735e81d6 | 40 | |
peps | 2:bd0c735e81d6 | 41 | typedef struct time_screen { |
peps | 2:bd0c735e81d6 | 42 | int cents; |
peps | 2:bd0c735e81d6 | 43 | int seconds; |
peps | 2:bd0c735e81d6 | 44 | int minutes; |
peps | 2:bd0c735e81d6 | 45 | } measured_time; |
peps | 2:bd0c735e81d6 | 46 | |
peps | 2:bd0c735e81d6 | 47 | |
Ghiri | 1:26bcd89c18e5 | 48 | // Defines the pins connected to the rows |
peps | 2:bd0c735e81d6 | 49 | Keypad keypad(PA_0 , PA_1 , PA_4 , PB_0,PA_13 , PA_14 , PC_2 , PC_3 ); |
Ghiri | 1:26bcd89c18e5 | 50 | |
Ghiri | 1:26bcd89c18e5 | 51 | // Configures the serial port |
Ghiri | 1:26bcd89c18e5 | 52 | Serial pc( USBTX , USBRX ); |
Ghiri | 1:26bcd89c18e5 | 53 | |
Ghiri | 0:1a92e4a37697 | 54 | // read Voltage battery |
Ghiri | 0:1a92e4a37697 | 55 | DigitalOut heartbeat(LED1); |
Ghiri | 0:1a92e4a37697 | 56 | |
Ghiri | 0:1a92e4a37697 | 57 | // read Voltage battery, analog pin used PC_3 (Pin 37 of Morpho layout) |
Ghiri | 1:26bcd89c18e5 | 58 | //AnalogIn vbat(PC_3); |
Ghiri | 0:1a92e4a37697 | 59 | |
Ghiri | 0:1a92e4a37697 | 60 | // User button pressure |
Ghiri | 0:1a92e4a37697 | 61 | InterruptIn user_button(USER_BUTTON); |
Ghiri | 0:1a92e4a37697 | 62 | |
Ghiri | 0:1a92e4a37697 | 63 | // Sensor connected to digital input D9, alias PC7 on Morpho layout |
Ghiri | 1:26bcd89c18e5 | 64 | // Now p29 and p31 |
peps | 2:bd0c735e81d6 | 65 | InterruptIn gateStart(PA_15); |
peps | 2:bd0c735e81d6 | 66 | InterruptIn gateEnd(PB_7); |
Ghiri | 0:1a92e4a37697 | 67 | |
Ghiri | 0:1a92e4a37697 | 68 | // LCD Display (RS, E, D4, D5, D6, D7); |
Ghiri | 0:1a92e4a37697 | 69 | TextLCD lcd(D2,D3,D4,D5,D6,D7); |
peps | 2:bd0c735e81d6 | 70 | |
peps | 2:bd0c735e81d6 | 71 | Timer t; |
Ghiri | 0:1a92e4a37697 | 72 | |
peps | 2:bd0c735e81d6 | 73 | int lap = 0; |
peps | 2:bd0c735e81d6 | 74 | int last_read = 0; |
peps | 2:bd0c735e81d6 | 75 | int lap_time = 0; |
peps | 2:bd0c735e81d6 | 76 | |
peps | 2:bd0c735e81d6 | 77 | void (*loopMethod)(void); |
peps | 2:bd0c735e81d6 | 78 | |
peps | 2:bd0c735e81d6 | 79 | |
peps | 2:bd0c735e81d6 | 80 | //Conversione da millisecondi a mm:ss:cc |
peps | 2:bd0c735e81d6 | 81 | measured_time human_read(int ms){ |
peps | 2:bd0c735e81d6 | 82 | measured_time read; |
peps | 2:bd0c735e81d6 | 83 | div_t qr = div(ms,1000); |
peps | 2:bd0c735e81d6 | 84 | |
peps | 2:bd0c735e81d6 | 85 | read.cents = qr.rem % 100; |
peps | 2:bd0c735e81d6 | 86 | |
peps | 2:bd0c735e81d6 | 87 | qr = div(qr.quot,60); |
peps | 2:bd0c735e81d6 | 88 | read.seconds = qr.rem; |
peps | 2:bd0c735e81d6 | 89 | |
peps | 2:bd0c735e81d6 | 90 | qr = div(qr.quot,60); |
peps | 2:bd0c735e81d6 | 91 | read.minutes = qr.rem; |
peps | 2:bd0c735e81d6 | 92 | |
peps | 2:bd0c735e81d6 | 93 | return read; |
peps | 2:bd0c735e81d6 | 94 | } |
peps | 2:bd0c735e81d6 | 95 | |
peps | 2:bd0c735e81d6 | 96 | void measure_time(){ |
peps | 2:bd0c735e81d6 | 97 | int read = t.read_ms(); |
peps | 2:bd0c735e81d6 | 98 | |
peps | 2:bd0c735e81d6 | 99 | if(lap == 0){ |
peps | 2:bd0c735e81d6 | 100 | t.start(); |
peps | 2:bd0c735e81d6 | 101 | lap++; |
peps | 2:bd0c735e81d6 | 102 | }else{ |
peps | 2:bd0c735e81d6 | 103 | //Dabouncing per evitare problemi |
peps | 2:bd0c735e81d6 | 104 | if(read - last_read > 1000){ |
peps | 2:bd0c735e81d6 | 105 | |
peps | 2:bd0c735e81d6 | 106 | lap_time = read - last_read; |
peps | 2:bd0c735e81d6 | 107 | |
peps | 2:bd0c735e81d6 | 108 | if(lap >= NUM_LAP){ |
peps | 2:bd0c735e81d6 | 109 | t.stop(); |
peps | 2:bd0c735e81d6 | 110 | lap++; |
peps | 2:bd0c735e81d6 | 111 | }else{ |
peps | 2:bd0c735e81d6 | 112 | lap++; |
peps | 2:bd0c735e81d6 | 113 | } |
peps | 2:bd0c735e81d6 | 114 | |
peps | 2:bd0c735e81d6 | 115 | last_read = read; |
peps | 2:bd0c735e81d6 | 116 | } |
peps | 2:bd0c735e81d6 | 117 | } |
peps | 2:bd0c735e81d6 | 118 | |
peps | 2:bd0c735e81d6 | 119 | } |
peps | 2:bd0c735e81d6 | 120 | |
peps | 2:bd0c735e81d6 | 121 | void reset_measure(){ |
peps | 2:bd0c735e81d6 | 122 | t.stop(); |
peps | 2:bd0c735e81d6 | 123 | t.reset(); |
peps | 2:bd0c735e81d6 | 124 | lap = 0; |
peps | 2:bd0c735e81d6 | 125 | last_read = 0; |
peps | 2:bd0c735e81d6 | 126 | lcd.cls(); |
peps | 2:bd0c735e81d6 | 127 | } |
peps | 2:bd0c735e81d6 | 128 | |
Ghiri | 0:1a92e4a37697 | 129 | |
peps | 2:bd0c735e81d6 | 130 | void speedLoop() { |
peps | 2:bd0c735e81d6 | 131 | int read = t.read_ms(); |
peps | 2:bd0c735e81d6 | 132 | |
peps | 2:bd0c735e81d6 | 133 | measured_time time = human_read(read); |
peps | 2:bd0c735e81d6 | 134 | |
peps | 2:bd0c735e81d6 | 135 | lcd.locate(0,0); |
peps | 2:bd0c735e81d6 | 136 | lcd.printf("Totale %02d:%02d:%02d",time.minutes,time.seconds,time.cents); |
peps | 2:bd0c735e81d6 | 137 | |
peps | 2:bd0c735e81d6 | 138 | //Gestione dei parziali |
peps | 2:bd0c735e81d6 | 139 | if(lap > 1){ |
peps | 2:bd0c735e81d6 | 140 | time = human_read(lap_time); |
peps | 2:bd0c735e81d6 | 141 | lcd.locate(0,1); |
peps | 2:bd0c735e81d6 | 142 | lcd.printf("Giro %d %02d:%02d:%02d",lap - 1,time.minutes,time.seconds,time.cents); |
peps | 2:bd0c735e81d6 | 143 | } |
peps | 2:bd0c735e81d6 | 144 | } |
peps | 2:bd0c735e81d6 | 145 | |
peps | 2:bd0c735e81d6 | 146 | void start_time() { |
peps | 2:bd0c735e81d6 | 147 | lap = 0; |
peps | 2:bd0c735e81d6 | 148 | t.start(); |
peps | 2:bd0c735e81d6 | 149 | } |
peps | 2:bd0c735e81d6 | 150 | |
peps | 2:bd0c735e81d6 | 151 | void stop_time() { |
peps | 2:bd0c735e81d6 | 152 | lap = 1; |
peps | 2:bd0c735e81d6 | 153 | t.stop(); |
peps | 2:bd0c735e81d6 | 154 | int read = t.read_ms(); |
peps | 2:bd0c735e81d6 | 155 | |
peps | 2:bd0c735e81d6 | 156 | measured_time time = human_read(read); |
peps | 2:bd0c735e81d6 | 157 | |
peps | 2:bd0c735e81d6 | 158 | lcd.locate(0,1); |
peps | 2:bd0c735e81d6 | 159 | lcd.printf(" "); |
peps | 2:bd0c735e81d6 | 160 | lcd.locate(0,1); |
peps | 2:bd0c735e81d6 | 161 | lcd.printf("Totale %02d:%02d:%02d",time.minutes,time.seconds,time.cents); |
peps | 2:bd0c735e81d6 | 162 | } |
peps | 2:bd0c735e81d6 | 163 | |
peps | 2:bd0c735e81d6 | 164 | void labyrinthLoop() { |
peps | 2:bd0c735e81d6 | 165 | if (!lap) { |
peps | 2:bd0c735e81d6 | 166 | int read = t.read_ms(); |
peps | 2:bd0c735e81d6 | 167 | |
peps | 2:bd0c735e81d6 | 168 | measured_time time = human_read(read); |
peps | 2:bd0c735e81d6 | 169 | |
peps | 2:bd0c735e81d6 | 170 | lcd.locate(0,1); |
peps | 2:bd0c735e81d6 | 171 | lcd.printf("Elapsed %02d:%02d:%02d",time.minutes,time.seconds,time.cents); |
peps | 2:bd0c735e81d6 | 172 | } |
peps | 2:bd0c735e81d6 | 173 | } |
peps | 2:bd0c735e81d6 | 174 | |
peps | 2:bd0c735e81d6 | 175 | void configMode() { |
peps | 2:bd0c735e81d6 | 176 | switch(mode) { |
peps | 2:bd0c735e81d6 | 177 | case LABYRINTH: |
peps | 2:bd0c735e81d6 | 178 | gateStart.rise(&start_time); |
peps | 2:bd0c735e81d6 | 179 | gateEnd.rise(&stop_time); |
peps | 2:bd0c735e81d6 | 180 | loopMethod = &labyrinthLoop; |
peps | 2:bd0c735e81d6 | 181 | break; |
peps | 2:bd0c735e81d6 | 182 | |
peps | 2:bd0c735e81d6 | 183 | case SPEED: |
peps | 2:bd0c735e81d6 | 184 | default: |
peps | 2:bd0c735e81d6 | 185 | gateStart.rise(&measure_time); |
peps | 2:bd0c735e81d6 | 186 | loopMethod = &speedLoop; |
peps | 2:bd0c735e81d6 | 187 | break; |
peps | 2:bd0c735e81d6 | 188 | } |
peps | 2:bd0c735e81d6 | 189 | } |
peps | 2:bd0c735e81d6 | 190 | |
Ghiri | 0:1a92e4a37697 | 191 | //------------------------------------------------------------ |
Ghiri | 0:1a92e4a37697 | 192 | // |
Ghiri | 0:1a92e4a37697 | 193 | // Main body |
Ghiri | 0:1a92e4a37697 | 194 | // |
Ghiri | 0:1a92e4a37697 | 195 | //------------------------------------------------------------ |
Ghiri | 0:1a92e4a37697 | 196 | int main() { |
peps | 2:bd0c735e81d6 | 197 | char key; |
peps | 2:bd0c735e81d6 | 198 | user_button.fall(&reset_measure); |
peps | 2:bd0c735e81d6 | 199 | |
peps | 2:bd0c735e81d6 | 200 | gateStart.mode(PullDown); |
peps | 2:bd0c735e81d6 | 201 | gateEnd.mode(PullDown); |
peps | 2:bd0c735e81d6 | 202 | |
peps | 2:bd0c735e81d6 | 203 | configMode(); |
peps | 2:bd0c735e81d6 | 204 | |
peps | 2:bd0c735e81d6 | 205 | while(true) { |
Ghiri | 0:1a92e4a37697 | 206 | heartbeat = !heartbeat; |
peps | 2:bd0c735e81d6 | 207 | loopMethod(); |
peps | 2:bd0c735e81d6 | 208 | wait(0.1); |
peps | 2:bd0c735e81d6 | 209 | key = keypad.getKey(); |
peps | 2:bd0c735e81d6 | 210 | if (key != KEY_RELEASED) { |
peps | 2:bd0c735e81d6 | 211 | if(key == 'A') { |
peps | 2:bd0c735e81d6 | 212 | lcd.locate(0,0); |
peps | 2:bd0c735e81d6 | 213 | lcd.printf("Mode: LABYRINTH "); |
peps | 2:bd0c735e81d6 | 214 | mode = LABYRINTH; |
peps | 2:bd0c735e81d6 | 215 | configMode(); |
peps | 2:bd0c735e81d6 | 216 | } else if(key == 'B') { |
peps | 2:bd0c735e81d6 | 217 | lcd.locate(0,0); |
peps | 2:bd0c735e81d6 | 218 | lcd.printf("Mode: SPEED "); |
peps | 2:bd0c735e81d6 | 219 | mode = SPEED; |
peps | 2:bd0c735e81d6 | 220 | configMode(); |
peps | 2:bd0c735e81d6 | 221 | } else if(key == '*') { |
peps | 2:bd0c735e81d6 | 222 | reset_measure(); |
peps | 2:bd0c735e81d6 | 223 | } |
Ghiri | 0:1a92e4a37697 | 224 | } |
Ghiri | 0:1a92e4a37697 | 225 | } |
Ghiri | 0:1a92e4a37697 | 226 | } |
Ghiri | 0:1a92e4a37697 | 227 |