Arduino speedometer for my motorcycle

Hello everyone!

I am here with one programming problem and I am new to this but i am learning with this project. I am writing a program for my speedometer for my yamaha, because my old one broke.

I have written the code that shows me my current speed, but i cannot get something to work and this is to set my speedometer back to zero if there is no signal detection in one or half a second. My values stay at the last known value except if sensors catches all 8 signals in more than 2 seconds.

this is my code:

#include <LiquidCrystal.h>



int in = A0;
long tc1;
long tc2=0;
long tcp=0;
int lastSensorState;
int sensorState;
int sensorChangeCounter;
int i;
long hitrost;
LiquidCrystal lcd(12,11,5,4,3,2);



void setup() {
  pinMode(in,INPUT);
  Serial.begin(9600);
  LiquidCrystal lcd(12,11,5,4,3,2);
  lcd.begin(16, 2);

  lcd.print("Yamaha XT125X");
if (hitrost > 0){lcd.clear();}

}

void loop() {
  sensorState = analogRead(in);
  unsigned long time1 = millis();
   
  if(sensorState < 75){i = 0;}
  if(sensorState > 75){i = 1;}
  
  if(i != lastSensorState) {
    lastSensorState = i;
    
    if(i == 0){
      sensorChangeCounter++;
      
      }
    }
  
  if(sensorChangeCounter < 1){
    tc1=time1;
  }
  if(sensorChangeCounter > 7){
    tc2 = time1;
  }
  
  tcp=tc2-tc1;
  
  if(sensorChangeCounter > 7){
    lcd.clear();
    hitrost = (135.654/tcp *3600/100);
    lcd.print(hitrost);
    lcd.print(" km/h ");
   sensorChangeCounter=0;
  }
}

There is 8 signals that comes to arduino from my magnetic sensor. 8 signals is for one full rotate on my wheel.

Thanks for helping me.

Have a nice day

if(tcp > 500){//try 1000 if too fast hitrost = 0; } else { hitrost = (135.654/tcp *3600/100); }

????

Hey!

No its not good. Because i would need reading of time on one signal to do that. Because i can drive 5km/h and if i stop immedietly it won't detect all 8 signals. so it won't do the if statemant.

Thanks anyway :D :)

mm eight sensor change states must happen before you change the display

  if(sensorChangeCounter > 7){
    lcd.clear();
    hitrost = (135.654/tcp *3600/100);
    lcd.print(hitrost);
    lcd.print(" km/h ");
   sensorChangeCounter=0;
  }
}

if it took too long for the sensor to change state update the display?

#include <LiquidCrystal.h>



int in = A0;
long tc1;
long tc2=0;
long tcp=0;
long st1=0;
long st2=0;
long stp=0;
int lastSensorState;
int sensorState;
int sensorChangeCounter;
int i;
long hitrost;
LiquidCrystal lcd(12,11,5,4,3,2);



void setup() {
  pinMode(in,INPUT);
  Serial.begin(9600);
  LiquidCrystal lcd(12,11,5,4,3,2);
  lcd.begin(16, 2);

  lcd.print("Yamaha XT125X");
if (hitrost > 0){lcd.clear();}

}

void loop() {
  sensorState = analogRead(in);
  unsigned long time1 = millis();
   
  //record sensor change state time
  if(sensorState < 75){i = 0;st1=time1;}
  if(sensorState > 75){i = 1;st2=time2;}
  
  if(i != lastSensorState) {
    lastSensorState = i;
    
    if(i == 0){
      sensorChangeCounter++;
      
      }
    }
  
  if(sensorChangeCounter < 1){
    tc1=time1;
  }
  if(sensorChangeCounter > 7){
    tc2 = time1;
  }
  
  tcp=tc2-tc1;
  //compute time it took for sensor to change state
  stp=st2-st1;
  
  if(sensorChangeCounter > 7 || abs(stp) > 500){
    lcd.clear();
    hitrost = (135.654/tcp *3600/100);
    lcd.print(hitrost);
    lcd.print(" km/h ");
   sensorChangeCounter=0;
  }
}

kavkec:
Hey!

No its not good. Because i would need reading of time on one signal to do that. Because i can drive 5km/h and if i stop immedietly it won’t detect all 8 signals. so it won’t do the if statemant.

Thanks anyway :smiley: :slight_smile:

Why do you need 8 signals?

What if you only get 7 signals? or 6? or…

    hitrost = (135.654/tcp *3600/100);

What is that number 135.654 ? Is that your wheel circumference in centimeters?

If it is, then here is what I figure. You have 8 sensors around the wheel. Assuming that they are spaced equally around the wheel, that means that each sensor stands for 16.957 centimeters, more or less.

1 km/h = (100000 cm)/(3600 s) = 27.778 cm/s = 1.63813 sensors/second

1.63813 sensors/second = (1.63813 sensors)/(1000 ms) = (1 sensor)/(610.45 ms)

It seems that you could just count the number of sensors in 610 milliseconds, and display that number as km/h. Or else count the number of sensors in 2 seconds and multiply by 0.305225 to get km/h.

odometer: Assuming that they are spaced equally around the wheel, that means that each sensor stands for 16.957 centimeters, more or less.

Unfurtanely it is not equall. They spaced like this -|-|-| (where (-) means small space and (|) means bigger space. So i could just substract two times and i did that now.

I have everything ready for speedometer to show speed. But i cant figure out how to get that 0 km/h if there is no sensor state changing in half a second.

I like the idea of codingKnight and i will try it ASAP.

Two thoughts come to mind:

  • Can you reduce the number of magnets from eight to four on the current wheel? That way you could have them equally spaced and can begin calculating your speed after only two samples.
  • If #1 is not feasible on the current wheel, how about the other wheel? or the rear sprocket, or???

kavkec: Unfurtanely it is not equall. They spaced like this -|-|-| (where (-) means small space and (|) means bigger space. So i could just substract two times and i did that now.

Well, my idea will still work, just not perfectly. (The speed shown will sometimes be 1 km/h lower or 1 km/h higher than the correct speed.)

I have everything ready for speedometer to show speed. But i cant figure out how to get that 0 km/h if there is no sensor state changing in half a second.

Perhaps you should combine the two approaches.

Count milliseconds, and also count magnets. Keep counting until you reach either 610 milliseconds or 8 magnets.

If you reach 8 magnets first, then use your method for calculating speed, and display the speed. If you reach 610 milliseconds first, then use my method for calculating speed, and display the speed.

Once you display the speed, start counting again from 0 milliseconds and 0 magnets.