My first attempt at something useful.

I have been reading the threads about the speedo/bike computers and I decided I would try my self. This is very amateur and terrible. If anyone has a minute to leave some feedback, that would be excellent. For many reasons I expect that this wouldn't really work. However, it compiled and it's running on my Arduino using a switch for a trigger. Results seem to be consistent. Probably not accurate. But hey, a guy has to start somewhere.

/*
RPM and Speed via reed switch.
Based on state change example and blink without delay.
 http://arduino.cc/en/Tutorial/ButtonStateChange
 http://www.arduino.cc/en/Tutorial/BlinkWithoutDelay
This code is junk.
This really needs to trigger an interupt. That is beyond me currently.
You have been warned.
Reed switch may require hardware debouncing.
*/

// Constants.
const int switchPin = 8;        // Set the pin used for reed switch.
const float circ = 69.12;       // Circumference of wheel = DIAM * PI in inches

// Variables.
int switchCounter = 0;              // Rotation counter.
int switchState = 0;                // State of reed switch.
int lastSwitchState = 0;            //last state of switch.
unsigned long lastMillis = 0;       // Last report HIGH
unsigned long currentMillis = 0;    // Current report HIGH         
unsigned long difMillis = 0;        // Difference
float RPM = 0;                      // RPM in Float.
float MPH = 0;                      // Miles Per Hour.


void setup() {
  pinMode(switchPin, INPUT);
  Serial.begin(115200);
}

// Enter loop.

void loop() {

  switchState = digitalRead(switchPin);
  
  if (switchState != lastSwitchState) {
   
    if (switchState == HIGH) {
    switchCounter++;
    currentMillis = millis();
    difMillis = currentMillis - lastMillis;
    RPM = 1000 / difMillis * 60;
    MPH = RPM * circ / 12 / 5280 * 60;

/*    Serial.print("switchCounter  ");
    Serial.println(switchCounter);
    Serial.println(currentMillis);
    Serial.println(lastMillis);
    Serial.println(difMillis);
*/
    
    Serial.print("RPM  ");
    Serial.println(RPM);
    Serial.print("MPH  ");
    Serial.println(MPH);
  }
 else {
   Serial.println("OFF");
  }
}

lastSwitchState = switchState;
lastMillis = currentMillis;

}

Those equations where bad. I changed them a little and I still get results. I'll call that a plus for now. Also changed millis() to micros(). Thanks to the other speedo thread I see plenty that can be better. This is it for now. It's still ugly but a bit better. Back to work for me. Whoops looks like that counter needs to be unsigned also. Correct?

/*
RPM and Speed via reed switch.
Based on state change example and blink withought delay.
 http://arduino.cc/en/Tutorial/ButtonStateChange
 http://www.arduino.cc/en/Tutorial/BlinkWithoutDelay
This code is junk.
This really needs to trigger an interupt. That is beyond me currently.
You have been warned.
Reed switch may require hardware debouncing.
*/

// Constants.
const int switchPin = 8;        // Set the pin used for reed switch.
const float circ = 69.12;       // Circumference of wheel = DIAM * PI in inches

// Variables.
unsigned int switchCounter = 0;     // Rotation counter.
int switchState = 0;                // State of reed switch.
int lastSwitchState = 0;            //last state of switch.
unsigned long lastMicros = 0;       // Last report HIGH
unsigned long currentMicros = 0;    // Current report HIGH         
unsigned long difMicros = 0;        // Difference
float RPM = 0;                      // RPM in Float.
float MPH = 0;                      // Miles Per Hour.


void setup() {
  pinMode(switchPin, INPUT);
  Serial.begin(115200);
}

// Enter loop.

void loop() {

  switchState = digitalRead(switchPin);
  
  if (switchState != lastSwitchState) {
   
    if (switchState == HIGH) {
    switchCounter++;
    currentMicros = micros();
    difMicros = currentMicros - lastMicros;
    RPM = 60000000.0 / difMicros;
    MPH = (((RPM * circ) / 12) / 5280) * 60;
/*    Serial.print("switchCounter  ");
    Serial.println(switchCounter);
    Serial.println(currentMicros);
    Serial.println(lastMicros);
    Serial.println(difMicros);
*/    
    Serial.print("RPM  ");
    Serial.println(RPM);
    Serial.print("MPH  ");
    Serial.println(MPH);
  }
 else {
   Serial.println("OFF");
  }
}

lastSwitchState = switchState;
lastMicros = currentMicros;

}

You are relying on polling to catch every switch change. Depending on the diameter of the wheel, and the rotational speed, and what else the sketch is doing, that might, or might not, happen.

Specifically, Serial.print() statement really slow the code down. So does floating point math.

But, if the code works for speeds up to what you will routinely encounter, then there is no need to change anything.

Of course, I wonder how you are going to Serial.print() anything when the Arduino is actually installed on the bicycle. Planning to carry a laptop along on all your rides?

LOL no laptops on my bicycle no. This is more of a theoretical exercise. A building block if you will. I don't really need the serial coms at all. It's just there so I can see if the output looks sane. Ultimately, I would like to have the ability for at least 6000 RPM.

My way over the top goal is to use the arduino to run a couple of low pressure batch-fire High-Z fuel injectors. I can lift the throttle body out of a junk yard from any late 80s - mid 90s GM V8 for my old car.

I have already installed a MegaSquirt in one car and sold it. I am seeing if I can do it for less than 2 grand this time.

Do you think that the 328P would be fast enough to run the required fuel calcs and fire the injectors? Spark is handled separately by a distributor that has been converted to CDI. So no minute angel calcs have to be done. However, the required fuel calc will have to be accurate and somewhat timely. I intended to use speed density calculation. That means I need RPM, Throttle Position, Intake Pressure, and Intake Air Temp at the very minimum. Land speed isn't strictly required.

If I can get that working up to an acceptable RPM, I had delusions of adding another AVR for spark spark control. That will have to be quite fast and accurate. It's a good thing that this is a project car. Because I am as green as it gets with C code. Like I said, this was the first attempt by me to do anything other than flash a LED.

I know this really needs to trigger an interrupt so I can do other things and there are ways of speeding up the operation here by some degree. So I was attempting to understand a much more simple set of concepts before I got too crazy.

Thank you for looking at it though. To tell you the truth Mr. PaulS, you have helped me understand this more than you know already. Your postings on various other subjects here has helped me understand a great deal. So thank you again.