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 ![]()
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
)
#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++;
}
}