the project: monitoring rpm of bicycle wheel with a reed switch from a cycle computer, with one magnet on the wheel, then activating different outputs dependent on the rpm.
I have played around with some simple code and circuits and have managed to get a clean debounced signal from the reed switch to the arduino and then output that on another pin to an led. The trouble i'm having is writing the sketch to make the reed switch work as an rpm counter and divide the rpm range into four bands(slow, med, fast and v.fast) with each band corresponding to an on/off output pin. As im still at a fairly basic level of programming iv'e been looking for existing code to work with, i found this peice of code for an IR tachometer but have not had much luck in adapting it yet. any help is welcome.
/*
* 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. A status LED is connected
* to pin 12. Pin 2 (interrupt 0) is connected across the IR detector.
*
*
*/
int ledPin = 13; // IR LED connected to digital pin 13
int statusPin = 12; // LED connected to digital pin 12
volatile byte rpmcount;
volatile int status;
unsigned int rpm;
unsigned long timeold;
void rpm_fun()
{
//Each rotation, this interrupt function is run twice, so take that into consideration for
//calculating RPM
//Update count
rpmcount++;
//Toggle status LED
if (status == LOW) {
status = HIGH;
} else {
status = LOW;
}
digitalWrite(statusPin, status);
}
void setup()
{
Serial.begin(9600);
//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);
//Use statusPin to flash along with interrupts
pinMode(statusPin, OUTPUT);
rpmcount = 0;
rpm = 0;
timeold = 0;
status = LOW;
}
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 = 30*1000/(millis() - timeold)*rpmcount;
timeold = millis();
rpmcount = 0;
//Write it out to serial port
Serial.println(rpm,DEC);
//Restart the interrupt processing
attachInterrupt(0, rpm_fun, FALLING);
}