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
- Committer:
- peps
- Date:
- 2018-05-18
- Revision:
- 2:bd0c735e81d6
- Parent:
- 1:26bcd89c18e5
- Child:
- 3:fd1353986910
File content as of revision 2:bd0c735e81d6:
//############################################################## //## //## Event: RoboVal - Robot Race //## //## Chronometer double use //## //## Version 1.99A //## //## Hardware platform used: ST NUCLEO-F401RE //## //## Software IDE used: mbed online version //## //## Organizzation: Verona FabLab //## //## Date creation: 2018.01.01 //## //## Software developpers: FdF,GV, AG //## //## Base on original version of by FdF //## //############################################################## #include <stdlib.h> #include "mbed.h" #include "TextLCD.h" #include "keypad.h" // Default Number Lap #define NUM_LAP 3 // Reference for Low/Min Voltage battery #define VBAT_MIN 7.2 // Modalità di funzionamento (in attesa della tastiera) enum modes { LABYRINTH, SPEED } mode = SPEED; typedef struct time_screen { int cents; int seconds; int minutes; } 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 ); // Configures the serial port Serial pc( USBTX , USBRX ); // read Voltage battery DigitalOut heartbeat(LED1); // read Voltage battery, analog pin used PC_3 (Pin 37 of Morpho layout) //AnalogIn vbat(PC_3); // 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); // LCD Display (RS, E, D4, D5, D6, D7); TextLCD lcd(D2,D3,D4,D5,D6,D7); Timer t; int lap = 0; int last_read = 0; int lap_time = 0; void (*loopMethod)(void); //Conversione da millisecondi a mm:ss:cc measured_time human_read(int ms){ measured_time read; div_t qr = div(ms,1000); read.cents = qr.rem % 100; qr = div(qr.quot,60); read.seconds = qr.rem; qr = div(qr.quot,60); read.minutes = qr.rem; return read; } void measure_time(){ int read = t.read_ms(); if(lap == 0){ t.start(); lap++; }else{ //Dabouncing per evitare problemi if(read - last_read > 1000){ lap_time = read - last_read; 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(); } void speedLoop() { int read = t.read_ms(); measured_time time = human_read(read); lcd.locate(0,0); lcd.printf("Totale %02d:%02d:%02d",time.minutes,time.seconds,time.cents); //Gestione dei parziali if(lap > 1){ time = human_read(lap_time); lcd.locate(0,1); lcd.printf("Giro %d %02d:%02d:%02d",lap - 1,time.minutes,time.seconds,time.cents); } } void start_time() { lap = 0; t.start(); } void stop_time() { lap = 1; t.stop(); int read = t.read_ms(); measured_time time = human_read(read); lcd.locate(0,1); lcd.printf(" "); lcd.locate(0,1); lcd.printf("Totale %02d:%02d:%02d",time.minutes,time.seconds,time.cents); } void labyrinthLoop() { if (!lap) { int read = t.read_ms(); measured_time time = human_read(read); lcd.locate(0,1); lcd.printf("Elapsed %02d:%02d:%02d",time.minutes,time.seconds,time.cents); } } void configMode() { switch(mode) { case LABYRINTH: gateStart.rise(&start_time); gateEnd.rise(&stop_time); loopMethod = &labyrinthLoop; break; case SPEED: default: gateStart.rise(&measure_time); loopMethod = &speedLoop; break; } } //------------------------------------------------------------ // // Main body // //------------------------------------------------------------ int main() { char key; user_button.fall(&reset_measure); gateStart.mode(PullDown); gateEnd.mode(PullDown); configMode(); while(true) { 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(); } } } }