Source code required..HELP!!!

I am working on a project which requires detecting speed of a motorbike and do execute some changes based on speed. The speed sensor gives 8 pulses per wheel revolution. I need to calculate the speed by detecting the pulses and display it on an LCD. I am using an LCD which uses I2C interface and it works fine. But I do not know how to: 1) Calculate the speed on the arduino and the display it on the LCD.
Please help me as I do not know much about coding or hardware. Please provide me a code that I can use to calculate the speed and
then displaying it on the LCD (one revolution of wheel gives 8 pulses and I can connect the speed sensor input to any pin on arduino).

what is outside diameter of tyre or revs/mile etc?

When you know the diameter of the tyre, you can use that to estimate how far the bike has travelled. Then you just divide that by elapsed time between the pulses and get m/s, km/h or mph or whatever you prefer. Eight pulses would equal the diameter of the tyre, 1 pulse would be 1/8th of the diameter in this case. Multiple pulses are good for accuracy. There could be some coding tips you could need for not missing any pulses. Maybe running it through interrupt service routines might help.

I'd like to know how to control an LCD too, but there's a probably a datasheet or a tutorial and a library somewhere, so I would start with that. Sorry, but I can't help with LCD's.

Edit: And of course I was thinking circumference when I was writing diameter somewhere up there.. circumference = PI * diameter. 8 pulses equals the circumference in travelled distance.

Some starter code

#include<lcd.h>

float numberofpulsesperrotation = 8.0;
float wheeldiameter = 1; // assume 1 meter BIG wheel
float wheelsize = 3.14159265 * wheeldiameter; // or 2* PI * radius)

void setup()
{
...
}

void loop()
{
  ...
  if (timetorefreshscreen())
  {
    float distance = numberofpulses * wheelsize / numberofpulsesperrotation;
    float speed = 60 * wheelsize * numberofpulsesinlastminute/numberofpulsesperrotation/1000; // KM/h
    lcd.print(distance);
    lcd.print(speed);
  }
}

bool timetorefreshscreen()
{
... 
  return true;
}

What code do you have sofar?

I do not have any code to get to the speed part. I have just opened programming textbooks!! I know nothing about coding as of now.
However I want to say a thing, which is, a few days back one of my friends did this coding for a PIC mC on the same bike using the same sensor. I dont have terms with him anymore. That guy didn't use diameter of the wheel.
The project was to actuate some solenoids once a certain bike speed is reached. All I know is he used some time period calculations to calculate and display the speed on the LCD and the best part was the speed which he got displayed on project LCD was same as the one being displayed on the bike manufacturer's LCD instrument cluster(its a 150 cc motor bike).
Now I need such a code which can give me the speed on my project lcd. As I had said, I could use any I/O pin on Arduino for speed detection.
I have the I2C LCD library etc and I can display anything on it from a sketch(I copy pasted the codes from net!).

edifiedsprit:
That guy didn't use diameter of the wheel.

That doesn't seem very likely.

Think about it. You get 8 pulses and know that the wheel has made one revolution and you know how long it took, but how far has the bike moved forward ? Without knowing that how can you work out the speed ? You have to know the circumference of the wheel either derived from the diameter or measured directly.

maybe he used 1/8 th of the circumference instead ....:wink:

You could do something simple like this for the counting of pulses, I think. Interrupts 0 and 1 link to pins 2 or 3 most of the time. Whenever it's time to estimate speed, you would reset pulse_count back to 0 and set another long variable to millis() for example.

const float CIRCUMFERENCE = 3.0F;
const long PULSES_PER_REV = 8;
volatile int pulse_count = 0;
long timems = 0;

// interrupt service routine
void addPulse()
{
  pulse_count++;
}

// Returns speed in distance unit per second. So, if circumference was given in meters, 
// this would give m/s, and you would multiply it by 3.6 to get speed in km/h and display that.
float speed()
{
  //assuming you won't keep the speedometer running for longer than one month in one go.
  long time_diff = millis() - timems;

  //That's too fast. Not resetting counters before returning so that the next measurement could still succeed. 
  //This could also return a negative number instead of 0.0F for failed measurements and you could ignore them.
  if (time_diff <= 0.0F)
    return 0.0F;

  float v = CIRCUMFERENCE * pulse_count * 1000.0F / ( time_diff * PULSES_PER_REV );

  //reset for next measurement
  pulse_count = 0;
  timems = millis();

  return v;
}

void setup()
{
  timems = millis();
  attachInterrupt(0, addPulse, RISING);
}

void loop()
{
// call speed() at sensible intervals and update the reading to LCD
...
}

I am assuming that the sensor sets the sensor HIGH based on pulses that occur 8 times per each revolution, so this is something I would start with, and then figure out how to report speed properly in case it gives funky readings in the real world.

Make sure it cannot update speed too often because otherwise this would just report 0 most of the time, because there may not be any pulses since the last measurement. This one includes the interrupt I mentioned earlier, but probably does not give you a stable reading.. So it needs something that calls the function speed() at regular intervals, add delay(1000) or some such. You can tune the delay based on how responsive you want the reading to be, but not too far. I added some safe-guards and comments to try and fix that at the last moment.

You can probably guess the speed from the frequency of the pulses and calibrating the reading to match the actual speed, but this is roughly the same thing and this at least is mathematically correct. The diameter or the circumference of the wheel still directly affects at what rate the pulses would occur, so the circumference and diameter are relevant one way or the other. And of course the air pressure inside the tire affects the pulse rate, and the readings, because the diameter is not always a constant if the tire is filled with air.

You cannot derive the speed from the frequency of the pulses.

If a wheel of 36 inches diameter rotates at the same rate as one of 24 inches diameter (or any other diameter for that matter) the pulse rate will be the same but the bike will be going at a different speed.

edifiedsprit:
I do not have any code to get to the speed part. I have just opened programming textbooks!! I know nothing about coding as of now.
However I want to say a thing, which is, a few days back one of my friends did this coding for a PIC mC on the same bike using the same sensor. I dont have terms with him anymore. That guy didn't use diameter of the wheel.
The project was to actuate some solenoids once a certain bike speed is reached. All I know is he used some time period calculations to calculate and display the speed on the LCD and the best part was the speed which he got displayed on project LCD was same as the one being displayed on the bike manufacturer's LCD instrument cluster(its a 150 cc motor bike).
Now I need such a code which can give me the speed on my project lcd. As I had said, I could use any I/O pin on Arduino for speed detection.
I have the I2C LCD library etc and I can display anything on it from a sketch(I copy pasted the codes from net!).

Sounds like you should first make amends with the person who you don't have terms with unless he's in hospital for turning on his NOS bottle with those solenoids a little too early by not properly calculating how fast he was really going

UKHeliBob:
You cannot derive the speed from the frequency of the pulses.

If a wheel of 36 inches diameter rotates at the same rate as one of 24 inches diameter (or any other diameter for that matter) the pulse rate will be the same but the bike will be going at a different speed.

That's where the calibration comes in but it has to be recalibrated if diameter changed. Never mind, I don't know what I was thinking. You'd need a reference to compare against. However, all one needs to do is to just calculate the speed using a known diameter like you wrote. That way you know exactly how far the bike has travelled between each pulse.