fixing the code of a Leonardo based tachometer!

Hi all, I'm new here. I'm not a native speaker, so pardon me for my English :frowning: .
Just received my Arduino Leonardo today :). I tried some basic calculations and stuff like blinking and lcd interfacing.
Its Interesting, I am now trying to make a tachometer with Leonardo. I'm following this tutuorial Arduino RPM Counter / Tachometer Code -.

I guess the board they've used and Leonardo differ, :frowning: (ie pin 3 is interrupt0 in leonard but pin2 is in the board here)
I tried hanging the pins on the program and reconnecting the circuit but it doesn't work =(


The sketch

This is the code, reposting it,

/*
 * 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.
 *
 * Code based on: www.instructables.com/id/Arduino-Based-Optical-Tachometer/
 * Coded by: arduinoprojects101.com
 */

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()
 {
   //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);  // 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, 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 = 30*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);
  }

Can anyone help me to fix the project?
Does anyone here have tried this project before? or know some other tachometer project that works with Leonardo.
Thank you!

What is really the problem?

The only difference between your project (with Leonardo) and the project from the page of your link is that you must connect the IR receiver in the pin 3 (instead of the pin 2).

the problem is the lcd's just showing up rpm = 0 foerver.! :cold_sweat:

I'm not sure it is causing your immediate problem, but detaching the interrupt during calculations is neither necessary not sensible. Where you want to read and then clear the rpm_count, you can suspend interrupts, copy rpm_count to a local temporary variable, set rpm_count to zero and then re-enable interrupts. There is an alternative approach to work out rate-of-change of a counter by saving the previous value and subtracting it from the new value to calculate the difference. Since this avoids having to reset the volatile data, and since the volatile data is contained in a single byte, the access becomes atomic which means you can safely leave interrupts running while your main code does these calculations. But the approach you're using of resetting the count to zero should work, if it's coded correctly. Are you sure the interrupt handler is being called at all? You may be looking at a simple electrical problem.

Are you sure that your IR LED and IR photo transistor are working as planned? You could test this with this code or something similar, take note I threw this together really quickly and have not tried to compile it so there could be an error.

int ledPin = 13;                // IR LED connected to digital pin 13

void setup(){
   attachInterrupt(0, test, FALLING);

   pinMode(ledPin, OUTPUT);
   digitalWrite(ledPin, HIGH);
}

void test(){
   digitalWrite(6 !digitalRead(6));
}

void loop(){}

Where you could attach a visible light LED to pin 6 and it would toggle every time the IR photo transistor is blocked from the IR LED.

You could also print something to your computer if you do not have a LED

The only difference between your project (with Leonardo) and the project from the page of your link is that you must connect the IR receiver in the pin 3 (instead of the pin 2).

What happend when you tried out the suggestion from @ luisilva ?

Just change the connection(wire) from pin 2 to pin3!

I suggest you read & study this page:

AnalysIR:

The only difference between your project (with Leonardo) and the project from the page of your link is that you must connect the IR receiver in the pin 3 (instead of the pin 2).

What happend when you tried out the suggestion from @ luisilva ?

Just change the connection(wire) from pin 2 to pin3!

I suggest you read & study this page:
attachInterrupt() - Arduino Reference

Yes, I found the information here.