Facebook
From Jacque Dixon, 2 Years ago, written in C++.
Embed
Download Paste or View Raw
Hits: 105
  1. #include <IRremote.h>
  2. #include <LiquidCrystal.h>
  3.  
  4.  
  5. // laps info
  6. unsigned long car20currentRunStartMillis;
  7. unsigned long car20lastRunInMillis;
  8. int car20currentLap;
  9.  
  10. unsigned long car30currentRunStartMillis;
  11. unsigned long car30lastRunInMillis;
  12. int car30currentLap;
  13.  
  14. unsigned long car40currentRunStartMillis;
  15. unsigned long car40lastRunInMillis;
  16. int car40currentLap;
  17.  
  18. unsigned long car50currentRunStartMillis;
  19. unsigned long car50lastRunInMillis;
  20. int car50currentLap;
  21.  
  22. // laser gate
  23. boolean car20lastgatesensorstate = LOW;   // the previous reading from sensor
  24. unsigned long car20lastDebounceTime = 0;  // the last time the sensor pin was toggled
  25. boolean car20reading = LOW;
  26.  
  27. boolean car30lastgatesensorstate = LOW;   // the previous reading from sensor
  28. unsigned long car30lastDebounceTime = 0;  // the last time the sensor pin was toggled
  29. boolean car30reading = LOW;
  30.  
  31. boolean car40lastgatesensorstate = LOW;   // the previous reading from sensor
  32. unsigned long car40lastDebounceTime = 0;  // the last time the sensor pin was toggled
  33. boolean car40reading = LOW;
  34.  
  35. boolean car50lastgatesensorstate = LOW;   // the previous reading from sensor
  36. unsigned long car50lastDebounceTime = 0;  // the last time the sensor pin was toggled
  37. boolean car50reading = LOW;
  38.  
  39. unsigned long bestRunInMillis;
  40. const int RECV_PIN = 7;
  41. IRrecv irrecv(RECV_PIN);
  42. decode_results results;
  43. LiquidCrystal lcd(9, 8, 5, 4, 3, 2);
  44.  
  45. void setup() {
  46.   Serial.begin(9600);
  47.   // pin mode
  48.   delay(50); // to late the sensor and laser work, so we wont get the lap triggered.
  49.   lcd.begin(16, 4);
  50.   delay(3000);
  51.   lcd.setCursor(0, 0);
  52.   lcd.print("C20:");
  53.   delay(50);
  54.   lcd.setCursor(0, 1);
  55.   lcd.print("C30:");
  56.   delay(50);
  57.   lcd.setCursor(4, 2);
  58.   lcd.print("C40:");
  59.   delay(50);
  60.   lcd.setCursor(4, 3);
  61.   lcd.print("C50:");
  62.   delay(50);
  63.   lcd.setCursor(11, 0);
  64.   lcd.print("|:");
  65.   lcd.setCursor(11, 1);
  66.   lcd.print("|:");
  67.   lcd.setCursor(15, 2);
  68.   lcd.print("|:");
  69.   lcd.setCursor(15, 3);
  70.   lcd.print("|:");
  71.   irrecv.enableIRIn();
  72.   irrecv.blink13(true);
  73.  
  74.   // reset params
  75.   car20currentRunStartMillis = 0;
  76.   car20lastRunInMillis = 0;
  77.   car20currentLap = 0;
  78.  
  79.   car30currentRunStartMillis = 0;
  80.   car30lastRunInMillis = 0;
  81.   car30currentLap = 0;
  82.  
  83.   car40currentRunStartMillis = 0;
  84.   car40lastRunInMillis = 0;
  85.   car40currentLap = 0;
  86.  
  87.   car50currentRunStartMillis = 0;
  88.   car50lastRunInMillis = 0;
  89.   car50currentLap = 0;
  90.  
  91. }
  92.  
  93. void loop()
  94. {
  95.   if (irrecv.decode(&results)) {
  96.     Serial.println(results.value, HEX);
  97.     if ((results.value == 0x550BB350)  && ((millis() - car20lastDebounceTime) >= (1000))) {
  98.       car20reading = HIGH;
  99.       Car20();
  100.     }
  101.     else if ((results.value == 0xEA1500FF) && ((millis() - car30lastDebounceTime) >= (1000))) {
  102.       car30reading = HIGH;
  103.       Car30();
  104.     }
  105.     else if ((results.value == 0xFD20DF) && ((millis() - car40lastDebounceTime) >= (1000))) {
  106.       car40reading = HIGH;
  107.       Car40();
  108.     }
  109.     else if ((results.value == 0xFD20DF) && ((millis() - car50lastDebounceTime) >= (1000))) {
  110.       car50reading = HIGH;
  111.       Car50();
  112.     }
  113.     irrecv.resume();
  114.   }
  115.  
  116.   //Serial.println(bestRunInMins);
  117. }
  118.  
  119. void Car20() {
  120.  
  121.   unsigned long car20savedMillis;
  122.   unsigned long car20fastestlap;
  123.  
  124.   // If the switch changed, due to noise or pressing:
  125.   if (car20reading != car20lastgatesensorstate) {
  126.     // reset the debouncing timer
  127.     car20lastDebounceTime = millis();
  128.   }
  129.  
  130.   if (car20reading != car20lastgatesensorstate) {
  131.     car20lastgatesensorstate = car20reading;
  132.  
  133.     // If we went High, this mean the beam was broken
  134.     if (car20lastgatesensorstate == HIGH) {
  135.       // save the millis so all the math on it will be done with the same value.
  136.       car20savedMillis = millis();
  137.       // if its not the first lap
  138.       if (car20currentLap > 0) {
  139.         // save the last run
  140.         car20lastRunInMillis = car20savedMillis - car20currentRunStartMillis;
  141.         lcd.setCursor(4, 0);
  142.         lcd.print(car20lastRunInMillis / 1000.0, 3);
  143.         // if last run is faster then best run
  144.         if (car20lastRunInMillis < car20fastestlap || car20fastestlap == 0) {
  145.  
  146.           car20fastestlap = car20lastRunInMillis;
  147.           lcd.setCursor(14, 0);
  148.           lcd.print(car20fastestlap / 1000.0, 3);
  149.         }
  150.       }
  151.  
  152.  
  153.       car20currentRunStartMillis = car20savedMillis;
  154.  
  155.  
  156.       car20currentLap++;
  157.  
  158.       if (car20fastestlap < bestRunInMillis || bestRunInMillis == 0) {
  159.         bestRunInMillis = car20fastestlap;
  160.       }
  161.       car20lastgatesensorstate = LOW;
  162.     }
  163.   }
  164.  
  165. }
  166.  
  167. void Car30() {
  168.  
  169.   unsigned long car30savedMillis;
  170.   unsigned long car30fastestlap;
  171.  
  172.   // If the switch changed, due to noise or pressing:
  173.   if (car30reading != car30lastgatesensorstate) {
  174.     // reset the debouncing timer
  175.     car30lastDebounceTime = millis();
  176.   }
  177.  
  178.   if (car30reading != car30lastgatesensorstate) {
  179.     car30lastgatesensorstate = car30reading;
  180.  
  181.     // If we went High, this mean the beam was broken
  182.     if (car30lastgatesensorstate == HIGH) {
  183.       // save the millis so all the math on it will be done with the same value.
  184.       car30savedMillis = millis();
  185.       // if its not the first lap
  186.       if (car30currentLap > 0) {
  187.         // save the last run
  188.         car30lastRunInMillis = car30savedMillis - car30currentRunStartMillis;
  189.         lcd.setCursor(4, 1);
  190.         lcd.print(car30lastRunInMillis / 1000.0, 3);
  191.         // if last run is faster then best run
  192.         if (car30lastRunInMillis < car30fastestlap || car30fastestlap == 0) {
  193.  
  194.           car30fastestlap = car30lastRunInMillis;
  195.           lcd.setCursor(12, 1);
  196.           lcd.print(car30fastestlap / 1000.0, 3);
  197.         }
  198.       }
  199.  
  200.  
  201.       car30currentRunStartMillis = car30savedMillis;
  202.  
  203.  
  204.       car30currentLap++;
  205.  
  206.       if (car30fastestlap < bestRunInMillis || bestRunInMillis == 0) {
  207.         bestRunInMillis = car30fastestlap;
  208.       }
  209.       car30lastgatesensorstate = LOW;
  210.     }
  211.   }
  212.  
  213. }
  214.  
  215. void Car40() {
  216.  
  217.   unsigned long car40savedMillis;
  218.   unsigned long car40fastestlap;
  219.  
  220.   // If the switch changed, due to noise or pressing:
  221.   if (car40reading != car40lastgatesensorstate) {
  222.     // reset the debouncing timer
  223.     car40lastDebounceTime = millis();
  224.   }
  225.  
  226.   if (car40reading != car40lastgatesensorstate) {
  227.     car40lastgatesensorstate = car40reading;
  228.  
  229.     // If we went High, this mean the beam was broken
  230.     if (car40lastgatesensorstate == HIGH) {
  231.       // save the millis so all the math on it will be done with the same value.
  232.       car40savedMillis = millis();
  233.       // if its not the first lap
  234.       if (car40currentLap > 0) {
  235.         // save the last run
  236.         car40lastRunInMillis = car40savedMillis - car40currentRunStartMillis;
  237.         lcd.setCursor(7, 2);
  238.         lcd.print(car40lastRunInMillis / 1000.0, 3);
  239.         // if last run is faster then best run
  240.         if (car40lastRunInMillis < car40fastestlap || car40fastestlap == 0) {
  241.  
  242.           car40fastestlap = car40lastRunInMillis;
  243.           lcd.setCursor(12, 1);
  244.           lcd.print(car40fastestlap / 1000.0, 3);
  245.         }
  246.       }
  247.  
  248.  
  249.       car40currentRunStartMillis = car40savedMillis;
  250.  
  251.  
  252.       car40currentLap++;
  253.  
  254.       if (car40fastestlap < bestRunInMillis || bestRunInMillis == 0) {
  255.         bestRunInMillis = car40fastestlap;
  256.       }
  257.       car40lastgatesensorstate = LOW;
  258.     }
  259.   }
  260.  
  261. }
  262.  
  263. void Car50() {
  264.  
  265.   unsigned long car50savedMillis;
  266.   unsigned long car50fastestlap;
  267.  
  268.   // If the switch changed, due to noise or pressing:
  269.   if (car50reading != car50lastgatesensorstate) {
  270.     // reset the debouncing timer
  271.     car50lastDebounceTime = millis();
  272.   }
  273.  
  274.   if (car50reading != car50lastgatesensorstate) {
  275.     car50lastgatesensorstate = car50reading;
  276.  
  277.     // If we went High, this mean the beam was broken
  278.     if (car50lastgatesensorstate == HIGH) {
  279.       // save the millis so all the math on it will be done with the same value.
  280.       car50savedMillis = millis();
  281.       // if its not the first lap
  282.       if (car50currentLap > 0) {
  283.         // save the last run
  284.         car50lastRunInMillis = car50savedMillis - car50currentRunStartMillis;
  285.         lcd.setCursor(7, 3);
  286.         lcd.print(car50lastRunInMillis / 1000.0, 3);
  287.         // if last run is faster then best run
  288.         if (car50lastRunInMillis < car50fastestlap || car50fastestlap == 0) {
  289.  
  290.           car50fastestlap = car50lastRunInMillis;
  291.           lcd.setCursor(12, 1);
  292.           lcd.print(car50fastestlap / 1000.0, 3);
  293.         }
  294.       }
  295.  
  296.  
  297.       car50currentRunStartMillis = car50savedMillis;
  298.  
  299.  
  300.       car50currentLap++;
  301.  
  302.       if (car50fastestlap < bestRunInMillis || bestRunInMillis == 0) {
  303.         bestRunInMillis = car50fastestlap;
  304.       }
  305.       car50lastgatesensorstate = LOW;
  306.     }
  307.   }
  308.  
  309. }
  310.  
  311.