Advertisement
talofer99

Bicycle speedometer

Oct 28th, 2020
2,528
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.20 KB | None | 0 0
  1. // include the library code:
  2. #include <LiquidCrystal.h>
  3.  
  4. // initialize the library with the numbers of the interface pins
  5. LiquidCrystal lcd(12, 11, 10, 9, 8, 7);
  6. // general place holder (for use in functions)
  7. unsigned long millisValue = 0;
  8. // speed value
  9. float Speed = 0 ;
  10. // wheel size in CM
  11. const int Wheel_circumference_in_Cm = 210 ;  // (2PiR).
  12. // Magnatic reader params
  13. const int MagneticReader_Pin = 2;
  14. boolean MagneticReader_Last_state;
  15. boolean MagneticReader_Current_state;
  16. unsigned long MagneticReader_Last_state_Change = 0;
  17.  
  18. int MagneticReader_Last_state_isonMove_Millis_Gap = Wheel_circumference_in_Cm * 36 ; // the min for 1km/hour speed
  19. // isonMove
  20. boolean isonMove = false;
  21. // total distance and time
  22. unsigned long total_time_onMove = 0;
  23. unsigned long total_cm_onMove = 0;
  24. // for now leave this as indicator
  25. const int ledPin =  13;      // the number of the LED pin
  26. int tempcounter = 0;
  27.  
  28. void setup() {
  29.   // set serial
  30.   Serial.begin(115200);
  31.   // set up the LCD's number of columns and rows:
  32.   lcd.begin(16, 2);
  33.  
  34.   // set the fixed text
  35.   lcd.setCursor(0, 0);
  36.   lcd.print("SPEED:");
  37.   lcd.setCursor(0, 1);
  38.   lcd.print("TIME:");
  39.  
  40.   // initialize the LED pin as an output:
  41.   pinMode(ledPin, OUTPUT);
  42.   pinMode(MagneticReader_Pin, INPUT_PULLUP);
  43.  
  44.   MagneticReader_Last_state = digitalRead(MagneticReader_Pin);
  45. }
  46.  
  47. void loop() {
  48.   // read the state of the magnetic reader.
  49.   MagneticReader_Current_state = !digitalRead(MagneticReader_Pin);
  50.  
  51.   // if the last read was longer then 3 sec ... probably stop - show speed 0 (zero)
  52.   if (MagneticReader_Last_state_Change + MagneticReader_Last_state_isonMove_Millis_Gap < millis()) {
  53.     // set speed to ZERO For display
  54.     Speed = 0;
  55.     // set on the move to false
  56.     isonMove = false;
  57.   } // end if past time
  58.  
  59.   // set indicator // TO BE REMOVED (THIS ONLY FOR VISUAL DEBUG - BLIN KEVERY TIME THE WHEEL CROSS)
  60.   digitalWrite(ledPin, MagneticReader_Current_state);
  61.  
  62.   // if state changed
  63.   if (MagneticReader_Current_state != MagneticReader_Last_state)  {
  64.     // set the global var to use in all the functions to keep it on the same millis
  65.     millisValue = millis();
  66.  
  67.     // only when we turn HIGH we do the calc of speed and setting of the last chnage miliis
  68.     if (MagneticReader_Current_state)    {
  69.       // now only if we already on the move
  70.       if (isonMove) {
  71.  
  72.         // set the counter - will display for debugin TO BE REMOVED
  73.         tempcounter += 1;
  74.         Serial.println(tempcounter);
  75.  
  76.         // calc speed
  77.         Speed =  float(Wheel_circumference_in_Cm) / float((millisValue - MagneticReader_Last_state_Change) / 36) ;
  78.  
  79.         // Calculate totals :
  80.         total_time_onMove += millisValue - MagneticReader_Last_state_Change;
  81.         total_cm_onMove += Wheel_circumference_in_Cm;
  82.  
  83.         // if not is on move -
  84.       } else {
  85.         // flip satet
  86.         isonMove = true;
  87.       } // end  (isonMove)
  88.  
  89.       // set the time
  90.       MagneticReader_Last_state_Change = millisValue;
  91.     } // end if (MagneticReader_Current_state)
  92.  
  93.     // set the current to last - so this loop will not happen again untill it chnages again.
  94.     MagneticReader_Last_state = MagneticReader_Current_state;
  95.   } // end if state changed
  96.  
  97.   // print speed - if speed less then 10 print it align to right
  98.   lcd.setCursor(7, 0);
  99.   if (int(Speed) < 10) {
  100.     lcd.print(" ");  // if less then 10 add " " space - to remove prev carcters
  101.   }
  102.   lcd.print(int(Speed));
  103.  
  104.   // print total distance
  105.   lcd.setCursor(11, 0);
  106.   if (int(total_cm_onMove / 100000) < 10) {
  107.     lcd.print(" ");  // if less then 10 add " " space  so when its over 10 it will not move in "space"
  108.   }
  109.   lcd.print(float(float(total_cm_onMove) / 100000.00), 2);
  110.  
  111.   //calc and print time in MM:SS
  112.   int minutes_for_diplay = int(total_time_onMove / 60000);
  113.   int sec_for_display = int((total_time_onMove - minutes_for_diplay * 60000) / 1000);
  114.  
  115.   // print total time
  116.   lcd.setCursor(7, 1);
  117.   if (minutes_for_diplay < 10) {
  118.     lcd.print("0"); // if min less then 10 add 0 (zero)
  119.   }
  120.   lcd.print(minutes_for_diplay);
  121.   lcd.print(":");
  122.   if (sec_for_display < 10) {
  123.     lcd.print("0"); // if sec less then 10 add 0 (zero)
  124.   }
  125.   lcd.print(sec_for_display);
  126.  
  127.  
  128. } // end loop
  129.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement