talofer99 icon

Bicycle speedometer

talofer99 | PRO | 10/28/20 07:17:41 PM UTC | 0 ⭐ | 2811 👁️ | Never ⏰ | []
C |

4.2 KB

|

None

|

0 👍

/

0 👎

// include the library code:
#include <LiquidCrystal.h>
 
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 10, 9, 8, 7);
// general place holder (for use in functions)
unsigned long millisValue = 0;
// speed value
float Speed = 0 ;
// wheel size in CM
const int Wheel_circumference_in_Cm = 210 ;  // (2PiR).
// Magnatic reader params
const int MagneticReader_Pin = 2;
boolean MagneticReader_Last_state;
boolean MagneticReader_Current_state;
unsigned long MagneticReader_Last_state_Change = 0;
 
int MagneticReader_Last_state_isonMove_Millis_Gap = Wheel_circumference_in_Cm * 36 ; // the min for 1km/hour speed
// isonMove
boolean isonMove = false;
// total distance and time
unsigned long total_time_onMove = 0;
unsigned long total_cm_onMove = 0;
// for now leave this as indicator
const int ledPin =  13;      // the number of the LED pin
int tempcounter = 0;
 
void setup() {
  // set serial
  Serial.begin(115200);
  // set up the LCD's number of columns and rows:
  lcd.begin(16, 2);
 
  // set the fixed text
  lcd.setCursor(0, 0);
  lcd.print("SPEED:");
  lcd.setCursor(0, 1);
  lcd.print("TIME:");
 
  // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);
  pinMode(MagneticReader_Pin, INPUT_PULLUP);
 
  MagneticReader_Last_state = digitalRead(MagneticReader_Pin);
}
 
void loop() {
  // read the state of the magnetic reader.
  MagneticReader_Current_state = !digitalRead(MagneticReader_Pin);
 
  // if the last read was longer then 3 sec ... probably stop - show speed 0 (zero)
  if (MagneticReader_Last_state_Change + MagneticReader_Last_state_isonMove_Millis_Gap < millis()) {
    // set speed to ZERO For display
    Speed = 0;
    // set on the move to false
    isonMove = false;
  } // end if past time
 
  // set indicator // TO BE REMOVED (THIS ONLY FOR VISUAL DEBUG - BLIN KEVERY TIME THE WHEEL CROSS)
  digitalWrite(ledPin, MagneticReader_Current_state);
 
  // if state changed
  if (MagneticReader_Current_state != MagneticReader_Last_state)  {
    // set the global var to use in all the functions to keep it on the same millis
    millisValue = millis();
 
    // only when we turn HIGH we do the calc of speed and setting of the last chnage miliis
    if (MagneticReader_Current_state)    {
      // now only if we already on the move
      if (isonMove) {
 
        // set the counter - will display for debugin TO BE REMOVED
        tempcounter += 1;
        Serial.println(tempcounter);
 
        // calc speed
        Speed =  float(Wheel_circumference_in_Cm) / float((millisValue - MagneticReader_Last_state_Change) / 36) ;
 
        // Calculate totals :
        total_time_onMove += millisValue - MagneticReader_Last_state_Change;
        total_cm_onMove += Wheel_circumference_in_Cm;
 
        // if not is on move -
      } else {
        // flip satet
        isonMove = true;
      } // end  (isonMove)
 
      // set the time
      MagneticReader_Last_state_Change = millisValue;
    } // end if (MagneticReader_Current_state)
 
    // set the current to last - so this loop will not happen again untill it chnages again.
    MagneticReader_Last_state = MagneticReader_Current_state;
  } // end if state changed
 
  // print speed - if speed less then 10 print it align to right
  lcd.setCursor(7, 0);
  if (int(Speed) < 10) {
    lcd.print(" ");  // if less then 10 add " " space - to remove prev carcters
  }
  lcd.print(int(Speed));
 
  // print total distance
  lcd.setCursor(11, 0);
  if (int(total_cm_onMove / 100000) < 10) {
    lcd.print(" ");  // if less then 10 add " " space  so when its over 10 it will not move in "space"
  }
  lcd.print(float(float(total_cm_onMove) / 100000.00), 2);
 
  //calc and print time in MM:SS
  int minutes_for_diplay = int(total_time_onMove / 60000);
  int sec_for_display = int((total_time_onMove - minutes_for_diplay * 60000) / 1000);
 
  // print total time
  lcd.setCursor(7, 1);
  if (minutes_for_diplay < 10) {
    lcd.print("0"); // if min less then 10 add 0 (zero)
  }
  lcd.print(minutes_for_diplay);
  lcd.print(":");
  if (sec_for_display < 10) {
    lcd.print("0"); // if sec less then 10 add 0 (zero)
  }
  lcd.print(sec_for_display);
 
 
} // end loop
 

Comments