Using timer0 and external interrupt is enough for this ?.
It's too much for this.
An ISR is a good idea, because even though the time between pulses is long, the pulses are short. But all you need to do to get the time is capture the value of millis() in the ISR.
Now I think I got it.(I am waiting for yours reply this is rite or wrong).
Circumference of the wheel= 2πr (where ‘r’ is in cm)
= 2×3.14×30
= 188.4 cm or 1.884 metres
Speed. Let’s assume that in 1 second the wheel
completes one revolution. In other words, in one second, the bike has covered 1.88 metres. Therefore the speed in km/hour:
N×1.88×3600/1000
= N×6.784 or N×6.8
where ‘N’ is the number of revolutions per second.
‘6.8’ is a constant and only ‘N’ varies; for example, if ‘N’
is 5, the speed equals 5×6.8= 34 km/hour.
I get this calculation from this link. this is correct ?.
Let's say you measure 875 milliseconds for a single revolution
speed = (170" / 63360) / (875milli / 3600000)
or
speed = 170 * 3600000 / (875 * 63360)
by my math, that results in 11mph (integer math with its rounding)
For KPH, use
1 kilometer = 39370 inches
speed = 170 * 3600000 / (875 * 39370)
speed = 17kph
Using floating point math, that would be 17.7blahblah kph
But representing the speed in an integer value is probably less confusing.
And the arduino likes integer math much more than floating point.
My math is always suspect. Check for errors. If found, mention it and we will all be better off.
Hope things got settled here.. I would just like to shower some more light on this topic.
I have already made a project in which the speed of a bi-cycle is calculated using Arduino and hall sensor.
The project can be found here.
I have used the ISR and millis() to calculate the time taken for one complete rotation of the wheel. Then using this time taken I have calculated the rpm of the wheel. Then finally after knowing the circumference of the wheel we can calculate its velocity in km/hr.
I have simplified the formulas and have listed them below...
/*Arduino Code for measuring speed of the Vechile using Hall Sensor
* Coded by Circuitdigest.com
* On 14-04-2017
*/
/*CONNECTION DETIALS
* Arduino D11 -> RX of BT Module
* Arduino D12 -> Tx of BT
* Arduino D2 -> Hall sensor 3rd pin
*/
#include <SoftwareSerial.h>// import the serial library
SoftwareSerial Cycle_BT(11, 12); // RX, TX
int ledpin=13; // led on D13 will show blink on / off
int BluetoothData; // the data given from Computer
float radius_of_wheel = 0.33; //Measure the radius of your wheel and enter it here
volatile byte rotation; // variale for interrupt fun must be volatile
float timetaken,rpm,dtime;
int v;
unsigned long pevtime;
void setup()
{
Cycle_BT.begin(9600); //start the Bluetooth communication at 9600 baudrate
//pinMode(ledpin,OUTPUT); //LED pin aoutput for debugging
attachInterrupt(0, magnet_detect, RISING); //secound pin of arduino used as interrupt and magnet_detect will be called for each interrupt
rotation = rpm = pevtime = 0; //Initialize all variable to zero
}
void loop()
{
/*To drop to zero if vehicle stopped*/
if(millis()-dtime>1500) //no magnet found for 1500ms
{
rpm= v = 0; // make rpm and velocity as zero
Cycle_BT.write(v);
dtime=millis();
}
v = radius_of_wheel * rpm * 0.37699; //0.33 is the radius of the wheel in meter
}
void magnet_detect() //Called whenever a magnet is detected
{
rotation++;
dtime=millis();
if(rotation>=2)
{
timetaken = millis()-pevtime; //time in millisec for two rotations
rpm=(1000/timetaken)*60; //formulae to calculate rpm
pevtime = millis();
rotation=0;
Cycle_BT.write(v);
//Cycle_BT.println("Magnet detected...."); //enable while testing the hardware
}
}