Finished Speedometer code! Have a look!

After a few days of testing and debugging and modifying I have finally fixed and improved Onions code which he gave me to measure all sorts of things on a bike.

Here are the features:
Shows RPM
Shows Speed in km/h
Shows Distance traveled in meters
Shows max speed attained in km/h
Shows average speed (not working yet)

The code runs on a 16x4 LCD and displays all the data on the screen at once. The code can be modified to use a 16x2 LCD screen also.
The sensor is a reed switch which completes the circuit when a magnet is held over it thus sending a HIGH input on the digital pin.
There must be a pulldown resistor on the sensor so it will go back down to LOW when the reed switch is open.

At the top you can change your wheel circumference to match your bike’s and it’s ready to go.
It doesn’t matter where you mount your magnet on the wheel either towards center or farther to the edge but it is good to test out yourself and see how your reed switch reacts with your specific magnet.

I will be making a shield for the LCD and sensor + pull-down circuit so it will be portable.

Here is the code (finally) any questions? Just ask.

#include <LiquidCrystal440.h>

float rotationTime;  
float distanceTravelledPerSecond; 
float wheelCircumference = 50;  // Circumference of the wheel
float RPM;  
float speeds;  
float maximumSpeed; 
unsigned int distanceTravelled;  
//unsigned int averageSpeed;  // not needed for now
unsigned long revoloutions = 0;  .
unsigned long startRotation = 0;  
unsigned long endRotation = 0;  
unsigned long timeInSeconds;  
unsigned long timeInMilliseconds;  
unsigned long lastMillis;  
boolean reedSwitchState;  
boolean lastReedSwitchState; 
const int reedSwitchPin = 5;  
 
 int buttonState;
int val;
int val2;

LiquidCrystal lcd(7,8,9,10,11,12);

void DoTimeAndDistanceCalcs()
{
    revoloutions ++;  
    endRotation = millis();  
    rotationTime = endRotation - startRotation;  
    startRotation = endRotation;  
    RPM = (60000 / rotationTime);  
    distanceTravelled = revoloutions * wheelCircumference / 100;  
    distanceTravelledPerSecond = RPM * wheelCircumference;  
    distanceTravelledPerSecond = distanceTravelledPerSecond / 100000 * 60; 
    speeds = distanceTravelledPerSecond;  
    if(speeds > maximumSpeed) 
      maximumSpeed = speeds;  
    timeInSeconds = millis();  
    timeInSeconds = timeInMilliseconds / 1000;  
    //averageSpeed = (distanceTravelled / timeInSeconds);  //not needed for now
}

void setup(){
  pinMode(reedSwitchPin, INPUT);  
  Serial.begin(9600);
  buttonState = digitalRead(reedSwitchPin);
  lcd.begin(16,4);
  lcd.setCursor(0,0);
  lcd.print("Dst");
  lcd.setCursor(8,0);
  lcd.print("Spd");
  lcd.setCursor(0,2);
  lcd.print("Max");
  lcd.setCursor(8,2);
  lcd.print("RPM");
}
void loop(){

  val = digitalRead(reedSwitchPin);
  delay(10);
  val2 = digitalRead(reedSwitchPin);
  
  if (val == val2) {
     if (val != buttonState) {
lastReedSwitchState = reedSwitchState;  
reedSwitchState = val;  
  

  if((lastReedSwitchState == LOW) && (reedSwitchState == HIGH)){  
    DoTimeAndDistanceCalcs();

    lcd.setCursor(8,3);
    lcd.print(RPM);  
    lcd.setCursor(0,1);
    lcd.print(distanceTravelled);  
    lcd.setCursor(8,1);
    lcd.print(speeds);  
    lcd.setCursor(0,3);
    lcd.print(maximumSpeed);  
    // lcd.setCursor(8,3);
     //lcd.print(averageSpeed);   //not needed for now

  }    }
  buttonState = val ; 
  //Serial.println(digitalRead(reedSwitchPin));  testing purposes
 }}

You might get more replies on another section of the forum as "Programming Questions" is for questions not finished code. Just a thought :)

any questions?

Any reason for not using ctrl-T before posting?

Well done on finishing the code! It looks like a good program, and you got it to work, which is better than can be said for my version!

Well done! Onions.

AWOL:

any questions?

Any reason for not using ctrl-T before posting?

What does it do? Opens a new tab?

Kari

I read a similar Awol post this morning and discovered that it invokes an auto code formatter. I have been too lazy to check whether I can configure it to make it format my code in the (25 year old) style I prefer.

Silly me, I though CTRL-T while composing the post, before sending it... :blush:

Cheers, Kari