Scalextric Lap Timer using ultrasonic sensor
Dependencies: HCSR04 mbed TextLCD
main.cpp@3:389e402bd42f, 2017-10-09 (annotated)
- Committer:
- MrBedfordVan
- Date:
- Mon Oct 09 17:24:44 2017 +0000
- Revision:
- 3:389e402bd42f
- Parent:
- 2:b47f8dbf374c
Added stability and display distance for debug as it runs
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
MrBedfordVan | 1:034f79c00c98 | 1 | // Scalextric Timer for Ada Lovelace Day |
MrBedfordVan | 1:034f79c00c98 | 2 | // |
MrBedfordVan | 1:034f79c00c98 | 3 | // Uses Nucleo board F072 with LCD Keypad shield |
MrBedfordVan | 1:034f79c00c98 | 4 | // Connect D3 to Trig & D2 to Echo |
MrBedfordVan | 1:034f79c00c98 | 5 | // Connect VCC to 5V (pin CN7 18) |
MrBedfordVan | 1:034f79c00c98 | 6 | // Connect GND to 0V (pin CN7 19) |
MrBedfordVan | 1:034f79c00c98 | 7 | |
MrBedfordVan | 0:b88af2ed370b | 8 | #include "mbed.h" |
MrBedfordVan | 1:034f79c00c98 | 9 | #include "hcsr04.h" // Ultrasonic sensor |
MrBedfordVan | 1:034f79c00c98 | 10 | #include "TextLCD.h" // LCD1602 |
MrBedfordVan | 0:b88af2ed370b | 11 | |
MrBedfordVan | 1:034f79c00c98 | 12 | // serial PC for debug |
MrBedfordVan | 3:389e402bd42f | 13 | //Serial pc(SERIAL_TX, SERIAL_RX); |
MrBedfordVan | 1:034f79c00c98 | 14 | // Ultrasonic sensor |
MrBedfordVan | 3:389e402bd42f | 15 | HCSR04 usensor(D3,D2); // Trig, Echo - locks sometimes |
MrBedfordVan | 1:034f79c00c98 | 16 | |
MrBedfordVan | 1:034f79c00c98 | 17 | // LCD Shield |
MrBedfordVan | 1:034f79c00c98 | 18 | TextLCD lcd(D8, D9, D4, D5, D6, D7); // used to drive display |
MrBedfordVan | 3:389e402bd42f | 19 | //AnalogIn keys(A0); // used to sample buttons |
MrBedfordVan | 1:034f79c00c98 | 20 | |
MrBedfordVan | 1:034f79c00c98 | 21 | Timer lap_timer; |
MrBedfordVan | 1:034f79c00c98 | 22 | |
MrBedfordVan | 2:b47f8dbf374c | 23 | // Subroutine to display text on LCD - called from below |
MrBedfordVan | 2:b47f8dbf374c | 24 | // call as follows: |
MrBedfordVan | 2:b47f8dbf374c | 25 | // textLCD(text, line) |
MrBedfordVan | 2:b47f8dbf374c | 26 | // where text = up to 16 characters of text |
MrBedfordVan | 2:b47f8dbf374c | 27 | // line = line 0 or 1 on the LCD; where 0 is the top line and 1 is the bottom line |
MrBedfordVan | 2:b47f8dbf374c | 28 | |
MrBedfordVan | 1:034f79c00c98 | 29 | void textLCD(char *text, int line) { |
MrBedfordVan | 2:b47f8dbf374c | 30 | |
MrBedfordVan | 3:389e402bd42f | 31 | char tmpBuf[16]; |
MrBedfordVan | 3:389e402bd42f | 32 | for (int i = 0; i < 14; i++) { |
MrBedfordVan | 3:389e402bd42f | 33 | if (i < strlen(text)) { |
MrBedfordVan | 3:389e402bd42f | 34 | tmpBuf[i] = text[i]; |
MrBedfordVan | 3:389e402bd42f | 35 | } |
MrBedfordVan | 3:389e402bd42f | 36 | else |
MrBedfordVan | 3:389e402bd42f | 37 | { |
MrBedfordVan | 3:389e402bd42f | 38 | tmpBuf[i] = 0x20; |
MrBedfordVan | 3:389e402bd42f | 39 | } |
MrBedfordVan | 1:034f79c00c98 | 40 | lcd.locate(i, line); |
MrBedfordVan | 1:034f79c00c98 | 41 | lcd.putc(tmpBuf[i]); |
MrBedfordVan | 1:034f79c00c98 | 42 | } |
MrBedfordVan | 1:034f79c00c98 | 43 | } |
MrBedfordVan | 1:034f79c00c98 | 44 | |
MrBedfordVan | 3:389e402bd42f | 45 | // Subroutine to display Lap data on LCD - called from below |
MrBedfordVan | 3:389e402bd42f | 46 | // call as follows: |
MrBedfordVan | 3:389e402bd42f | 47 | // DisplayLap(lap_count, dist, lap_time, Show_Time); |
MrBedfordVan | 3:389e402bd42f | 48 | |
MrBedfordVan | 3:389e402bd42f | 49 | void DisplayLap(int lcl_lap_count, int lcl_dist, float lcl_lap_time, bool Show_Time) { |
MrBedfordVan | 3:389e402bd42f | 50 | |
MrBedfordVan | 3:389e402bd42f | 51 | char lap_string[16]; |
MrBedfordVan | 3:389e402bd42f | 52 | char lap_string2[16]; |
MrBedfordVan | 3:389e402bd42f | 53 | //textLCD("1 ", 0); // Clear top line on LCD |
MrBedfordVan | 3:389e402bd42f | 54 | //textLCD("2 ", 1); // Clear bottom line on LCD |
MrBedfordVan | 3:389e402bd42f | 55 | //lcd.cls(); |
MrBedfordVan | 3:389e402bd42f | 56 | |
MrBedfordVan | 3:389e402bd42f | 57 | int lcl_lap_time_integerPart = (int)lcl_lap_time; |
MrBedfordVan | 3:389e402bd42f | 58 | int lcl_lap_time_decimalPart = ((int)(lcl_lap_time*1000)%1000); |
MrBedfordVan | 3:389e402bd42f | 59 | |
MrBedfordVan | 3:389e402bd42f | 60 | sprintf(lap_string, "Lap : %d D : %d", (char*)lcl_lap_count, (char*)lcl_dist); // Format Lap count string |
MrBedfordVan | 3:389e402bd42f | 61 | // pc.printf("%s \n\r", lap_string); |
MrBedfordVan | 3:389e402bd42f | 62 | textLCD("1 ", 0); // Clear top line on LCD |
MrBedfordVan | 3:389e402bd42f | 63 | textLCD(lap_string, 0); // Display distance on LCD |
MrBedfordVan | 3:389e402bd42f | 64 | |
MrBedfordVan | 3:389e402bd42f | 65 | if (Show_Time) { |
MrBedfordVan | 3:389e402bd42f | 66 | sprintf(lap_string2, "Time : %d.%d s, ", (char*)lcl_lap_time_integerPart, (char*)lcl_lap_time_decimalPart); // Format Lap time string |
MrBedfordVan | 3:389e402bd42f | 67 | // pc.printf("%s \n\r", lap_string); |
MrBedfordVan | 3:389e402bd42f | 68 | textLCD("1 ", 1); // Clear bottom line on LCD |
MrBedfordVan | 3:389e402bd42f | 69 | textLCD(lap_string2, 1); // Display Lap Time on LCD |
MrBedfordVan | 3:389e402bd42f | 70 | } |
MrBedfordVan | 3:389e402bd42f | 71 | } |
MrBedfordVan | 3:389e402bd42f | 72 | |
MrBedfordVan | 2:b47f8dbf374c | 73 | /////////////////////////////////////////////////////////////////////////////// |
MrBedfordVan | 2:b47f8dbf374c | 74 | // Main Program starts here |
MrBedfordVan | 2:b47f8dbf374c | 75 | /////////////////////////////////////////////////////////////////////////////// |
MrBedfordVan | 0:b88af2ed370b | 76 | |
MrBedfordVan | 0:b88af2ed370b | 77 | int main() |
MrBedfordVan | 0:b88af2ed370b | 78 | { |
MrBedfordVan | 2:b47f8dbf374c | 79 | |
MrBedfordVan | 2:b47f8dbf374c | 80 | //// Try changing these: |
MrBedfordVan | 2:b47f8dbf374c | 81 | // Variables to tune |
MrBedfordVan | 2:b47f8dbf374c | 82 | float min_laptime = 1.0; // Minimum laptime - time to wait after seeing car before measuring again in seconds |
MrBedfordVan | 3:389e402bd42f | 83 | int sample_time = 500; // Sample time - time between sampling values from sensor in milliseconds |
MrBedfordVan | 3:389e402bd42f | 84 | int trigger_distance = 5; // Trigger distance - distance below which the car registers as detected |
MrBedfordVan | 2:b47f8dbf374c | 85 | //// Careful with anything after this though |
MrBedfordVan | 2:b47f8dbf374c | 86 | |
MrBedfordVan | 2:b47f8dbf374c | 87 | // Variables used within the program - set to their initial values |
MrBedfordVan | 0:b88af2ed370b | 88 | int dist = 0; |
MrBedfordVan | 3:389e402bd42f | 89 | float lap_time = 0.0; |
MrBedfordVan | 1:034f79c00c98 | 90 | int lap_count = 0; |
MrBedfordVan | 3:389e402bd42f | 91 | |
MrBedfordVan | 0:b88af2ed370b | 92 | |
MrBedfordVan | 2:b47f8dbf374c | 93 | // Initialise the serial interface and set its data rate |
MrBedfordVan | 3:389e402bd42f | 94 | // pc.baud(9600); |
MrBedfordVan | 0:b88af2ed370b | 95 | |
MrBedfordVan | 1:034f79c00c98 | 96 | // Intro Screen |
MrBedfordVan | 3:389e402bd42f | 97 | // pc.printf("ARM Sheffield Scalextric Challenge \n"); // Display to Serial Interface |
MrBedfordVan | 2:b47f8dbf374c | 98 | textLCD("ARM Scalextric ", 0); // Display on First Line of LCD |
MrBedfordVan | 3:389e402bd42f | 99 | wait_ms(2000); // Wait 2s |
MrBedfordVan | 3:389e402bd42f | 100 | textLCD(" ", 0); // Clear top line on LCD |
MrBedfordVan | 1:034f79c00c98 | 101 | |
MrBedfordVan | 3:389e402bd42f | 102 | // Main loop - runs forever |
MrBedfordVan | 1:034f79c00c98 | 103 | while(1) { |
MrBedfordVan | 2:b47f8dbf374c | 104 | // Set up and read ultrasonic sensor after "sample_time" |
MrBedfordVan | 0:b88af2ed370b | 105 | usensor.start(); |
MrBedfordVan | 0:b88af2ed370b | 106 | wait_ms(sample_time); |
MrBedfordVan | 2:b47f8dbf374c | 107 | dist=usensor.get_dist_cm(); // read sesnsor distance measure |
MrBedfordVan | 3:389e402bd42f | 108 | //pc.printf("Lap %d Distance %d wait_time %d \n\r", lap_count, dist, wait_time); // Debug - display distance to Serial Interface |
MrBedfordVan | 3:389e402bd42f | 109 | if (dist < trigger_distance) { // Spotted something closer than trigger_distance from the sensor |
MrBedfordVan | 2:b47f8dbf374c | 110 | if (lap_count == 0) { // First Lap - start lap timer |
MrBedfordVan | 1:034f79c00c98 | 111 | lap_timer.reset(); |
MrBedfordVan | 1:034f79c00c98 | 112 | lap_timer.start(); |
MrBedfordVan | 3:389e402bd42f | 113 | //pc.printf("Timing... \r"); // Debug to Serial Interface |
MrBedfordVan | 2:b47f8dbf374c | 114 | textLCD("Timing..........", 1); // Message to LCD |
MrBedfordVan | 1:034f79c00c98 | 115 | } else { // Completed a lap |
MrBedfordVan | 2:b47f8dbf374c | 116 | lap_time = lap_timer.read(); // Get time for lap |
MrBedfordVan | 1:034f79c00c98 | 117 | lap_timer.reset(); |
MrBedfordVan | 3:389e402bd42f | 118 | DisplayLap(lap_count, dist, lap_time, 1); |
MrBedfordVan | 0:b88af2ed370b | 119 | } |
MrBedfordVan | 2:b47f8dbf374c | 120 | lap_count++; // Increment lap counter by 1 |
MrBedfordVan | 3:389e402bd42f | 121 | wait(min_laptime); // Wait for a time before starting to monitor sensor for next lap |
MrBedfordVan | 3:389e402bd42f | 122 | } // end of something spotted |
MrBedfordVan | 3:389e402bd42f | 123 | else // nothing spotted - output debug |
MrBedfordVan | 3:389e402bd42f | 124 | { |
MrBedfordVan | 3:389e402bd42f | 125 | DisplayLap(lap_count, dist, lap_time, 0); |
MrBedfordVan | 3:389e402bd42f | 126 | } |
MrBedfordVan | 3:389e402bd42f | 127 | } // end of main while loop |
MrBedfordVan | 3:389e402bd42f | 128 | |
MrBedfordVan | 3:389e402bd42f | 129 | |
MrBedfordVan | 2:b47f8dbf374c | 130 | } // end of main program |
MrBedfordVan | 0:b88af2ed370b | 131 |