Compteur RPM à affichage LCD (la revanche)

Posted on Sep 22 -

Fabrice, mon ami d’en face, a suggéré une épuration du code a base de ruse de renard.

C’est plus clean, y’a plus de calcul de modulo dans le loop, c’est mieux. Je mets a jour. J’ai commenté le code, ça devrait se lire.

A noter qu’on pourrait cabler le LCD différement pour libèrer la sortie digitale 1 et ainsi écrire la valeur en même temps sur le port série. J’ai essayé, ça marche…

 * Effet hall Tachometer LCD display
 * Uses hall effect sensor to implement a tachometer.
 * A status LED is connected to pin 8.
 * Pin 2 (interrupt 0) is connected across the hall effect sensor.

// include the library code:
#include ;

// initialize the lcd library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 1);

int statusPin = 8;             
// Status LED connected to digital pin 8

volatile byte rpmcount;
volatile int status;
unsigned int rpm;
unsigned long timeold;

void setup()
  // Serial.begin(9600);
   attachInterrupt(0, rpm_fun, RISING);
      //Use statusPin to flash along with interrupts
   pinMode(statusPin, OUTPUT);
      // set up the LCD’s number of rows and columns: 
  lcd.begin(16, 2);
      // Shift display 1 character to the left 
      // initial values
   rpmcount = 0;
   rpm = 0;

void loop()
   if (rpmcount >= 20) { 
     //Update RPM every 20 counts, increase this for better RPM resolution,
     //decrease for faster update
     //detach interrupt during calculation
    //Note that this would be 601000/(millis() - timeold)rpmcount if the interrupt
    //happened once per revolution instead of twice. Other multiples could be used
    //for multi-bladed propellers or fans
    //To keep the rpm value 4 digits long, I add 10000 to the rpm value and hide 
    //the first digit using lcd.scrollDisplayLeft() in setup()
    //Nice trick from sack ;) thx !
     rpm = 10000 + 151000/(millis() - timeold)rpmcount;
     timeold = millis();
     rpmcount = 0;
      //print rpm value on LCD
// Serial.println((rpm-10000),DEC);
      //detach interrupt during calculation
      attachInterrupt(0, rpm_fun, RISING);
 void rpm_fun()
   //Each rotation, this interrupt function is run twice
   //Toggle status LED       
   if (status == LOW) {
   status = HIGH;    } else {
 status = LOW;    }    digitalWrite(statusPin, status);  

Other Posts