Go Down

Topic: Using arduino and IR sensor to count RPM (Read 1 time) previous topic - next topic

Likayuu

Hello,

I have all the components and have written the code to be able to count the RPM using a IR sensor a LCD and Arduino. For some reason I can never get the correct RPM to be displayed. Below is my code. Any help would be greatly appreciated, first time coding so I'm pretty new at this. For my test set up I have a piece of reflective tape on a fan blade. The fan is black so it only picks up that piece of tape.

Code: [Select]
#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x27,16,2);

float value=0;
float rev=0;
int rpm;
int oldtime=0;
int time;

void isr() //interrupt service routine
{
rev++;
}

void setup()
{
lcd.init();     //initialize lcd
lcd.backlight();
attachInterrupt(0,isr,RISING); //attaching the interrupt

lcd.setCursor(3,0);
lcd.print("Tachometer");
delay(2000);

}

void loop()
{
delay(1000);               //update rpm every second
detachInterrupt(0);        //detaches the interrupt
time = millis()-oldtime;   //finds the time
rpm = (rev/time)*60000;    //calculates rpm for blades
oldtime = millis();        //saves the current time
rev=0;                     //resets the counter
lcd.clear();
lcd.setCursor(3,0);
lcd.print("TACHOMETER");
lcd.setCursor(0,1);
lcd.print("Speed: ");
lcd.setCursor(6,1);
lcd.print(rpm);
lcd.setCursor(13,1);
lcd.print("RPM");
lcd.print("  ");
attachInterrupt(0,isr,RISING);
}

Paul_KD7HB

I didn't examine your code closely, but have to wonder why you are using float for an RPM counting when you indicate only a single reflector on your device. You can only, ever, get an integer number of counts and an integer value for RPM, never a fraction of an RPM.

Furthermore, using a float entails multiple instructions because it is 4 bytes long.

Paul

Likayuu

I searched the web and formatted this code from several I saw. Again this is my first stab at coding and not to sure what to do. What should I use instead of a float. I saw somewhere about making  the variable volatile but not sure what that means

dougp

Again this is my first stab at coding and not to sure what to do.
First, bookmark this Arduino reference page.

I saw somewhere about making  the variable volatile but not sure what that means
Read about volatile variables on the page referenced above.
I don't trust atoms.  They make up everything.

No private consultations undertaken!

6v6gt

If the value you are getting is unexpectedly high, it could be that the IR sensor is delivering multiple pulses per detection. In that case you may have to add some sort of debouncing.

Go Up