 # Speedometer calculation

The Radius of the front wheel is 30cm so i calculated the circumference 2pieR to have it as 188.4 cm or 1.884 meters means in 1second it will cover a distance of 1.884 meters and the speed would be calculated by multiplying number of revolutions in 1 sec with 1.884 x 3600/100 thus the equation becomes as follows:

N × 6.784 or N × 6.8

SO I WANT TO KNOW THE WAY IN CODE TO HAVE THE VALUE OF "N"?(i'm using a hall effect sensor or a reed switch)

AND,

also i want to use the same magnet that is used by the speedometer wire so how should i mount the hall sensor or reed switch near it?

This is a link to the image of speedometer assembly with the bikes front wheel http://winacro.com/images/stories/DSC_0044.JPG

ok i got that the number of times the reed switch or Hall sensor crosses the magnet it will get me the value of "N" now its left that where to mount this reed or hall nearby or in the speedometer console?

If the magnet is on the wheel , you must place the hall sensor in such a place that the magnet passes the hall sensor as close as possible (but not closer!).

The best way to count the pulses is to use interrupts - see reference section of this site.

Furthermore you could get more precission by attaching more magnets on the wheel as you will get more pulses per rotation. Use an unsigned long as counter to prevent quick overflow.

Also worth trying is to make a speedo meter of your front wheel and of your back wheel. You will see that they will differ in value, up to you what is the right value, the front the back or something in between?

Succes

Remember also that the further from the axle, the faster the magnet will be travelling, and so the shorter the pulse. This doesn't normally matter if you're using interrupts, but may make a difference if you're polling.

i wrote a program without interrupts but that is more or less half utilizing as its value will never be less than 6.8 (as bcz of this equation Nx6.8) and it will never increment like it does like 10-11-12-13-14-15km/hr and so on.

can you guys help me with an example and how will polling help?

The program i have till now goes like (N x 6.8) 1 x 6.8=6.8km/hr, 2x6.8= 13.6km/hr and so on but the increments should be like 1km/hr then 2km/hr then 3km/hr and so on

``````long prevoiusMillis = 0;
Const int inputPin = 8;//input from the Hall Sensor/reed switch
int lastButtonState = LOW;
long interval = 1000;  // the amount of interval lag wanted

void Setup() {
pinMode(inputPin, INPUT);
Serial.begin(19200);
}

void loop() {

int m = m++;
}

if(currentMillis - previousMillis > interval) {

previousMillis = currentMillis;
Compute();
m=0;
}

void Compute() {

float n = m * 6.8;
float kmh = n; //the final km/hr value
}
``````

But first, make sure it at least compiles.

And that Compute does something useful.

I actually wrote that code in a copy bcz i want to give you guys an idea that where im going so anyone of you can show me a correct way however the code is follows which is too much extent correct:

``````long previousMillis = 0;
const int inputPin = 8;//input from the Hall Sensor/reed switch
int lastButtonState = LOW;
long interval = 1000;  // the amount of interval lag wanted
int m;// variable to hold pulses

void Setup() {
pinMode(inputPin, INPUT);
Serial.begin(19200);
}

void loop() {

unsigned long currentMillis = millis();
m = m++;
}

if(currentMillis - previousMillis > interval) {

previousMillis = currentMillis;
float n = m * 6.8;
float kmh = n; //the final km/hr value
m=0;
}

}
``````

also its worth defining here that i replied a lot late bcz something was there that was causing a BIG BIG problem so i wasn't able to login in and according to me that went for more than 10hours the problem with arduino site started with DB errors and 404 errors then accessing the site after logging in was not possible!

Nishant: ``` void loop() {

unsigned long currentMillis = millis(); int reading = digitalRead(inputPin); while(reading != lastButtonState) {   m = m++; }

What is this code fragment intended to do? I don't see any way for reading or lastButtonState to change once you have entered the while loop.

``````int m;// variable to hold pulses
``````

One letter global variable names are not recommended. How does one associate m with pulse count. Now, pulseCnt or pulseCount would be easy to associate with pulse count.

Along with PeterH's question (which is probably the crux of your problem), I'd ask what the currentMillis variable is for. It is local, so it isn't accessible outside of loop().

Generally, if you want to create a speedometer, you separate the counting of pulses from the sensor from the use of pulse count. That is most easily done by connecting the sensor to pin 2 or 3, and defining an interrupt service routine to be called when the sensor is triggered. That ISR then just counts pulses (in a properly named volatile global variable).

The loop() function then just keeps track of time, and periodically checks how many pulses have occurred, computes the speed, and resets the counter (not necessarily in that order). How often to check depends on what type of counter you are using (byte, int, long) and how long it takes that counter to overflow (you do not want that to ever happen).

Yes you both are correct i just wanted to get my idea down on paper and that

``````]unsigned long currentMillis = millis();
m = m++;
``````

intends to increment the variable to give me number of revolutions in a second(interval value) for this equation kmh = n * 6.8

Nishant: ``` ]unsigned long currentMillis = millis(); int reading = digitalRead(inputPin); while(reading != lastButtonState) {   m = m++; ```

intends to increment the variable to give me number of revolutions in a second(interval value) for this equation kmh = n * 6.8

I don't think it does anything like that. In fact I can't make any sense of it at all, and the only thing I think it'll do is lock your sketch up as soon as you enter the while loop.

I'd expect to see a variable recording last time you calculated the speed, and the number of wheel sensor inputs since that time, and some logic to see whether it was due to do the next speed calculation. The speed calculation itself would consist of a bit of arithmetic to convert the wheel counter to a speed (and perhaps display it somewhere) and then note when the next calculation was due and zero the wheel counter.

Thnks peter for the input NOW im considering the approach by paulS to use interrupt that whenever the sensor clicks it activates an interrupt and then counts the pulses and the loop() does the timing work like take care of how many pulses were there in a second, lets see what next turns out.

Just as a quick aside, have you checked the drive ratio of the speedometer cable? It isn't necessarily one revolution of cable for one revolution of wheel.

As the speedometer cable will be removed and the console where the speedometer cable is inserted and screwed would be open i will FIX in THE HALL SENSOR and as the magnet moves the sensor must be flowing pulses, hold on for some time yesterday i already ordered sensors and other stuff so pretty soon i will test all this.

The main problem im facing is in the equation and not in the program i mean to say FOR EXAMPLE to get a speed of 1km/hr on the LCD screen i need N=0.1471 or N = 1/6.8(FOR THE EQUATION N X 6.8 (where n is the number of revolutions in one second)) so HOW THIS VALUE I CAN ATTAIN FROM A HALL SENSOR?