Hi all
i've recently bought this Arduino UNO compatible module because i'd like to build a simple RPM counter: Imgur: The magic of the Internet It has 3 pins: GND, OUT, +5V.
I did some research on the internet but i don't understand how does it work. Do the output float from an Higher value from a Lower value when an obstacle passes and deny the IR beam? Is it right to connect it like if was a led? ("int ledPin = 13;")
Right now on the display i only see random numbers, that change every loop although no obstacle passes.
This is my actual circuit: Imgur: The magic of the Internet
This is the code:
// include the library code:
#include <LiquidCrystal.h>
int ledPin = 13; // IR LED connected to digital pin 13
volatile byte rpmcount;
unsigned int rpm;
unsigned long timeold;
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
void rpm_fun(){
//Each rotation, this interrupt function is run twice, so take that into consideration for
//calculating RPM
//Update count
rpmcount++;
}
void setup() {
lcd.begin(16, 2);// set up the LCD's number of columns and rows:
//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, FALLING);
//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);
//Note that this would be 60*1000/(millis() - timeold)*rpmcount if the interrupt
//happened once per revolution instead of twice. Other multiples could be used
//for multi-bladed propellers or fans
rpm = 60*1000/(millis() - timeold)*rpmcount;
timeold = millis();
rpmcount = 0;
//Print out result to lcd
lcd.clear();
lcd.print("RPM: ");
lcd.print(rpm);
//Restart the interrupt processing
attachInterrupt(0, rpm_fun, FALLING);
}