Scalextric Lap Timer using ultrasonic sensor

Dependencies:   HCSR04 mbed TextLCD

Committer:
MrBedfordVan
Date:
Tue Oct 11 09:52:30 2016 +0000
Revision:
2:b47f8dbf374c
Parent:
1:034f79c00c98
Child:
3:389e402bd42f
Changed some variables

Who changed what in which revision?

UserRevisionLine numberNew 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 0:b88af2ed370b 13 Serial pc(SERIAL_TX, SERIAL_RX);
MrBedfordVan 1:034f79c00c98 14 // Ultrasonic sensor
MrBedfordVan 1:034f79c00c98 15 HCSR04 usensor(D3,D2); // Trig, Echo
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 1:034f79c00c98 19 AnalogIn keys(A0); // used to sample buttons
MrBedfordVan 1:034f79c00c98 20
MrBedfordVan 1:034f79c00c98 21 Timer lap_timer;
MrBedfordVan 1:034f79c00c98 22 Timer wait_timer;
MrBedfordVan 1:034f79c00c98 23
MrBedfordVan 2:b47f8dbf374c 24 // Subroutine to display text on LCD - called from below
MrBedfordVan 2:b47f8dbf374c 25 // call as follows:
MrBedfordVan 2:b47f8dbf374c 26 // textLCD(text, line)
MrBedfordVan 2:b47f8dbf374c 27 // where text = up to 16 characters of text
MrBedfordVan 2:b47f8dbf374c 28 // line = line 0 or 1 on the LCD; where 0 is the top line and 1 is the bottom line
MrBedfordVan 2:b47f8dbf374c 29
MrBedfordVan 1:034f79c00c98 30 void textLCD(char *text, int line) {
MrBedfordVan 2:b47f8dbf374c 31
MrBedfordVan 1:034f79c00c98 32 char tmpBuf[16];
MrBedfordVan 1:034f79c00c98 33 for (int i = 0; i < 16; i++) tmpBuf[i] = 0x20;
MrBedfordVan 1:034f79c00c98 34 for (int i = 0; i < strlen(text); i++) {
MrBedfordVan 1:034f79c00c98 35 if (i < 16) tmpBuf[i] = text[i];
MrBedfordVan 1:034f79c00c98 36 lcd.locate(i, line);
MrBedfordVan 1:034f79c00c98 37 lcd.putc(tmpBuf[i]);
MrBedfordVan 1:034f79c00c98 38 }
MrBedfordVan 1:034f79c00c98 39 }
MrBedfordVan 1:034f79c00c98 40
MrBedfordVan 2:b47f8dbf374c 41 ///////////////////////////////////////////////////////////////////////////////
MrBedfordVan 2:b47f8dbf374c 42 // Main Program starts here
MrBedfordVan 2:b47f8dbf374c 43 ///////////////////////////////////////////////////////////////////////////////
MrBedfordVan 0:b88af2ed370b 44
MrBedfordVan 0:b88af2ed370b 45 int main()
MrBedfordVan 0:b88af2ed370b 46 {
MrBedfordVan 2:b47f8dbf374c 47
MrBedfordVan 2:b47f8dbf374c 48 //// Try changing these:
MrBedfordVan 2:b47f8dbf374c 49 // Variables to tune
MrBedfordVan 2:b47f8dbf374c 50 float min_laptime = 1.0; // Minimum laptime - time to wait after seeing car before measuring again in seconds
MrBedfordVan 2:b47f8dbf374c 51 int sample_time = 100; // Sample time - time between sampling values from sensor in milliseconds
MrBedfordVan 2:b47f8dbf374c 52 int trigger_distance = 2; // Trigger distance - distance below which the car registers as detected
MrBedfordVan 2:b47f8dbf374c 53 //// Careful with anything after this though
MrBedfordVan 2:b47f8dbf374c 54
MrBedfordVan 2:b47f8dbf374c 55 // Variables used within the program - set to their initial values
MrBedfordVan 0:b88af2ed370b 56 int dist = 0;
MrBedfordVan 2:b47f8dbf374c 57 float lap_time = 0;
MrBedfordVan 1:034f79c00c98 58 int lap_count = 0;
MrBedfordVan 2:b47f8dbf374c 59 float wait_time = min_laptime + 1.0; // ensure larger than minimum lap time for first loop
MrBedfordVan 2:b47f8dbf374c 60
MrBedfordVan 2:b47f8dbf374c 61 // Variables used to hold text to display on LCD for correct formatting
MrBedfordVan 1:034f79c00c98 62 char lap_string[16];
MrBedfordVan 1:034f79c00c98 63 char laptime_string[16];
MrBedfordVan 0:b88af2ed370b 64
MrBedfordVan 2:b47f8dbf374c 65 // Initialise the serial interface and set its data rate
MrBedfordVan 2:b47f8dbf374c 66 pc.baud(9600);
MrBedfordVan 0:b88af2ed370b 67
MrBedfordVan 1:034f79c00c98 68 // Intro Screen
MrBedfordVan 2:b47f8dbf374c 69 pc.printf("ARM Sheffield Scalextric Challenge \n"); // Display to Serial Interface
MrBedfordVan 2:b47f8dbf374c 70 textLCD("ARM Scalextric ", 0); // Display on First Line of LCD
MrBedfordVan 1:034f79c00c98 71
MrBedfordVan 2:b47f8dbf374c 72 // Main loop - runs foerever
MrBedfordVan 1:034f79c00c98 73 while(1) {
MrBedfordVan 2:b47f8dbf374c 74 // Set up and read ultrasonic sensor after "sample_time"
MrBedfordVan 0:b88af2ed370b 75 usensor.start();
MrBedfordVan 0:b88af2ed370b 76 wait_ms(sample_time);
MrBedfordVan 2:b47f8dbf374c 77 dist=usensor.get_dist_cm(); // read sesnsor distance measure
MrBedfordVan 2:b47f8dbf374c 78 pc.printf("Distance %d\n\r", dist); // Debug - display distance to Serial Interface
MrBedfordVan 2:b47f8dbf374c 79 if (lap_count > 0) { // If not the first lap - read value of wait timer
MrBedfordVan 1:034f79c00c98 80 wait_time = wait_timer.read();
MrBedfordVan 0:b88af2ed370b 81 }
MrBedfordVan 2:b47f8dbf374c 82 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
MrBedfordVan 2:b47f8dbf374c 83 wait_timer.reset(); // reset the wait timer
MrBedfordVan 2:b47f8dbf374c 84 wait_timer.start(); // start the wait timer again
MrBedfordVan 2:b47f8dbf374c 85 if (lap_count == 0) { // First Lap - start lap timer
MrBedfordVan 1:034f79c00c98 86 lap_timer.reset();
MrBedfordVan 1:034f79c00c98 87 lap_timer.start();
MrBedfordVan 2:b47f8dbf374c 88 pc.printf("Timing... \r"); // Debug to Serial Interface
MrBedfordVan 2:b47f8dbf374c 89 textLCD("Timing..........", 1); // Message to LCD
MrBedfordVan 1:034f79c00c98 90 } else { // Completed a lap
MrBedfordVan 2:b47f8dbf374c 91 lap_time = lap_timer.read(); // Get time for lap
MrBedfordVan 1:034f79c00c98 92 lap_timer.reset();
MrBedfordVan 2:b47f8dbf374c 93 pc.printf("Lap : %d, Lap time : %f s \r", lap_count, lap_time); // Debug to Serial Interface
MrBedfordVan 2:b47f8dbf374c 94 sprintf(lap_string, "Lap : %d ", (char*)lap_count); // Format Lap count string
MrBedfordVan 2:b47f8dbf374c 95 sprintf(laptime_string, "Lap time : %f s, ", lap_time); // Format Lap time string
MrBedfordVan 2:b47f8dbf374c 96 textLCD(" ", 0); // Clear top line on LCD
MrBedfordVan 2:b47f8dbf374c 97 textLCD(lap_string, 0); // Display Lap Count on LCD
MrBedfordVan 2:b47f8dbf374c 98 textLCD(" ", 1); // Clear bottom line on LCD
MrBedfordVan 2:b47f8dbf374c 99 textLCD(laptime_string, 1); // Display Lap Time on LCD
MrBedfordVan 0:b88af2ed370b 100 }
MrBedfordVan 2:b47f8dbf374c 101 lap_count++; // Increment lap counter by 1
MrBedfordVan 2:b47f8dbf374c 102 } // end of something spotted
MrBedfordVan 2:b47f8dbf374c 103 } // end of while loop
MrBedfordVan 2:b47f8dbf374c 104 } // end of main program
MrBedfordVan 0:b88af2ed370b 105