Scalextric Lap Timer using ultrasonic sensor
Dependencies: HCSR04 mbed TextLCD
main.cpp
- Committer:
- MrBedfordVan
- Date:
- 2016-10-11
- Revision:
- 2:b47f8dbf374c
- Parent:
- 1:034f79c00c98
- Child:
- 3:389e402bd42f
File content as of revision 2:b47f8dbf374c:
// 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" // Ultrasonic sensor #include "TextLCD.h" // LCD1602 // serial PC for debug Serial pc(SERIAL_TX, SERIAL_RX); // 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; // Subroutine to display text on LCD - called from below // call as follows: // textLCD(text, line) // where text = up to 16 characters of text // line = line 0 or 1 on the LCD; where 0 is the top line and 1 is the bottom line 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]); } } /////////////////////////////////////////////////////////////////////////////// // Main Program starts here /////////////////////////////////////////////////////////////////////////////// int main() { //// 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 //// Careful with anything after this though // Variables used within the program - set to their initial values int dist = 0; float lap_time = 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); // Intro Screen pc.printf("ARM Sheffield Scalextric Challenge \n"); // Display to Serial Interface textLCD("ARM Scalextric ", 0); // Display on First Line of LCD // Main loop - runs foerever 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 if (lap_count == 0) { // First Lap - start lap timer lap_timer.reset(); lap_timer.start(); 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 } lap_count++; // Increment lap counter by 1 } // end of something spotted } // end of while loop } // end of main program