Newbie programmin question to read RPM

Hello!

I'm newbie in Arduino and programming in C. I have tried to make a little project with a Nano and an OLED to my car.

The projects important things:

RPM signal from OBD (Hall pin/RPM pin at OBD connector)
+/GND from OBD connector
The voltmeter is in work in progress :slight_smile:

Function:

When have RPM (10 1/min) the OLED is display when not have the display is off.
10-2800 1/min ECO display with voltage.
2800-5000 1/min SPORT display with voltage.
5000-10000 M-Sport enabled blinking than M-sport mode display with shift lights.

The RPM signal taked to pin 2 and attach intterrupt to count it. I tryed (the code stolen from the web :S ) but the count is not take to backward only + + so it's only counting. Whats my mistake in the code?

And the second question,i want to take the last mode (the M-Sport with shift light) to enable anyway when entered this mode to 30sec than exit from this mode. How can i make this thing? :S Thank you very much for the helping (and sry for bad english :slight_smile: )

#include "U8glib.h"

#include "MicroLCD.h"

#include <Arduino.h>


volatile int rpmcount=0;

int rpm=0;

unsigned long lastmillis=0;

int mmodeactivated = 0;

int fordulatszam;

int inPin = 2;

int ledPin = 13;

int feszultseg = 14;



U8GLIB_SSD1306_128X64 u8g(U8G_I2C_OPT_NONE);

LCD_SSD1306 lcd;




void setup() 
{
  
pinMode(inPin, INPUT);

  // digitalRead(inPin, HIGH);      //Esetleg kivehető
  
attachInterrupt(0, rpm_fan , RISING);      //FALLING (Magasról alacsonyra)   RISING (Alacsonyról magasra)
}


void loop()


