Scalextric Lap Timer using ultrasonic sensor
Dependencies: HCSR04 mbed TextLCD
Diff: main.cpp
- Revision:
- 3:389e402bd42f
- Parent:
- 2:b47f8dbf374c
--- a/main.cpp Tue Oct 11 09:52:30 2016 +0000 +++ b/main.cpp Mon Oct 09 17:24:44 2017 +0000 @@ -10,16 +10,15 @@ #include "TextLCD.h" // LCD1602 // serial PC for debug -Serial pc(SERIAL_TX, SERIAL_RX); +//Serial pc(SERIAL_TX, SERIAL_RX); // Ultrasonic sensor -HCSR04 usensor(D3,D2); // Trig, Echo +HCSR04 usensor(D3,D2); // Trig, Echo - locks sometimes // LCD Shield TextLCD lcd(D8, D9, D4, D5, D6, D7); // used to drive display -AnalogIn keys(A0); // used to sample buttons +//AnalogIn keys(A0); // used to sample buttons Timer lap_timer; -Timer wait_timer; // Subroutine to display text on LCD - called from below // call as follows: @@ -29,15 +28,48 @@ 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]; + char tmpBuf[16]; + for (int i = 0; i < 14; i++) { + if (i < strlen(text)) { + tmpBuf[i] = text[i]; + } + else + { + tmpBuf[i] = 0x20; + } lcd.locate(i, line); lcd.putc(tmpBuf[i]); } } +// Subroutine to display Lap data on LCD - called from below +// call as follows: +// DisplayLap(lap_count, dist, lap_time, Show_Time); + +void DisplayLap(int lcl_lap_count, int lcl_dist, float lcl_lap_time, bool Show_Time) { + + char lap_string[16]; + char lap_string2[16]; + //textLCD("1 ", 0); // Clear top line on LCD + //textLCD("2 ", 1); // Clear bottom line on LCD + //lcd.cls(); + + int lcl_lap_time_integerPart = (int)lcl_lap_time; + int lcl_lap_time_decimalPart = ((int)(lcl_lap_time*1000)%1000); + + sprintf(lap_string, "Lap : %d D : %d", (char*)lcl_lap_count, (char*)lcl_dist); // Format Lap count string +// pc.printf("%s \n\r", lap_string); + textLCD("1 ", 0); // Clear top line on LCD + textLCD(lap_string, 0); // Display distance on LCD + + if (Show_Time) { + sprintf(lap_string2, "Time : %d.%d s, ", (char*)lcl_lap_time_integerPart, (char*)lcl_lap_time_decimalPart); // Format Lap time string +// pc.printf("%s \n\r", lap_string); + textLCD("1 ", 1); // Clear bottom line on LCD + textLCD(lap_string2, 1); // Display Lap Time on LCD + } + } + /////////////////////////////////////////////////////////////////////////////// // Main Program starts here /////////////////////////////////////////////////////////////////////////////// @@ -48,58 +80,52 @@ //// Try changing these: // Variables 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 values from sensor in milliseconds - int trigger_distance = 2; // Trigger distance - distance below which the car registers as detected + int sample_time = 500; // Sample time - time between sampling values from sensor in milliseconds + int trigger_distance = 5; // Trigger distance - distance below which the car registers as detected //// Careful with anything after this though // Variables used within the program - set to their initial values int dist = 0; - float lap_time = 0; + float lap_time = 0.0; int lap_count = 0; - float wait_time = min_laptime + 1.0; // ensure larger than minimum lap time for first loop - - // Variables used to hold text to display on LCD for correct formatting - char lap_string[16]; - char laptime_string[16]; + // Initialise the serial interface and set its data rate - pc.baud(9600); + // pc.baud(9600); // Intro Screen - pc.printf("ARM Sheffield Scalextric Challenge \n"); // Display to Serial Interface + // pc.printf("ARM Sheffield Scalextric Challenge \n"); // Display to Serial Interface textLCD("ARM Scalextric ", 0); // Display on First Line of LCD + wait_ms(2000); // Wait 2s + textLCD(" ", 0); // Clear top line on LCD - // Main loop - runs foerever + // Main loop - runs forever while(1) { // Set up and read ultrasonic sensor after "sample_time" usensor.start(); wait_ms(sample_time); dist=usensor.get_dist_cm(); // read sesnsor distance measure - pc.printf("Distance %d\n\r", dist); // Debug - display distance to Serial Interface - if (lap_count > 0) { // If not the first lap - read value of wait timer - wait_time = wait_timer.read(); - } - if ((dist < trigger_distance) and (wait_time > min_laptime)) { // Spotted something closer than trigger_distance from the sensor and at least a minimum laptime later - wait_timer.reset(); // reset the wait timer - wait_timer.start(); // start the wait timer again + //pc.printf("Lap %d Distance %d wait_time %d \n\r", lap_count, dist, wait_time); // Debug - display distance to Serial Interface + if (dist < trigger_distance) { // Spotted something closer than trigger_distance from the sensor if (lap_count == 0) { // First Lap - start lap timer lap_timer.reset(); lap_timer.start(); - pc.printf("Timing... \r"); // Debug to Serial Interface + //pc.printf("Timing... \r"); // Debug to Serial Interface textLCD("Timing..........", 1); // Message to LCD } else { // Completed a lap lap_time = lap_timer.read(); // Get time for lap lap_timer.reset(); - pc.printf("Lap : %d, Lap time : %f s \r", lap_count, lap_time); // Debug to Serial Interface - sprintf(lap_string, "Lap : %d ", (char*)lap_count); // Format Lap count string - sprintf(laptime_string, "Lap time : %f s, ", lap_time); // Format Lap time string - textLCD(" ", 0); // Clear top line on LCD - textLCD(lap_string, 0); // Display Lap Count on LCD - textLCD(" ", 1); // Clear bottom line on LCD - textLCD(laptime_string, 1); // Display Lap Time on LCD + DisplayLap(lap_count, dist, lap_time, 1); } lap_count++; // Increment lap counter by 1 - } // end of something spotted - } // end of while loop + wait(min_laptime); // Wait for a time before starting to monitor sensor for next lap + } // end of something spotted + else // nothing spotted - output debug + { + DisplayLap(lap_count, dist, lap_time, 0); + } + } // end of main while loop + + } // end of main program