talofer99

Slot Car Time Keeping Gate

Apr 3rd, 2019
968
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.07 KB | None | 0 0
  1. /*
  2. * Time keeping digital gate
  3. * Code by: Tal Ofer
  4. * GLCD pinout https://playground.arduino.cc/Code/GLCDks0108/
  5. * GLCD Library https://bitbucket.org/bperrybap/openglcd
  6. */
  7.  
  8.  
  9. #include "openGLCD.h"
  10.  
  11. // laps info
  12. unsigned long currentRunStartMillis;
  13. unsigned long lastRunInMillis;
  14. unsigned long bestRunInMillis;
  15. int currentLap;
  16. unsigned long savedMillis;
  17.  
  18. gText t1; // will define runtime later
  19. gText t2; // will define runtime later
  20. gText t3; // will define runtime later
  21.  
  22. // global for display
  23. int sec_val, milli_val;
  24.  
  25. // laser gate
  26. const int gateSensorPin = 2;    // the number of the gate sensor pin
  27. int gateSensorState;             // the current reading from the sensor
  28. int lastgateSensorState = LOW;   // the previous reading from sensor
  29. unsigned long lastDebounceTime = 0;  // the last time the sensor pin was toggled
  30. int debounceDelay = 50;    // the debounce time; increase if the output flickers
  31.  
  32.  
  33. void setup() {
  34.   // pin mode
  35.   pinMode(gateSensorPin, INPUT);
  36.   delay(50); // to late the sensor and laser work, so we wont get the lap triggered.
  37.   // start GLCD
  38.   GLCD.Init(NON_INVERTED);
  39.   // define areas
  40.   t1.DefineArea(textAreaTOP, lcdnums14x24);
  41.   t2.DefineArea(0, GLCD.CenterY, 8, 2, fixednums7x15);
  42.   t3.DefineArea(GLCD.CenterX, GLCD.CenterY, 8, 2, fixednums7x15);
  43.   t3.SetFontColor(WHITE); // set font color
  44.   t3.ClearArea();
  45.   // print text
  46.   GLCD.SelectFont(System5x7);
  47.   GLCD.CursorTo(1, 4);
  48.   GLCD.print("LAST");
  49.   GLCD.CursorTo(11, 4);
  50.   GLCD.print("BEST");
  51.   // reset params
  52.   currentRunStartMillis = 0;
  53.   lastRunInMillis = 0;
  54.   bestRunInMillis = 0;
  55.   currentLap = 0;
  56.  
  57. }
  58.  
  59. void loop()
  60. {
  61.   // read the state of the laser sensor:
  62.   int reading = digitalRead(gateSensorPin);
  63.   // If the switch changed, due to noise or pressing:
  64.   if (reading != lastgateSensorState) {
  65.     // reset the debouncing timer
  66.     lastDebounceTime = millis();
  67.   } //end if
  68.  
  69.   // if passes the debounce time
  70.   if ((millis() - lastDebounceTime) > debounceDelay) {
  71.     if (reading != gateSensorState) {
  72.       gateSensorState = reading;
  73.  
  74.       // If we went low, this mean the beam was broken
  75.       if (gateSensorState == LOW) {
  76.         // save the millis so all the math on it will be done with the same value.
  77.         savedMillis = millis();
  78.         // if its not the first lap
  79.         if (currentLap > 0) {
  80.           // save the last run
  81.           lastRunInMillis = savedMillis - currentRunStartMillis;
  82.           // if last run is faster then best run
  83.           if (lastRunInMillis < bestRunInMillis || bestRunInMillis == 0) {
  84.             //save as best
  85.             bestRunInMillis = lastRunInMillis;
  86.           } //end if
  87.         } //end if
  88.        
  89.         //reset the current
  90.         currentRunStartMillis = savedMillis;
  91.        
  92.         // move lap counter
  93.         currentLap++;
  94.       } //end if
  95.     } //enf if
  96.   } //end if
  97.  
  98.  
  99.   // save the reading. Next time through the loop, it'll be the lastgateSensorState:
  100.   lastgateSensorState = reading;
  101.  
  102.  
  103.  
  104.   // print Laps
  105.   t1.CursorTo(0, 0); // set in location
  106.   t1.Printf(F("%02d"), currentLap);
  107.  
  108.   // save current milis
  109.   savedMillis = millis();
  110.  
  111.   // if we start the first lap
  112.   if (currentLap > 0) {
  113.     calcResultFromMillis(savedMillis - currentRunStartMillis, &sec_val, &milli_val);
  114.   } else {
  115.     calcResultFromMillis(0, &sec_val, &milli_val);
  116.   } //end if
  117.  
  118.   // CURRENT RUN
  119.   t1.CursorTo(3, 0); // column & row is relative to text area
  120.   t1.Printf(F("%02d.%03d"), sec_val, milli_val);
  121.  
  122.   // LAST RUN
  123.   calcResultFromMillis(lastRunInMillis, &sec_val, &milli_val);
  124.   t2.CursorTo(1, 1); // column & row is relative to text area
  125.   t2.Printf(F("%02d.%03d"), sec_val, milli_val);
  126.  
  127.   // BEST RUN
  128.   calcResultFromMillis(bestRunInMillis, &sec_val, &milli_val);
  129.   t3.CursorTo(1, 1);
  130.   t3.Printf(F("%02d.%03d"), sec_val, milli_val);
  131.  
  132. } //wnd loop
  133.  
  134.  
  135.  
  136.  
  137. // calculate millis into 2 values, seconeds and millis for display
  138. void calcResultFromMillis(unsigned long value, int *sec_val, int *milli_val) {
  139.   *sec_val = int(value / 1000);
  140.   *milli_val = value - *sec_val * 1000;
  141. }
Advertisement
Add Comment
Please, Sign In to add comment