RPM meter problem

johnwasser:
OK. Here is a version that will show each step between the previously displayed RPM and the current RPM:

#include <SPI.h>

#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

const unsigned long MICROSECONDS_PER_HALF_MINUTE = 30 * 1000000UL;

const int RPM_GRANULARITY = 20;

const byte OLED_RESET_PIN = 4;

Adafruit_SSD1306 display(OLED_RESET_PIN);

#if (SSD1306_LCDHEIGHT != 32)
#error("Height incorrect, please fix Adafruit_SSD1306.h!");
#endif

volatile unsigned long MicrosecondsInHalfRevolution = 0;

void setup() {
  Serial.begin(9600);

attachInterrupt(0, HalfRevolutionISR, RISING); // the output from spark plug sensor

display.begin(SSD1306_SWITCHCAPVCC, 0x3C);  // initialize with the I2C addr 0x3C (for the 128x32)
}

void HalfRevolutionISR()
{
  static unsigned long lastMicroseconds = 0;
  unsigned long currentMicroseconds = micros();
  MicrosecondsInHalfRevolution = currentMicroseconds - lastMicroseconds;
  lastMicroseconds = currentMicroseconds;
}

void loop() {
  static unsigned int OldRPM = 0;

// Copy volatile varaibles while interrupts are disabled
  noInterrupts();
  unsigned long microsecondsHalfRev = MicrosecondsInHalfRevolution;
  interrupts();

// Calculate RPM
  unsigned int RPM = MICROSECONDS_PER_HALF_MINUTE / microsecondsHalfRev;

// Reduce the resolution to match the desired granularity.
  // Note that the division is done in integers
  RPM = (RPM / RPM_GRANULARITY) * RPM_GRANULARITY;

// Display only on change to avoid flicker
  if (RPM != OldRPM) {
    // Display each step of size RPM_GRANULARITY
    if (RPM > OldRPM)
      OldRPM += RPM_GRANULARITY;
    else
      OldRPM -= RPM_GRANULARITY;
    display.clearDisplay();
    drawPercentbar( 0, 16, 128, 15, map(RPM, 0, 8000, 0, 100));
    text(RPM);
    display.display();
  }
}

void text (unsigned int RPM) {
  display.setCursor (0, 0) ;
  display.setTextSize(2);
  display.setTextColor(WHITE);
  display.print (RPM) ;
  display.print (" RPM") ;
}

void drawPercentbar(int x, int y, int width, int height, int progress)
{

progress = constrain(progress, 0, 100);

float bar = ((float)(width - 4) / 100) * progress;

display.drawRect(x, y, width, height, WHITE);
  display.fillRect(x + 2, y + 2, bar , height - 4, WHITE);

if ( height >= 15) {
    display.setCursor((width / 2) - 3, y + 5 );
    display.setTextSize(1);
    display.setTextColor(WHITE);
    if ( progress >= 50)
      display.setTextColor(BLACK, WHITE);
    display.print(progress);
    display.print("%");
  }
}

johnwasser you save me!!! i 'll try the first code you post me. its brilliant and exactly that i want!!!!!
tomorrow i will try the second code you post before minutes. you save me and thank you very much for direct help and anwser!!!!! we can keep communication for my project??? thanks a lot for the help!!!! trully you are the best so far!!! :slight_smile: