Advertisement
talofer99

Slot Car Time Keeping Gate

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