{
     
{
if (millis() - lastmillis == 1000)
{
 detachInterrupt(0);    //Disable interrupt when calculating
/* Convert frecuency to RPM, note: this works for one interruption per full rotation. For two interrups per full rotation use rpmcount * 30.*/
fordulatszam=rpmcount * 60;
 rpmcount = 0; // Restart the RPM counter
 lastmillis = millis(); // Uptade lasmillis
   }  
 if (10 <= fordulatszam)
  
{
  
u8g.sleepOff();
  
}
  
else
  
{
  
u8g.sleepOn();
  
}

  
if (fordulatszam >= 10 && fordulatszam <= 2800)
  {
    u8g.firstPage();
    do
    {
      u8g.drawLine(0, 20, 128, 20);
      u8g.setFont(u8g_font_helvB12);
      u8g.drawStr( 19, 13, "ECO MODE");
      u8g.drawFrame(5, 35, 30, 20);
      u8g.drawBox(9, 32, 5, 3 );
      u8g.drawBox(26, 32, 5, 3 );
      u8g.drawLine(9, 42, 12, 42);
      u8g.drawLine(26, 42, 30, 42);
      u8g.drawLine(28, 40, 28, 44);
      lcd.setFontSize(FONT_SIZE_XLARGE);
      lcd.setCursor(50, 5);
      lcd.print(feszultseg);
      u8g.drawStr( 100, 50, "V");
    }
    while ( u8g.nextPage() );
    ;
  }
  if (fordulatszam > 2800 && fordulatszam <= 5000)
  {
    u8g.firstPage();
    do
    {
      u8g.drawLine(0, 20, 128, 20);
      u8g.setFont(u8g_font_helvB12);
      u8g.drawStr( 9, 13, "SPORT MODE");
      u8g.drawFrame(5, 35, 30, 20);
      u8g.drawBox(9, 32, 5, 3 );
      u8g.drawBox(26, 32, 5, 3 );
      u8g.drawLine(9, 42, 12, 42);
      u8g.drawLine(26, 42, 30, 42);
      u8g.drawLine(28, 40, 28, 44);
      lcd.setFontSize(FONT_SIZE_XLARGE);
      lcd.setCursor(50, 5);
      lcd.print(feszultseg);
      u8g.drawStr( 100, 50, "V");
    }
    while ( u8g.nextPage() );
    ;
  }

  if (fordulatszam > 5000 && fordulatszam <= 10000)
  {
    if (mmodeactivated < 3) {
      u8g.firstPage();
      do
      {
        u8g.setFont(u8g_font_helvB12);
        u8g.drawStr( 25 , 15, "M-SPORT");
        u8g.drawStr( 40 , 37, "MODE");
        u8g.drawStr( 15 , 60, "ACTIVATED");
      }
      while (u8g.nextPage());
      delay(650);
      u8g.firstPage();
      do
      {
      }
      while (u8g.nextPage());
      delay(250);
      mmodeactivated = mmodeactivated + 1;
    }
    else if (mmodeactivated > 2)
    {
      u8g.firstPage();
      do
      {
        u8g.drawLine(0, 20, 128, 20);
        u8g.setFont(u8g_font_helvB12);
        u8g.drawStr( 0, 13, "M-SPORT MODE");
        if (fordulatszam >= 5000 && fordulatszam <= 5150)
        {
          u8g.drawBox ( 5 , 56 , 10 , 4 );
        }
        if (fordulatszam > 5150 && fordulatszam <= 5300)
        {
          u8g.drawBox ( 5 , 56 , 10 , 4 );
          u8g.drawBox ( 20 , 52 , 10 , 8);
        }
        if (fordulatszam > 5300 && fordulatszam <= 5450)
        {
          u8g.drawBox ( 5 , 56 , 10 , 4 );
          u8g.drawBox ( 20 , 52 , 10 , 8);
          u8g.drawBox ( 35 , 48 , 10 , 12);
        }
        if (fordulatszam > 5450 && fordulatszam <= 5650)
        {
          u8g.drawBox ( 5 , 56 , 10 , 4 );
          u8g.drawBox ( 20 , 52 , 10 , 8);
          u8g.drawBox ( 35 , 48 , 10 , 12);
          u8g.drawBox ( 50 , 42 , 10 , 16);
        }
        if (fordulatszam > 5650 && fordulatszam <= 5850)
        {
          u8g.drawBox ( 5 , 56 , 10 , 4 );
          u8g.drawBox ( 20 , 52 , 10 , 8);
          u8g.drawBox ( 35 , 48 , 10 , 12);
          u8g.drawBox ( 50 , 42 , 10 , 16);
          u8g.drawBox ( 65 , 38 , 10 , 20);
        }
        if (fordulatszam > 5850 && fordulatszam <= 6050)
        {
          u8g.drawBox ( 5 , 56 , 10 , 4 );
          u8g.drawBox ( 20 , 52 , 10 , 8);
          u8g.drawBox ( 35 , 48 , 10 , 12);
          u8g.drawBox ( 50 , 42 , 10 , 16);
          u8g.drawBox ( 65 , 38 , 10 , 20);
          u8g.drawBox ( 80 , 34 , 10 , 24);
        }
        if (fordulatszam > 6050 && fordulatszam <= 6250)
        {
          u8g.drawBox ( 5 , 56 , 10 , 4 );
          u8g.drawBox ( 20 , 52 , 10 , 8);
          u8g.drawBox ( 35 , 48 , 10 , 12);
          u8g.drawBox ( 50 , 44 , 10 , 16);
          u8g.drawBox ( 65 , 38 , 10 , 20);
          u8g.drawBox ( 80 , 34 , 10 , 24);
          u8g.drawBox ( 95 , 30 , 10 , 28);
        }
        if (fordulatszam > 6250 && fordulatszam <= 6500)
        {
          u8g.drawBox ( 5 , 56 , 10 , 4 );
          u8g.drawBox ( 20 , 52 , 10 , 8);
          u8g.drawBox ( 35 , 48 , 10 , 12);
          u8g.drawBox ( 50 , 44 , 10 , 16);
          u8g.drawBox ( 65 , 40 , 10 , 20);
          u8g.drawBox ( 80 , 36 , 10 , 24);
          u8g.drawBox ( 95 , 32 , 10 , 28);
          u8g.drawBox ( 110, 28 , 10 , 32);
        }
        if (fordulatszam > 6500 && fordulatszam <= 10000)
        {
          u8g.setFont(u8g_font_helvB12);
          u8g.drawStr( 0, 50, "|||||  S H I F T  |||||");
        }
      }
      while ( u8g.nextPage() );
    }


  }
attachInterrupt(0, rpm_fan, FALLING); //enable interrupt
void rpm_fan() 

{                         /* this code will be executed every time the interrupt 0 (pin2) gets low.*/
  
rpmcount++;

}

}

Surely your code does NOT look like that. Post you code using code tags, NOT quote tags. Before you re-post it, use Tools + Auto Format to properly indent it.

if (millis() - lastmillis == 1000)

Suppose that you miss a millisecond? What happens if the difference goes from 999 to 1001 (as it very likely)? Your code will then never execute the body of the statement.

if (millis() - lastmillis >= 1000)

Edited (sry for it :slight_smile: ) now in code. With execute arduino is says it's okay,and it's works with this millis. But this part of the code stolen from web. I'm not editted this part.

In real the code shows this:

If i push the gaspedal, the OLED shows firs the ECO mode. I wait ~ 10 sec on 3000 1/min the OLED shows the SPORT mode than the other mods,at the end it stays on M-Mode --SHIFT!-- and stopd. If i not push the pedal it's not go back to ECO mode. It stays on that mode where i disengaged the pedal.
So it's not work whit the rpm what i want.

void loop()


{
    
{

So much for using Tools + Auto Format...

Why does loop() have TWO open curly braces?

You have the interrupt handler function define INSIDE of loop(). That won't even compile, so talking about it working is nonsense.

You have far too much stuff going on with the interrupt handler detached. In fact, it should never have been detached. Disable interrupts, copy the necessary data, re-enable interrupts.

Sorry a bit bad english and hard to understand,but i try. So now i edited the code,remove the braces what you said. And take the interrupts to i think the right way. (Plus take the signal Rise and Fall to backward.)

#include "U8glib.h"

#include "MicroLCD.h"

#include <Arduino.h>

volatile int rpmcount = 0;

int rpm = 0;

unsigned long lastmillis = 0;

int mmodeactivated = 0;

int fordulatszam;

int inPin = 2;

int ledPin = 13;

int feszultseg = 14;

 

void rpm_fan_plus()

{                         /* this code will be executed every time the interrupt 0 (pin2) gets lower.*/

  rpmcount++;

}

 

void rpm_fan_minus()

{                         /* this code will be executed every time the interrupt 0 (pin2) gets higher.*/

  rpmcount--;

}

 

U8GLIB_SSD1306_128X64 u8g(U8G_I2C_OPT_NONE);

LCD_SSD1306 lcd;

 

void setup() {

  pinMode(inPin, INPUT);

  // digitalRead(inPin, HIGH);      //Esetleg kivehető

  attachInterrupt(digitalPinToInterrupt(inPin), rpm_fan_plus , RISING);      //FALLING (Magasról alacsonyra)   RISING (Alacsonyról magasra)

  attachInterrupt(digitalPinToInterrupt(inPin), rpm_fan_minus , FALLING);      //FALLING (Magasról alacsonyra)   RISING (Alacsonyról magasra)

}

void loop()

  if (millis() - lastmillis == 1000)

  {

    attachInterrupt(digitalPinToInterrupt(inPin), rpm_fan_plus , RISING);      //FALLING (Magasról alacsonyra)   RISING (Alacsonyról magasra)

    attachInterrupt(digitalPinToInterrupt(inPin), rpm_fan_minus , FALLING);      //FALLING (Magasról alacsonyra)   RISING (Alacsonyról magasra)

    detachInterrupt(digitalPinToInterrupt(inPin));    //Disable interrupt when calculating

    rpm = rpmcount;                           /* Convert frecuency to RPM, note: this works for one interruption per full rotation. For two interrups per full rotation use rpmcount * 30.*/

    lastmillis = millis(); // Uptade lasmillis

  }

  fordulatszam = rpmcount * 2;

  if (10 <= fordulatszam)

  {

  u8g.sleepOff();

  }

  else

  {

  u8g.sleepOn();

  }

 

  if (fordulatszam >= 10 && fordulatszam <= 2800)

  {

    u8g.firstPage();

    do

    {

      u8g.drawLine(0, 20, 128, 20);

      u8g.setFont(u8g_font_helvB12);

      u8g.drawStr( 19, 13, "ECO MODE");

      u8g.drawFrame(5, 35, 30, 20);

      u8g.drawBox(9, 32, 5, 3 );

      u8g.drawBox(26, 32, 5, 3 );

      u8g.drawLine(9, 42, 12, 42);

      u8g.drawLine(26, 42, 30, 42);

      u8g.drawLine(28, 40, 28, 44);

     lcd.setFontSize(FONT_SIZE_XLARGE);

      lcd.setCursor(50, 5);

      lcd.print(feszultseg);

      u8g.drawStr( 100, 50, "V");

    }

    while ( u8g.nextPage() );

  }

  if (fordulatszam > 2800 && fordulatszam <= 5000)

  {

    u8g.firstPage();

    do

    {

      u8g.drawLine(0, 20, 128, 20);

      u8g.setFont(u8g_font_helvB12);

      u8g.drawStr( 9, 13, "SPORT MODE");

      u8g.drawFrame(5, 35, 30, 20);

      u8g.drawBox(9, 32, 5, 3 );

      u8g.drawBox(26, 32, 5, 3 );

      u8g.drawLine(9, 42, 12, 42);

      u8g.drawLine(26, 42, 30, 42);

      u8g.drawLine(28, 40, 28, 44);

      lcd.setFontSize(FONT_SIZE_XLARGE);

      lcd.setCursor(50, 5);

      lcd.print(feszultseg);

      u8g.drawStr( 100, 50, "V");

    }

    while ( u8g.nextPage() );

  }

 

  if (fordulatszam > 5000 && fordulatszam <= 10000)

  {

    if (mmodeactivated < 3) {

      u8g.firstPage();

      do

      {

        u8g.setFont(u8g_font_helvB12);

        u8g.drawStr( 25 , 15, "M-SPORT");

        u8g.drawStr( 40 , 37, "MODE");

        u8g.drawStr( 15 , 60, "ACTIVATED");

      }

      while (u8g.nextPage());

      delay(650);

      u8g.firstPage();

      do

      {

      }

      while (u8g.nextPage());

      delay(250);

      mmodeactivated = mmodeactivated + 1;

    }

    else if (mmodeactivated > 2)

    {

      u8g.firstPage();

      do

      {

        u8g.drawLine(0, 20, 128, 20);

        u8g.setFont(u8g_font_helvB12);

        u8g.drawStr( 0, 13, "M-SPORT MODE");

        if (fordulatszam >= 5000 && fordulatszam <= 5150)

        {

          u8g.drawBox ( 5 , 56 , 10 , 4 );

        }

        if (fordulatszam > 5150 && fordulatszam <= 5300)

        {

          u8g.drawBox ( 5 , 56 , 10 , 4 );

          u8g.drawBox ( 20 , 52 , 10 , 8);

        }

        if (fordulatszam > 5300 && fordulatszam <= 5450)

        {

          u8g.drawBox ( 5 , 56 , 10 , 4 );

          u8g.drawBox ( 20 , 52 , 10 , 8);

          u8g.drawBox ( 35 , 48 , 10 , 12);

        }

        if (fordulatszam > 5450 && fordulatszam <= 5650)

        {

          u8g.drawBox ( 5 , 56 , 10 , 4 );

          u8g.drawBox ( 20 , 52 , 10 , 8);

          u8g.drawBox ( 35 , 48 , 10 , 12);

          u8g.drawBox ( 50 , 42 , 10 , 16);

        }

        if (fordulatszam > 5650 && fordulatszam <= 5850)

        {

          u8g.drawBox ( 5 , 56 , 10 , 4 );

          u8g.drawBox ( 20 , 52 , 10 , 8);

          u8g.drawBox ( 35 , 48 , 10 , 12);

          u8g.drawBox ( 50 , 42 , 10 , 16);

          u8g.drawBox ( 65 , 38 , 10 , 20);

        }

        if (fordulatszam > 5850 && fordulatszam <= 6050)

        {

          u8g.drawBox ( 5 , 56 , 10 , 4 );

          u8g.drawBox ( 20 , 52 , 10 , 8);

          u8g.drawBox ( 35 , 48 , 10 , 12);

          u8g.drawBox ( 50 , 42 , 10 , 16);

          u8g.drawBox ( 65 , 38 , 10 , 20);

          u8g.drawBox ( 80 , 34 , 10 , 24);

        }

        if (fordulatszam > 6050 && fordulatszam <= 6250)

        {

          u8g.drawBox ( 5 , 56 , 10 , 4 );

          u8g.drawBox ( 20 , 52 , 10 , 8);

          u8g.drawBox ( 35 , 48 , 10 , 12);

          u8g.drawBox ( 50 , 44 , 10 , 16);

          u8g.drawBox ( 65 , 38 , 10 , 20);

          u8g.drawBox ( 80 , 34 , 10 , 24);

          u8g.drawBox ( 95 , 30 , 10 , 28);

        }

        if (fordulatszam > 6250 && fordulatszam <= 6500)

        {

          u8g.drawBox ( 5 , 56 , 10 , 4 );

          u8g.drawBox ( 20 , 52 , 10 , 8);

          u8g.drawBox ( 35 , 48 , 10 , 12);

          u8g.drawBox ( 50 , 44 , 10 , 16);

          u8g.drawBox ( 65 , 40 , 10 , 20);

          u8g.drawBox ( 80 , 36 , 10 , 24);

          u8g.drawBox ( 95 , 32 , 10 , 28);

          u8g.drawBox ( 110, 28 , 10 , 32);

        }

        if (fordulatszam > 6500 && fordulatszam <= 10000)

        {

          u8g.setFont(u8g_font_helvB12);

          u8g.drawStr( 0, 50, "|||||  S H I F T  |||||");

        }

      }

      while ( u8g.nextPage() );

    }


}
  attachInterrupt(digitalPinToInterrupt(inPin), rpm_fan_plus , RISING);      //FALLING (Magasról alacsonyra)   RISING (Alacsonyról magasra)

  attachInterrupt(digitalPinToInterrupt(inPin), rpm_fan_minus , FALLING);      //FALLING (Magasról alacsonyra)   RISING (Alacsonyról magasra)

Each time you add an interrupt handler for a pin, all previous interrupt handlers for that pin are removed. So, you end up with only the rpm_fan_minus() handler being registered. You need to use CHANGE as the type, and in rpm_fan() determine whether the signal was rising or falling.

  if (millis() - lastmillis == 1000)

STILL wrong.

    attachInterrupt(digitalPinToInterrupt(inPin), rpm_fan_plus , RISING);      //FALLING (Magasról alacsonyra)   RISING (Alacsonyról magasra)

    attachInterrupt(digitalPinToInterrupt(inPin), rpm_fan_minus , FALLING);      //FALLING (Magasról alacsonyra)   RISING (Alacsonyról magasra)

    detachInterrupt(digitalPinToInterrupt(inPin));    //Disable interrupt when calculating

NO. NO. NO.

Use cli() to disable interrupts and sei() to enable them. Disable ONLY long enough to copy the relevant data.

Why

does

your

code

have

so

many

blank

lines?

Here is the code without many blank spaces. I edited the millis. But i don't know how this cli() and sei() code working. Sry i'm newbie in C. Hard to understand this codes right now,i only understand the simple codes codes,but i try study that evrey day. So if you can help me,and take me a little lesson what is this,and example for me in this code i take a thank you for it! And many thanks bye the way for the helping from the first post :slight_smile:

#include "U8glib.h"
#include "MicroLCD.h"
#include <Arduino.h>
volatile int rpmcount = 0;
int rpm = 0;
unsigned long lastmillis = 0;
int mmodeactivated = 0;
int fordulatszam;
int inPin = 2;
int ledPin = 13;
int feszultseg = 14;

void rpm_fan_plus()

{                         /* this code will be executed every time the interrupt 0 (pin2) gets lower.*/
  rpmcount++;
}

void rpm_fan_minus()

{                         /* this code will be executed every time the interrupt 0 (pin2) gets higher.*/
  rpmcount--;
}

U8GLIB_SSD1306_128X64 u8g(U8G_I2C_OPT_NONE);
LCD_SSD1306 lcd;

void setup() {
  pinMode(inPin, INPUT);
  attachInterrupt(digitalPinToInterrupt(inPin), rpm_fan_minus , FALLING);      //FALLING (Magasról alacsonyra)   RISING (Alacsonyról magasra)
}
void loop()
{
  if (millis() - lastmillis >= 1000)
  {
    attachInterrupt(digitalPinToInterrupt(inPin), rpm_fan_plus , RISING);      //FALLING (Magasról alacsonyra)   RISING (Alacsonyról magasra)
    attachInterrupt(digitalPinToInterrupt(inPin), rpm_fan_minus , FALLING);      //FALLING (Magasról alacsonyra)   RISING (Alacsonyról magasra)
    detachInterrupt(digitalPinToInterrupt(inPin));    //Disable interrupt when calculating
    rpm = rpmcount;                           /* Convert frecuency to RPM, note: this works for one interruption per full rotation. For two interrups per full rotation use rpmcount * 30.*/
    lastmillis = millis(); 			// Uptade lasmillis
  }
  fordulatszam = rpmcount * 2;
  if (10 <= fordulatszam)
  {
  u8g.sleepOff();
  }
  else
  {
  u8g.sleepOn();
  }
  if (fordulatszam >= 10 && fordulatszam <= 2800)
  {
    u8g.firstPage();
    do
    {
      u8g.drawLine(0, 20, 128, 20);
      u8g.setFont(u8g_font_helvB12);
      u8g.drawStr( 19, 13, "ECO MODE");
      u8g.drawFrame(5, 35, 30, 20);
      u8g.drawBox(9, 32, 5, 3 );
      u8g.drawBox(26, 32, 5, 3 );
      u8g.drawLine(9, 42, 12, 42);
      u8g.drawLine(26, 42, 30, 42);
      u8g.drawLine(28, 40, 28, 44);
      lcd.setFontSize(FONT_SIZE_XLARGE);
      lcd.setCursor(50, 5);
      lcd.print(feszultseg);
      u8g.drawStr( 100, 50, "V");
    }
    while ( u8g.nextPage() );
  }
  if (fordulatszam > 2800 && fordulatszam <= 5000)
  {
    u8g.firstPage();
    do
    {
      u8g.drawLine(0, 20, 128, 20);
      u8g.setFont(u8g_font_helvB12);
      u8g.drawStr( 9, 13, "SPORT MODE");
      u8g.drawFrame(5, 35, 30, 20);
      u8g.drawBox(9, 32, 5, 3 );
      u8g.drawBox(26, 32, 5, 3 );
      u8g.drawLine(9, 42, 12, 42);
      u8g.drawLine(26, 42, 30, 42);
      u8g.drawLine(28, 40, 28, 44);
      lcd.setFontSize(FONT_SIZE_XLARGE);
      lcd.setCursor(50, 5);
      lcd.print(feszultseg);
      u8g.drawStr( 100, 50, "V");
    }
    while ( u8g.nextPage() );
  }
  if (fordulatszam > 5000 && fordulatszam <= 10000)
  {
    if (mmodeactivated < 3) 
	{
      u8g.firstPage();
      do
      {
        u8g.setFont(u8g_font_helvB12);
        u8g.drawStr( 25 , 15, "M-SPORT");
        u8g.drawStr( 40 , 37, "MODE");
        u8g.drawStr( 15 , 60, "ACTIVATED");
      }
      while (u8g.nextPage());
      delay(650);
      u8g.firstPage();
      do
      {
      }
      while (u8g.nextPage());
      delay(250);
      mmodeactivated = mmodeactivated + 1;
    }
    else if (mmodeactivated > 2)
    {
      u8g.firstPage();
      do
      {
        u8g.drawLine(0, 20, 128, 20);
        u8g.setFont(u8g_font_helvB12);
        u8g.drawStr( 0, 13, "M-SPORT MODE");

        if (fordulatszam >= 5000 && fordulatszam <= 5150)
        {
          u8g.drawBox ( 5 , 56 , 10 , 4 );
        }
        if (fordulatszam > 5150 && fordulatszam <= 5300)
        {
          u8g.drawBox ( 5 , 56 , 10 , 4 );
          u8g.drawBox ( 20 , 52 , 10 , 8);
        }
        if (fordulatszam > 5300 && fordulatszam <= 5450)
        {
          u8g.drawBox ( 5 , 56 , 10 , 4 );
          u8g.drawBox ( 20 , 52 , 10 , 8);
          u8g.drawBox ( 35 , 48 , 10 , 12);
        }
        if (fordulatszam > 5450 && fordulatszam <= 5650)
        {
          u8g.drawBox ( 5 , 56 , 10 , 4 );
          u8g.drawBox ( 20 , 52 , 10 , 8);
          u8g.drawBox ( 35 , 48 , 10 , 12);
          u8g.drawBox ( 50 , 42 , 10 , 16);
        }
        if (fordulatszam > 5650 && fordulatszam <= 5850)
        {
          u8g.drawBox ( 5 , 56 , 10 , 4 );
          u8g.drawBox ( 20 , 52 , 10 , 8);
          u8g.drawBox ( 35 , 48 , 10 , 12);
          u8g.drawBox ( 50 , 42 , 10 , 16);
          u8g.drawBox ( 65 , 38 , 10 , 20);
        }
        if (fordulatszam > 5850 && fordulatszam <= 6050)
        {
          u8g.drawBox ( 5 , 56 , 10 , 4 );
          u8g.drawBox ( 20 , 52 , 10 , 8);
          u8g.drawBox ( 35 , 48 , 10 , 12);
          u8g.drawBox ( 50 , 42 , 10 , 16);
          u8g.drawBox ( 65 , 38 , 10 , 20);
          u8g.drawBox ( 80 , 34 , 10 , 24);
        }
        if (fordulatszam > 6050 && fordulatszam <= 6250)
        {
          u8g.drawBox ( 5 , 56 , 10 , 4 );
          u8g.drawBox ( 20 , 52 , 10 , 8);
          u8g.drawBox ( 35 , 48 , 10 , 12);
          u8g.drawBox ( 50 , 44 , 10 , 16);
          u8g.drawBox ( 65 , 38 , 10 , 20);
          u8g.drawBox ( 80 , 34 , 10 , 24);
          u8g.drawBox ( 95 , 30 , 10 , 28);
        }
        if (fordulatszam > 6250 && fordulatszam <= 6500)
        {
          u8g.drawBox ( 5 , 56 , 10 , 4 );
          u8g.drawBox ( 20 , 52 , 10 , 8);
          u8g.drawBox ( 35 , 48 , 10 , 12);
          u8g.drawBox ( 50 , 44 , 10 , 16);
          u8g.drawBox ( 65 , 40 , 10 , 20);
          u8g.drawBox ( 80 , 36 , 10 , 24);
          u8g.drawBox ( 95 , 32 , 10 , 28);
          u8g.drawBox ( 110, 28 , 10 , 32);
        }
        if (fordulatszam > 6500 && fordulatszam <= 10000)
        {
          u8g.setFont(u8g_font_helvB12);
          u8g.drawStr( 0, 50, "|||||  S H I F T  |||||");
        }
      }
      while ( u8g.nextPage() );
    }
  }
}
    attachInterrupt(digitalPinToInterrupt(inPin), rpm_fan_plus , RISING);      //FALLING (Magasról alacsonyra)   RISING (Alacsonyról magasra)
    attachInterrupt(digitalPinToInterrupt(inPin), rpm_fan_minus , FALLING);      //FALLING (Magasról alacsonyra)   RISING (Alacsonyról magasra)
    detachInterrupt(digitalPinToInterrupt(inPin));    //Disable interrupt when calculating
    rpm = rpmcount;                           /* Convert frecuency to RPM, note: this works for one interruption per full rotation. For two interrups per full rotation use rpmcount * 30.*/
    lastmillis = millis(); 			// Uptade lasmillis

should be:

    cli();  // Disable interrupts
    rpm = rpmcount;
    lastmillis = millis(); 			// Uptade lasmillis
    sei(); // Enable interrupts

Ahha,so the "cli" is disable,the Sei enable,and only in the setup to take the interrupt. Ohh now i understand . Thank you very much. So if i edit this it must to be working now. I test it. And the 30-sec delaying (in the frist post) how can i write it? Take a simple delay? Or write another millis?

Or write another millis?

Yes. You do not want to use delay(). Record when the mode is started. Periodically, see if that mode has been active for whatever interval is interesting. If so, make whatever change is appropriate.

Note that you can't force the RPM to be something else, so you need to base the mode to display on something besides RPM if you want to tell the driver to shift only for some amount of time, even if the driver doesn't shift.

It's a bit hard for me again (because i said this millis and etc i'm stoled from the web,so that was he way? Take an int for the mmodemillis = 0 in the first lines,than add to this mode. Or again wrong? (Sry but i'm not find in my language an arduino help,or study things,examples etc. and a bit harder to study,and understand it in english for me :frowning: and google translate was not the best :slight_smile: )

}
    else if (mmodeactivated > 2)
    {
      if(mmodemillis = millis(30000))
      {
      u8g.firstPage();
      do
      {
        u8g.drawLine(0, 20, 128, 20);
        u8g.setFont(u8g_font_helvB12);
        u8g.drawStr( 0, 13, "M-SPORT MODE");

        if (fordulatszam >= 5000 && fordulatszam <= 5150)
        {
          u8g.drawBox ( 5 , 56 , 10 , 4 );
        }
        if (fordulatszam > 5150 && fordulatszam <= 5300)
        {
      if(mmodemillis = millis(30000))

The millis() function does not take any arguments. Assigning a value in an if statement is rarely correct.

Did you mean

   if(mmodemillis - millis() >= 30000)

?

Yeap my fault,same code right. Same like the first millis.

This code is the lastest (without the 30sec mode thing) but not this the important.
I send to Serial Monitor the real RPM (fordulatszam). And not work. now in this code always send 2040...2040...2040...2040... i push pedal nothing change,i turn off engine nothing change. If i take the countrpm-- nothing change only the -.
If i change FALLING to RISING nothing change.
If i change sei and cli to each other only change it to around 240...240...240..240 or 0..0..0...0...
I think something still wrong.

#include "U8glib.h"
#include "MicroLCD.h"
#include <Arduino.h>
volatile int rpmcount = 0;
int rpm = 0;
unsigned long lastmillis = 0;
int mmodeactivated = 0;
int fordulatszam;
int inPin = 2;
int ledPin = 13;
float vPow = 4.7;
float r1 = 100000;
float r2 = 10000;
int feszultseg = 0;

void rpm_fan_minus()
{
  rpmcount--;
}
void rpm_fan_plus()
{
  rpmcount++;
}

U8GLIB_SSD1306_128X64 u8g(U8G_I2C_OPT_NONE);
LCD_SSD1306 lcd;

void setup() {
  Serial.begin(9600);
  pinMode(inPin, INPUT);      //FALLING (MagasrĂłl alacsonyra)   RISING (AlacsonyrĂłl magasra)
  attachInterrupt(digitalPinToInterrupt(inPin), rpm_fan_plus , FALLING);
}
void loop()
{
  float v = (analogRead(0) * vPow) / 1024.0;
  float feszultseg = v / (r2 / (r1+r2));
  if (millis() - lastmillis >= 1000)
  {
    cli();
    rpm = rpmcount ;
    lastmillis = millis();    // Uptade lasmillis
    sei();
  }
  fordulatszam = rpmcount * 60;
  Serial.print(fordulatszam);
  //if (10 <= fordulatszam)
  //{
  //u8g.sleepOff();
  //}
  //else
  //{
  //u8g.sleepOn();
  //}
  if (fordulatszam >= 0 && fordulatszam <= 2800)
  {
    u8g.firstPage();
    do
    {
      u8g.drawLine(0, 20, 128, 20);
      u8g.setFont(u8g_font_helvB12);
      u8g.drawStr( 19, 13, "ECO MODE");
      u8g.drawFrame(5, 35, 30, 20);
      u8g.drawBox(9, 32, 5, 3 );
      u8g.drawBox(26, 32, 5, 3 );
      u8g.drawLine(9, 42, 12, 42);
      u8g.drawLine(26, 42, 30, 42);
      u8g.drawLine(28, 40, 28, 44);
      u8g.setPrintPos(50, 50);
      u8g.print(feszultseg);
      u8g.drawStr( 100, 50, "V");
    }
    while ( u8g.nextPage() );
  }
  if (fordulatszam > 2800 && fordulatszam <= 5000)
  {
    u8g.firstPage();
    do
    {
      u8g.drawLine(0, 20, 128, 20);
      u8g.setFont(u8g_font_helvB12);
      u8g.drawStr( 9, 13, "SPORT MODE");
      u8g.drawFrame(5, 35, 30, 20);
      u8g.drawBox(9, 32, 5, 3 );
      u8g.drawBox(26, 32, 5, 3 );
      u8g.drawLine(9, 42, 12, 42);
      u8g.drawLine(26, 42, 30, 42);
      u8g.drawLine(28, 40, 28, 44);
      u8g.setPrintPos(50, 50);
      u8g.print(feszultseg);
      u8g.drawStr( 100, 50, "V");
    }
    while ( u8g.nextPage() );
  }
  if (fordulatszam > 5000 && fordulatszam <= 10000)
  {
    if (mmodeactivated < 2) 
  {
      u8g.firstPage();
      do
      {
        u8g.setFont(u8g_font_helvB12);
        u8g.drawStr( 25 , 15, "M-SPORT");
        u8g.drawStr( 40 , 37, "MODE");
        u8g.drawStr( 15 , 60, "ACTIVATED");
      }
      while (u8g.nextPage());
      delay(650);
      u8g.firstPage();
      do
      {
      }
      while (u8g.nextPage());
      delay(250);
      mmodeactivated = mmodeactivated + 1;
    }
    else if (mmodeactivated > 1)
    {
      u8g.firstPage();
      do
      {
        u8g.drawLine(0, 20, 128, 20);
        u8g.setFont(u8g_font_helvB12);
        u8g.drawStr( 0, 13, "M-SPORT MODE");

        if (fordulatszam >= 5000 && fordulatszam <= 5150)
        {
          u8g.drawBox ( 5 , 56 , 10 , 4 );
        }
        if (fordulatszam > 5150 && fordulatszam <= 5300)
        {
          u8g.drawBox ( 5 , 56 , 10 , 4 );
          u8g.drawBox ( 20 , 52 , 10 , 8);
        }
        if (fordulatszam > 5300 && fordulatszam <= 5450)
        {
          u8g.drawBox ( 5 , 56 , 10 , 4 );
          u8g.drawBox ( 20 , 52 , 10 , 8);
          u8g.drawBox ( 35 , 48 , 10 , 12);
        }
        if (fordulatszam > 5450 && fordulatszam <= 5650)
        {
          u8g.drawBox ( 5 , 56 , 10 , 4 );
          u8g.drawBox ( 20 , 52 , 10 , 8);
          u8g.drawBox ( 35 , 48 , 10 , 12);
          u8g.drawBox ( 50 , 42 , 10 , 16);
        }
        if (fordulatszam > 5650 && fordulatszam <= 5850)
        {
          u8g.drawBox ( 5 , 56 , 10 , 4 );
          u8g.drawBox ( 20 , 52 , 10 , 8);
          u8g.drawBox ( 35 , 48 , 10 , 12);
          u8g.drawBox ( 50 , 42 , 10 , 16);
          u8g.drawBox ( 65 , 38 , 10 , 20);
        }
        if (fordulatszam > 5850 && fordulatszam <= 6050)
        {
          u8g.drawBox ( 5 , 56 , 10 , 4 );
          u8g.drawBox ( 20 , 52 , 10 , 8);
          u8g.drawBox ( 35 , 48 , 10 , 12);
          u8g.drawBox ( 50 , 42 , 10 , 16);
          u8g.drawBox ( 65 , 38 , 10 , 20);
          u8g.drawBox ( 80 , 34 , 10 , 24);
        }
        if (fordulatszam > 6050 && fordulatszam <= 6250)
        {
          u8g.drawBox ( 5 , 56 , 10 , 4 );
          u8g.drawBox ( 20 , 52 , 10 , 8);
          u8g.drawBox ( 35 , 48 , 10 , 12);
          u8g.drawBox ( 50 , 44 , 10 , 16);
          u8g.drawBox ( 65 , 38 , 10 , 20);
          u8g.drawBox ( 80 , 34 , 10 , 24);
          u8g.drawBox ( 95 , 30 , 10 , 28);
        }
        if (fordulatszam > 6250 && fordulatszam <= 6500)
        {
          u8g.drawBox ( 5 , 56 , 10 , 4 );
          u8g.drawBox ( 20 , 52 , 10 , 8);
          u8g.drawBox ( 35 , 48 , 10 , 12);
          u8g.drawBox ( 50 , 44 , 10 , 16);
          u8g.drawBox ( 65 , 40 , 10 , 20);
          u8g.drawBox ( 80 , 36 , 10 , 24);
          u8g.drawBox ( 95 , 32 , 10 , 28);
          u8g.drawBox ( 110, 28 , 10 , 32);
        }
        if (fordulatszam > 6500 && fordulatszam <= 10000)
        {
          u8g.setFont(u8g_font_helvB12);
          u8g.drawStr( 0, 50, "|||||  S H I F T  |||||");
        }
      }
      while ( u8g.nextPage() );
    }
  }
}
  if (millis() - lastmillis >= 1000)
  {
    cli();
    rpm = rpmcount ;
    lastmillis = millis();    // Uptade lasmillis
    sei();
  }

Once a second, you capture the number of pulses. That is NOT a count of the number of revolutions per minute. So the name used in the ISR is stupid. The number of pulses in one second is not the number of revolutions per minute.

Once a second, print the number of pulses WITHOUT F**KING WITH THE VALUE. If the number of pulses is not reasonable, then it is pointless to use the value and expect meaning.

Okay so it's not a count,it's a pulse,but how can i make from this pulse that real rpm value revolution per minute? Because i don't understand wich value do you mean at that code.

The code from here what is stoled Arduino Playground - ReadingRPM

Okay so it's not a count,it's a pulse

That you want to count. So, how about pulseCount as the variable name?

but how can i make from this pulse that real rpm value revolution per minute?

A little bit of simple math. You have some number of pulses. You know what time frame they arrived in. You know that some number of pulses equals one revolution. So, the number of pulses you have corresponds to some number of revolutions. The number of revolutions in the time frame that you have is in a direct ratio to revolutions in one minute.

So, fill in some numbers:

I got _____ pulses.
In ______ milliseconds.
There are _______ pulses per revolution.

So its rpm=pulsecount601000 thats what you mean.
But why on serail print take always 2040... 2040... 2040...?
This print speed is the pulsecount? Or the number?

So its rpm=pulsecount601000 thats what you mean.

Is it too hard to answer simple questions? I'll post just one then, so you don't feel overwhelmed.

How many pulses per revolution do you get?

Sorry i said,bit bad english,i can't understand some words sometimes. (and translator is not translate me perfect the hard words. ) i can only see that in serial print. What you seen in the code where input the serialprint (fordulatszam) that was say 2040 always so i think that was the pulses per revolution.