Advertisement
Maderdash

Untitled

Apr 13th, 2021
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.76 KB | None | 0 0
  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.  
  11. // laser gate
  12. boolean car20lastgatesensorstate = LOW;   // the previous reading from sensor
  13. unsigned long car20lastDebounceTime = 0;  // the last time the sensor pin was toggled
  14. boolean car20reading = LOW;
  15.  
  16. unsigned long bestRunInMillis;
  17. int debounceDelay = 5000;
  18. const int RECV_PIN = 7;
  19. IRrecv irrecv(RECV_PIN);
  20. decode_results results;
  21. LiquidCrystal lcd(9, 8, 5, 4, 3, 2);
  22.  
  23. void setup() {
  24.   Serial.begin(9600);
  25.   // pin mode
  26.   delay(50); // to late the sensor and laser work, so we wont get the lap triggered.
  27.   lcd.begin(16,4);
  28.   lcd.setCursor(0,0);
  29.   lcd.print("Car Tracker");
  30.   delay(3000);
  31.   lcd.clear();
  32.   lcd.setCursor(0,0);
  33.   lcd.print("C20:");
  34.   delay(50);
  35.   lcd.setCursor(10,0);
  36.   lcd.print("|:");
  37.   irrecv.enableIRIn();
  38.   irrecv.blink13(true);
  39.  
  40.   // reset params
  41.   car20currentRunStartMillis = 0;
  42.   car20lastRunInMillis = 0;
  43.   car20currentLap = 0;
  44.  
  45. }
  46.  
  47. void loop()
  48. {  
  49.   if (irrecv.decode(&results) && ((millis() - car20lastDebounceTime) >= (3000))){
  50.     car20lastDebounceTime = millis();
  51.     Serial.println(results.value, HEX);
  52.         if (results.value == 0xBA4500FF) {
  53.         car20reading = HIGH;
  54.         }
  55.         Car20();
  56.         irrecv.resume();
  57.     }
  58.  
  59. }
  60.  
  61.  void Car20() {
  62.  
  63. unsigned long car20savedMillis;
  64. unsigned long car20fastestlap;
  65.  
  66.   // If the switch changed, due to noise or pressing:
  67.   if (car20reading != car20lastgatesensorstate) {
  68.     // reset the debouncing timer
  69.     car20lastDebounceTime = millis();
  70.   }
  71.  
  72.     if (car20reading != car20lastgatesensorstate) {
  73.       car20lastgatesensorstate = car20reading;
  74.  
  75.       // If we went High, this mean the beam was broken
  76.       if (car20lastgatesensorstate == HIGH) {
  77.         // save the millis so all the math on it will be done with the same value.
  78.         car20savedMillis = millis();
  79.         // if its not the first lap
  80.         if (car20currentLap > 0) {
  81.           // save the last run
  82.           car20lastRunInMillis = car20savedMillis - car20currentRunStartMillis;
  83.           // if last run is faster then best run
  84.           if (car20lastRunInMillis < car20fastestlap || car20fastestlap == 0) {
  85.  
  86.             car20fastestlap = car20lastRunInMillis;
  87.           }
  88.         }
  89.  
  90.  
  91.         car20currentRunStartMillis = car20savedMillis;
  92.  
  93.  
  94.         car20currentLap++;
  95.  
  96.   lcd.setCursor(4,0);
  97.   lcd.print(car20lastRunInMillis / 1000.0, 3);
  98.   lcd.setCursor(12,0);
  99.   lcd.print(car20fastestlap / 1000.0, 3);
  100.  
  101.     if (car20fastestlap < bestRunInMillis || bestRunInMillis == 0) {
  102.     bestRunInMillis = car20fastestlap;
  103.   }
  104.   car20lastgatesensorstate = LOW;
  105.       }
  106.     }
  107.  
  108.   }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement