Is my speedometer code look usable as is?

Building a digital dash for a motorcycle build.
Already have the Oil Temp, Water temp, Fuel Pressure, Fuel level, and everything else working, now looking to add just a simple speedometer. thinking this will work just fine. I will have a gps unit mounted, but want a simple speedo just in case it's raining and the gps won't be on.

/*Edward Wolf 
  December 11 2018
  motorcycle speedometer section of digital dash
  for 1986 holda gl1200ltd
  using magnet in rear disk and 
  proximity switch Pepperl Fuchs NBB1,5-8GM50-E2
*/
// (digital) pin 5 is input from proximity switch
int prox_sw = 5;
// set number of proximity sensor counts for RPM reading 
// higher improves accuracy

float prox_trips = 25.0;

void setup() {

  Serial.begin(9600);
  // make the prox_sw pin an input:
  pinMode(prox_sw, INPUT);
}


void loop() {
  // preallocate values for tach
  float prox_cnt = 1.0;
  float start = micros();
  bool on_state = false;
  // counting number of times the proximity switch is tripped
  // but without double counting during the same trip
  while(true){
    if (digitalRead(prox_sw)==0){
      if (on_state==false){
        on_state = true;
        prox_cnt+=1.0;
      }
    } else{
      on_state = false;
    } 
    
    if (prox_cnt>=prox_trips){
      break;
    }
  }
  
  // print information although way too much
  float end_time = micros();
  float time_passed = ((end_time-start)/1000000.0);
  Serial.print(" Time   : ");
  Serial.print(time_passed);
  Serial.println("s");
  float rpm_val = (prox_cnt/time_passed)*60.0;

  Serial.print(" Pulses : ");
  Serial.println(prox_cnt/time_passed);
  Serial.print(" RPSec  : ");
  Serial.println(prox_cnt);
  Serial.print(" RPMin  : ");    
  Serial.println(rpm_val);
//tire dia * pi = circumference
//26" * 3.1415 = 81.679" OR 6.8065' or 775.7291 revs Per Mile
// so wheel speed rpm / tire revs per mile = miles per minute * 60 MPH!!!
  Serial.print(" MPH    : ");
  Serial.println(rpm_val/775.7291*60, 1);
  
  Serial.println("----------------");  

  delay(1);      
}
// end of speedometer section - now onward

Is my speedometer code look usable as is?

What happens when you try it?

If it works you probably don't need help.

If it does not work then it will be much easier to help if you tell us in detail what it actually does and what you want it to do that is different.

...R

Yes, like Robin2 says, get up on the bike and drive, or make a testrun using an electrical drill turning at a resonable speed…

Oh, it seems to work just fine. I have it set up on my test bench with the magnet mounted on a motor shaft, output on a scope to verify the electrical property's and just using the serial monitor. I was just wondering if it all looked ok, or if I was missing something.

My only nit pick is why is prox_trips a float and not an integer since you are counting pulses - not sure on Arduino (getting a bit fuzzy and bit lazy today) but an integer is usually faster and better for memory use.

A valid question for the future since the code works now - might be does anyone see a limit or gotcha in the code - - one system that was sent to me used a plain integer to count cans - but in testing they had never got more than 32,768 cans down the line - when my crew started putting over 32,768 cans down the line the count went negative until it got to zero then started back up again - simple for me to diagnose without looking at the code but the developers were thrown for a loop - then even more amazed when I could figure out the proper can count - live and learn

Good luck

Always remember the limits of the tools You use, the variables.…..

According to what you have, it looks you will only be updating the speedometer once per 25 revolutions of the wheel. You say that it is a 26 inch diameter wheel. That works out to roughly one update every 170 feet. At, say, 23 mph, that is one update every 5 seconds. At 38 mph, that is one update every 3 seconds. I recommend that you update more frequently, maybe once every 10 revolutions rather than 25.

While waiting for the wheel to make the appropriate number of revolutions, your code is stuck in the while(true) {  ...  } part and can do nothing else. In particular, this means that all of your gauges will freeze unless the motorcycle is in motion. (I assume that you have one Arduino controlling everything.) I suggest that you modify your while(true) {  ...  } to "give up" after a few seconds with no wheel motion.

odometer:
maybe once every 10 revolutions rather than 25.

Why not every single revolution?

That's how I control the speed of a small DC motor running at 15,000 RPM,

...R

Robin2:
Why not every single revolution?

That's how I control the speed of a small DC motor running at 15,000 RPM,

...R

Glad I read this...
Showed me that my idea will NOT work at all!
I wanted something simple with low processing... ie: not using interrupts
When I tested it using different counts, I get different numbers as a result...
sigh... back to the drawing board!

Wolfie666:
I wanted something simple with low processing... ie: not using interrupts

Why not use interrupts?

This is the code I use for detecting the speed of my motor. It is not complicated.

...R

Robin2:
Why not use interrupts?

This is the code I use for detecting the speed of my motor. It is not complicated.

...R

I will start over, looking at the code you provided.
I was trying to keep it short and simple as I want to have...

Cycle through the various sensors I'm using and repeat...
and...
have a rotary switch to select individual sensors
IE:
cycle through...
Water Temp them
Oil Temp
Oil Pressure
Fuel Pressure
Fuel Level
O2 left bank
O2 right bank
Air temp

or

then just select one of the above only.
I have a setup sort of running now without the speedo and it does work fine.

I’m thinking that using a NEO-6M GPS Module would be the simplest way to go.
Just have to read a pin, add a formula and "print’ the result.

I use one now for UTC Time.

A NEO-6M GPS Module is an excellent piece of stuff if You are in a reasonable open landscape. When You are driving below and surrounded by large, maybe wet tries the speed measurement tends to drop a lot. Look at a topic"Speedindicator using GPS". I finished that project, with help from members, and uses it on some railways. Using a sensor in the wheel and some software will probably work better provided that water and climate is kept away from conducting wires.