/*
* Optical Tachometer
*
* Uses an IR LED and IR phototransistor to implement an optical tachometer.
* The IR LED is connected to pin 13 and ran continually.
* Pin 2 (interrupt 0) is connected across the IR detector.
*
*/
int ledPin = 13; // IR LED connected to digital pin 13
volatile byte rpmcount;
unsigned int rpm;
unsigned long timeold;
// include the library code:
#include <LiquidCrystal.h>
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(7, 8, 9, 10, 11, 12);
void rpm_fun()
{
rpmcount++;
}
void setup()
{
lcd.begin(16, 2); // intialise the LCD
//Interrupt 0 is digital pin 2, so that is where the IR detector is connected
//Triggers on FALLING (change from HIGH to LOW)
attachInterrupt(0, rpm_fun, RISING);
//Turn on IR LED
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, HIGH);
rpmcount = 0;
rpm = 0;
timeold = 0;
}
void loop()
{
//Update RPM every second
delay(1000);
//Don't process interrupts during calculations
detachInterrupt(0);
rpm = 60*1000/(millis() - timeold)*rpmcount;
timeold = millis();
rpmcount = 0;
lcd.clear();
lcd.print("RPM=");
lcd.print(rpm);
//Restart the interrupt processing
attachInterrupt(0, rpm_fun, RISING);
}
The only thing I don't understand is the math used.
In other words, I don't understand this line: rpm = 60*1000/(millis() - timeold)*rpmcount;
Can someone explain to me the math being used? And if possible, can someone show me a way to improve the code in order to switch the display from rpm's to km/h ? That would be awesome thanks a lot !
Can someone explain to me the math being used? And if possible, can someone show me a way to improve the code in order to switch the display from rpm's to km/h ? That would be awesome thanks a lot ! smiley-sweat
Sure, no problem .
60*1000=60000
(millis() - timeold) means the number of milliseconds since the processor started running the current program MINUS the number of milliseconds saved in the variable timeold. (we can assume from the fact that in order to yield a positive result
(since there is no such thing as negative TIME) that millis() (NOW) has a value > timeold (which we can deduce cleverly from it's name that it was the value of millis() at some time in the PAST.
Let Millis =10000
Let timeold = 5000
Let rpmcount=5000
Then:
rpm = 60*1000/(millis() - timeold)rpmcount=[60000/(10000-5000)]=60000/5000=12rpmcount=60000
Since we don't know what rpmcount represents (a wheel or whatever having a diameter of whatever), we must conclude that
there is INSUFFICIENT DATA to convert this value into anything representing a speed such as mph or km/h.
millis()
Description
Returns the number of milliseconds since the Arduino board began running the current program. This number will overflow (go back to zero), after approximately 50 days.
smeagol109:
And if possible, can someone show me a way to improve the code in order to switch the display from rpm's to km/h ?
Speed = RPM * wheel circumference or speed = RPM * Pi * wheel diameter. That will give you inches/minute, which can easily be converted into MPH.
Divide by inches in a mile (63360) and multiply by minutes in an hour (60) or just divide by 1056 (60/63360 = 1/1056)
smeagol109:
And if possible, can someone show me a way to improve the code in order to switch the display from rpm's to km/h ?
Speed = RPM * wheel circumference or speed = RPM * Pi * wheel diameter. That will give you inches/minute, which can easily be converted into MPH.
Divide by inches in a mile (63360) and multiply by minutes in an hour (60) or just divide by 1056 (60/63360 = 1/1056)
That is, of course, assuming the OP has measured the wheel in inches. If they have the size of the wheel in cm, then the values will be different. Simpler then to convert to km/h as they're the same kind of unit.
RPM * circumference (in cm) = cm/m. Multiply by 60 for cm/h. Divide by 100,000 for km/h. Multiply by 1.609344 to convert km/h into mph.
majenko:
That is, of course, assuming the OP has measured the wheel in inches. If they have the size of the wheel in cm, then the values will be different. Simpler then to convert to km/h as they're the same kind of unit.
RPM * circumference (in cm) = cm/m. Multiply by 60 for cm/h. Divide by 100,000 for km/h. Multiply by 1.609344 to convert km/h into mph.
Agreed. As the OP has given no location in his ID, I had assumed (maybe wrongly) he's in the US or UK. I do wish that people would give some sort of location in their ID. A country would be nice, a town or city would be even better.