Advertisement
ZulRocky

Speedometer ReedSwitch

Dec 17th, 2018
199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <LiquidCrystal_I2C.h>
  2.  
  3. LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);
  4.  
  5. const int reedPin = 2;
  6.  
  7. float radius = 18;
  8. float circumference;
  9.  
  10. int reedState;
  11. int lastReedState = LOW;
  12.  
  13. long timer = 0;
  14. float kmh = 0.00;
  15. float maximal = 0.00;
  16.  
  17. // the following variables are unsigned longs because the time, measured in
  18. // milliseconds, will quickly become a bigger number than can be stored in an int.
  19. unsigned long lastDebounceTime = 0;
  20. unsigned long lastDebounceTime1 = 0;
  21. unsigned long lastDebounceTime2 = 0;
  22. unsigned long debounceDelay = 20;    // the debounce time; increase if the output flickers
  23.  
  24. unsigned long previousMillis13 = 0;
  25. unsigned long previousMillis1 = 0;
  26.  
  27. void setup()
  28. {
  29.   circumference = 2*3.14*radius; // 89.92
  30.   pinMode(reedPin, INPUT);
  31.  
  32.   Serial.write(12);//clear
  33.  
  34.   lcd.begin(16, 2);
  35.   Serial.begin(9600);
  36. }
  37.  
  38. void loop() {
  39.   // read the state of the switch into a local variable:
  40.   int reading = digitalRead(reedPin);
  41.  
  42.   // check to see if you just pressed the button
  43.   // (i.e. the input went from LOW to HIGH), and you've waited long enough
  44.   // since the last press to ignore any noise:
  45.  
  46.   // If the switch changed, due to noise or pressing:
  47.   if (reading != lastReedState) {
  48.     // reset the debouncing timer
  49.     lastDebounceTime = millis();
  50.   }
  51.  
  52.   if ((millis() - lastDebounceTime) > debounceDelay) {
  53.  
  54.       if (reading != reedState) {
  55.         reedState = reading;
  56.        
  57.         if (reedState == HIGH) {
  58.           lastDebounceTime1 = millis();
  59.           timer = lastDebounceTime1 - lastDebounceTime2;
  60.         }else{
  61.           lastDebounceTime2 = millis();
  62.         }
  63.  
  64.         Serial.println(timer);
  65.        
  66.         kmh = (56.8*float(circumference))/float(timer);//calculate miles per hour
  67.       }
  68.   }
  69.  
  70.   // set the LED:
  71.   //digitalWrite(ledPin, ledState);
  72.  
  73.   // save the reading. Next time through the loop, it'll be the lastButtonState:
  74.   lastReedState = reading;
  75.  
  76.   if(millis() - previousMillis13 >= 500){
  77.     previousMillis13 = millis();
  78.     displayKMH();
  79.     if(kmh > maximal){
  80.       maximal = kmh;
  81.     }
  82.     displayMAX();
  83.   }
  84.   if(millis() - lastDebounceTime2 >= 2000){
  85.     kmh = 0.00;
  86.   }
  87. }
  88.  
  89. void displayKMH(){
  90.   lcd.setCursor(0, 0);
  91.   lcd.print("      ");
  92.   lcd.setCursor(0, 0);
  93.   lcd.print(kmh);
  94.   lcd.setCursor(6, 0);
  95.   lcd.print(" KM/H  ");
  96. }
  97.  
  98. void displayMAX(){
  99.   lcd.setCursor(0, 1);
  100.   lcd.print(" MAX ");
  101.   lcd.print(maximal);
  102.   lcd.setCursor(11, 1);
  103.   lcd.print(" KM/H  ");
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement