Scalextric Lap Timer using ultrasonic sensor

Dependencies:   HCSR04 mbed TextLCD

Revision:
2:b47f8dbf374c
Parent:
1:034f79c00c98
Child:
3:389e402bd42f
--- a/main.cpp	Wed Sep 28 16:05:35 2016 +0000
+++ b/main.cpp	Tue Oct 11 09:52:30 2016 +0000
@@ -21,8 +21,14 @@
 Timer lap_timer;
 Timer wait_timer;
 
-// display text on LCD
+// 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++) {
@@ -32,58 +38,68 @@
     }
 }
 
+///////////////////////////////////////////////////////////////////////////////
+// Main Program starts here
+///////////////////////////////////////////////////////////////////////////////
 
 int main()
 {
-    // Variables used
+ 
+ //// 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 current_time = 0;
+    float lap_time = 0;
     int lap_count = 0;
-    float wait_time = 2.0;
-    pc.baud(9600);
+    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];
     
-    // 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
+    // Initialise the serial interface and set its data rate
+    pc.baud(9600);
 
     // Intro Screen
-    pc.printf("ARM Sheffield Scalextric Challenge \n");
-    textLCD("ARM Scalextric ", 0);
+    pc.printf("ARM Sheffield Scalextric Challenge \n"); // Display to Serial Interface
+    textLCD("ARM Scalextric ", 0);  // Display on First Line of LCD
 
-    // Main loop
+    // 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();
-       pc.printf("Distance %d\n\r", dist);  // Debug to Serial port/USB
-       if (lap_count > 0) { 
+       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 car at least a minimum laptime later
-         wait_timer.reset();
-         wait_timer.start();
-         if (lap_count == 0) {   // First Lap - start timer
+       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
-           textLCD("Timing..........", 1);   // 
-           lap_count++; 
+           pc.printf("Timing... \r");        // Debug to Serial Interface
+           textLCD("Timing..........", 1);   // Message to LCD
          } else {                // Completed a lap
-           current_time = lap_timer.read();
+           lap_time = lap_timer.read();  // Get time for lap
            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++; 
+           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