
Scalextric Lap Timer using ultrasonic sensor
Dependencies: HCSR04 mbed TextLCD
Revision 1:034f79c00c98, committed 2016-09-28
- Comitter:
- MrBedfordVan
- Date:
- Wed Sep 28 16:05:35 2016 +0000
- Parent:
- 0:b88af2ed370b
- Child:
- 2:b47f8dbf374c
- Commit message:
- Initial Version of simple Scalextric Lap Timer for Ada Lovelace day
Changed in this revision
TextLCD_4Ada.lib | Show annotated file Show diff for this revision Revisions of this file |
main.cpp | Show annotated file Show diff for this revision Revisions of this file |
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/TextLCD_4Ada.lib Wed Sep 28 16:05:35 2016 +0000 @@ -0,0 +1,1 @@ +http://developer.mbed.org/users/simon/code/TextLCD/#308d188a2d3a
--- a/main.cpp Tue Dec 01 13:38:46 2015 +0000 +++ b/main.cpp Wed Sep 28 16:05:35 2016 +0000 @@ -1,66 +1,89 @@ +// Scalextric Timer for Ada Lovelace Day +// +// Uses Nucleo board F072 with LCD Keypad shield +// Connect D3 to Trig & D2 to Echo +// Connect VCC to 5V (pin CN7 18) +// Connect GND to 0V (pin CN7 19) + #include "mbed.h" -#include "hcsr04.h" +#include "hcsr04.h" // Ultrasonic sensor +#include "TextLCD.h" // LCD1602 +// serial PC for debug Serial pc(SERIAL_TX, SERIAL_RX); -HCSR04 usensor(D2,D4); -DigitalOut myled1(LED1); -Timer t; -Timer wait_t; +// Ultrasonic sensor +HCSR04 usensor(D3,D2); // Trig, Echo + +// LCD Shield +TextLCD lcd(D8, D9, D4, D5, D6, D7); // used to drive display +AnalogIn keys(A0); // used to sample buttons + +Timer lap_timer; +Timer wait_timer; + +// display text on LCD +void textLCD(char *text, int line) { + char tmpBuf[16]; + for (int i = 0; i < 16; i++) tmpBuf[i] = 0x20; + for (int i = 0; i < strlen(text); i++) { + if (i < 16) tmpBuf[i] = text[i]; + lcd.locate(i, line); + lcd.putc(tmpBuf[i]); + } +} + int main() { + // Variables used int dist = 0; float current_time = 0; - float fastest_time = 99999; + int lap_count = 0; float wait_time = 2.0; - int lap_count = 0; - int fastest_lap = 0; pc.baud(9600); - myled1 = 1; + char lap_string[16]; + char laptime_string[16]; - float min_laptime = 1.0; // s - int num_laps = 10; - int sample_time = 100; //ms + // Values to tune + float min_laptime = 1.0; // Minimum laptime - time to wait after seeing car before measuring again in seconds + int sample_time = 100; // Sample time - time between sampling value from sensor in milliseconds + int trigger_distance = 4; // Trigger distance - distance below which the car registers as detected + // Intro Screen pc.printf("ARM Sheffield Scalextric Challenge \n"); - - while(lap_count < num_laps+1) { + textLCD("ARM Scalextric ", 0); + + // Main loop + while(1) { usensor.start(); wait_ms(sample_time); dist=usensor.get_dist_cm(); - //pc.printf("Distance %d\n\r", dist); + pc.printf("Distance %d\n\r", dist); // Debug to Serial port/USB if (lap_count > 0) { - wait_time = wait_t.read(); + wait_time = wait_timer.read(); } - if ((dist < 10) and (wait_time > min_laptime)) { // spotted car at least a minimum laptime later - myled1 = 1; - wait_t.reset(); - wait_t.start(); - if (lap_count == 0) { - t.reset(); - t.start(); - pc.printf("Timing... \r"); + if ((dist < trigger_distance) and (wait_time > min_laptime)) { // spotted car at least a minimum laptime later + wait_timer.reset(); + wait_timer.start(); + if (lap_count == 0) { // First Lap - start timer + lap_timer.reset(); + lap_timer.start(); + pc.printf("Timing... \r"); // Debug + textLCD("Timing..........", 1); // lap_count++; - } else { - //t.stop(); - current_time = t.read(); - t.reset(); - if (current_time < fastest_time) { - fastest_time = current_time; - fastest_lap = lap_count; - } - pc.printf("Lap : %d, Lap time : %f s , Fastest Lap Time : %f s \r", lap_count, current_time, fastest_time); + } else { // Completed a lap + current_time = lap_timer.read(); + lap_timer.reset(); + pc.printf("Lap : %d, Lap time : %f s \r", lap_count, current_time); + sprintf(lap_string, "Lap : %d ", (char*)lap_count); + sprintf(laptime_string, "Lap time : %f s, ", current_time); + textLCD(" ", 0); + textLCD(lap_string, 0); + textLCD(" ", 1); + textLCD(laptime_string, 1); lap_count++; } - } else { - myled1 = 0; - } + } } - pc.printf("\n GAME OVER \n"); - pc.printf("Fastest Lap Time : %f s on Lap : %d \n", fastest_time, fastest_lap); - - t.stop(); - wait_t.stop(); - }