unsigned long timer[5], ch[3], ch2[3], faktor[1];
byte last_channel[1];
int input[3];
int impulso = 0;
double dispValues[3];
float PresCorr = 0.88;
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define LARGHEZZA_SCHERMO 128 // Larghezza display OLED, in pixel
#define ALTEZZA_SCHERMO 64 // Altezza display OLED, in pixel
Adafruit_SSD1306 display(LARGHEZZA_SCHERMO, ALTEZZA_SCHERMO, &Wire, -1);
const unsigned char LOGO [] PROGMEM = {
// Mazda logo, 128x64px
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1f, 0xff, 0xff, 0xf8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xfe, 0x00, 0xff, 0xfe, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0xf8, 0x00, 0x00, 0x7f, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0xf0, 0x00, 0x00, 0x0f, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0x80, 0x00, 0x00, 0x03, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x13, 0x00, 0x00, 0x00, 0x01, 0xb0, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x31, 0x00, 0x00, 0x00, 0x02, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x70, 0x80, 0x00, 0x00, 0x00, 0x30, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x7c, 0x70, 0x00, 0x00, 0x18, 0x70, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x5e, 0x1c, 0x00, 0x00, 0xc1, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x1f, 0xc7, 0x00, 0x03, 0x07, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x90, 0x71, 0xc0, 0x06, 0x3f, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0xb0, 0x7e, 0x60, 0x19, 0xfc, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x30, 0x03, 0xf0, 0x37, 0xe0, 0x70, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x30, 0x01, 0xb0, 0x2f, 0x80, 0x70, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x30, 0x00, 0xf8, 0x7e, 0x00, 0x70, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0xb0, 0x00, 0x3d, 0x78, 0x00, 0x70, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0xb8, 0x00, 0x3c, 0xf0, 0x00, 0x70, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0xf8, 0x00, 0x16, 0xe0, 0x00, 0xd0, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0xfc, 0x00, 0x0b, 0xc0, 0x00, 0xd0, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x7c, 0x00, 0x05, 0xc0, 0x00, 0x90, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x7e, 0x00, 0x07, 0xc0, 0x01, 0xa0, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x3e, 0x00, 0x03, 0x80, 0x01, 0xa0, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x37, 0x00, 0x03, 0x80, 0x01, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x1b, 0x80, 0x00, 0x00, 0x03, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x1c, 0x80, 0x00, 0x00, 0x06, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x0c, 0xc0, 0x00, 0x00, 0x0c, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0x70, 0x00, 0x00, 0x1d, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0xbc, 0x00, 0x00, 0xfb, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xcf, 0x80, 0x0d, 0xbc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x7f, 0x9f, 0xe8, 0x78, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1f, 0xc0, 0x07, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3f, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0e, 0x00, 0x00, 0x00, 0x7f, 0xc0, 0x00, 0xdf, 0x8c, 0x20,
0x00, 0x00, 0x00, 0x3f, 0xe0, 0x7f, 0xff, 0xe0, 0x00, 0x3f, 0xff, 0xe0, 0x3f, 0xff, 0xff, 0xf0,
0x00, 0x00, 0x00, 0xe0, 0x3b, 0xe0, 0x3f, 0x1c, 0x01, 0xf8, 0x1e, 0x00, 0xe0, 0x00, 0x00, 0x60,
0x00, 0x00, 0x03, 0x80, 0x7f, 0x80, 0x31, 0x87, 0x0f, 0x81, 0xf8, 0x09, 0xc0, 0x0f, 0xff, 0xc0,
0x00, 0x00, 0x06, 0x00, 0x7e, 0x00, 0x60, 0x41, 0xfe, 0x07, 0xc0, 0x0f, 0x81, 0xff, 0xfc, 0x00,
0x00, 0x00, 0x0c, 0x30, 0xfc, 0x10, 0x60, 0x60, 0xf0, 0x1f, 0x80, 0x0f, 0x03, 0xff, 0xf8, 0x00,
0x00, 0x00, 0x38, 0x61, 0xf8, 0x70, 0xc0, 0x38, 0x00, 0x7f, 0xff, 0xfe, 0x03, 0xff, 0xf8, 0x00,
0x00, 0x00, 0x61, 0xe1, 0xe0, 0xe0, 0x80, 0x3c, 0x03, 0xdf, 0xf8, 0x6c, 0x00, 0x00, 0x0c, 0x00,
0x00, 0x01, 0xc3, 0xc3, 0xc3, 0xc1, 0x80, 0x78, 0x07, 0xfe, 0x00, 0xf8, 0x00, 0x00, 0x00, 0x00,
0x00, 0x03, 0x87, 0x87, 0x87, 0xc1, 0x01, 0xe0, 0x03, 0xff, 0xff, 0xff, 0xf0, 0x3e, 0x01, 0x00,
0x00, 0x06, 0x1f, 0x87, 0x1f, 0x83, 0x07, 0x80, 0x01, 0xff, 0xff, 0xff, 0x00, 0x0e, 0x00, 0x00,
0x00, 0x0c, 0x3f, 0x0c, 0x3f, 0x02, 0x1e, 0x01, 0xc0, 0xe0, 0x00, 0x00, 0x00, 0x0e, 0x01, 0x00,
0x00, 0x38, 0x7f, 0x08, 0xff, 0x06, 0x78, 0x07, 0xe0, 0x20, 0x00, 0x00, 0x00, 0x3c, 0x03, 0x00,
0x00, 0x7f, 0xfe, 0x01, 0xde, 0x07, 0xe0, 0x1c, 0x30, 0x18, 0x00, 0x00, 0x01, 0xf0, 0x06, 0x00,
0x00, 0xff, 0xfd, 0xe7, 0x8e, 0x0f, 0x80, 0xf0, 0x18, 0x0c, 0x00, 0x43, 0xff, 0xc0, 0x1c, 0x00,
0x03, 0xff, 0xff, 0xee, 0x0d, 0xfe, 0x03, 0xc0, 0x0c, 0x06, 0x03, 0xff, 0xf8, 0x01, 0xf0, 0x00,
0x07, 0xff, 0xff, 0xfc, 0x0c, 0x00, 0x3e, 0x00, 0x07, 0x03, 0xff, 0x00, 0x00, 0x3f, 0xc0, 0x00,
0x0e, 0xff, 0xff, 0xf0, 0x0f, 0xff, 0xf8, 0x00, 0x03, 0xff, 0xff, 0xff, 0xff, 0xfc, 0x00, 0x00
};
const unsigned char snowflake [] PROGMEM {
// Icona fiocco di neve, 9x9px
0x08, 0x00, 0x2a, 0x00, 0x6b, 0x00, 0x14, 0x00, 0xeb, 0x80, 0x14, 0x00, 0x6b, 0x00, 0x2a, 0x00,
0x08, 0x00
};
int analogIn = 1; // Cambiato da 0 a 1 per leggere da A1
int val = 0;
int x1 = 0;
int x2 = 0;
int x3 = 0;
int needle = 0;
void setup() {
PCICR |= (1 << PCIE0); // PIN D8
PCMSK0 |= (1 << PCINT0);
delay(2000);
Serial.begin(9600);
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Indirizzo 0x3C per 128x64
Serial.println(F("Errore nell'allocazione SSD1306"));
for (;;);
}
display.clearDisplay();
display.drawBitmap(0, 0, LOGO, 128, 64, WHITE); // Logo Mazda
display.display();
delay(500); // tempo di visualizzazione Logo Mazda orig 2000
pinMode(A0, INPUT);
}
void loop() {
delay(1);
display.clearDisplay();
/*********** BORDI ***********/
display.drawRect(0, 0, 128, 64, WHITE); // rettangolo riquadro
display.drawLine(63, 0, 63, 63, WHITE); // Bordo divisorio verticale
display.drawLine(0, 32, 128, 32, WHITE); // Bordo divisorio inferiore
/*********** VALORE TEMPERATURA ***********/
display.setTextSize(2); // valore originale 2 cambia la dim. del carattere
display.setCursor(10, 10); // valore originale 13, 28 cambia la posizione del carattere
if (dispValues[0] < 65) { // sotto i 65 °C compare l'icona del fiocco di neve
display.setTextColor(BLACK);
display.fillRect(2, 2, 60, 29, WHITE);//valore originale 2, 27, 60, 16, WHITE cambia la dim. del rettangolo
display.print(dispValues[0] , 0);
display.drawBitmap(1, 21, snowflake, 9, 9, BLACK); // Icona fiocco di neve orig. 3, 33, snowflake, 9, 9, BLACK
display.setTextColor(BLACK);
display.setTextSize(1);
display.setCursor(52, 21); // val orig (50, 35);
display.print((char)247); //"°" carattere
display.print("C");
}
else {
display.setTextColor(WHITE);
display.print(dispValues[0], 0);
display.setTextColor(WHITE);
display.setTextSize(1);
display.setCursor(50, 20);
display.print((char)247); //"°" carattere
display.print("C");
}
display.setTextColor(WHITE);
display.setTextSize(3);// dimensione carattere ori 2
display.setCursor(65, 9);// posizione cursore pressione
if ((dispValues[1] - PresCorr) < 0.1)
display.print (0.0, 1);
else
display.print(dispValues[1] - PresCorr, 1);
//********** Scritta Bar**********
display.setTextSize(1);
display.setCursor(120, 2);
display.print("B");
display.setCursor(120, 12);
display.print("a");
display.setCursor(120, 22);
display.print("r");
/*********** VOLTAGGIO BATTERIA ***********/
int rawValue = analogRead(A0); // Legge il valore analogico dal pin A0
float Vin = rawValue * 0.0293255;
if (Vin < 11.8) {
display.setTextColor(BLACK);
display.fillRect(2, 34, 60, 27, WHITE);
display.setTextSize(2);// dimensione carattere
display.setCursor(3,40);//posizione carattere
display.print (Vin, 1);
display.setCursor(55,52);//posizione carattere
display.setTextSize(1);// dimensione carattere
display.print ("V");
}
else{
display.setTextSize(2);// dimensione carattere
display.setCursor(3, 40);//posizione carattere
display.print(Vin, 1);
display.setTextSize(1);// dimensione carattere
display.setCursor(55,52);//posizione carattere
display.print("V");
}
//************* scritta AFR **********
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(120, 34);
display.print("A");
display.setCursor(120, 44);
display.print("F");
display.setCursor(120, 54);
display.print("R");
//********* calcolo AFR***********
val = analogRead(analogIn);
needle = map(val, 0, 1023, 1, 127);
display.setTextSize(2);
display.setTextColor(WHITE);
display.setCursor(68, 40);
display.print(x1);
display.print(x2);
display.print(".");
display.print(x3);
display.display();
devide();
}
void devide() {
x1 = 0;
x2 = 0;
x3 = 0;
int value = val;
while (value >= 100) {
value -= 100;
x1++;
}
while (value >= 10) {
value -= 10;
x2++;
}
x3 = value;
//******** fine calcolo AFR *********
}
ISR(PCINT0_vect) {
timer[0] = micros();
if (last_channel[0] == 0 && PINB & B00000001 ) {
last_channel[0] = 1;
ch[impulso] = timer[0] - timer[1];
timer[1] = timer[0];
if (ch[impulso] < 3000) {
impulso = 0;
} else {
impulso++;
}
}
else if (last_channel[0] == 1 && !(PINB & B00000001) ) {
last_channel[0] = 0;
input[impulso] = timer[0] - timer[1];
if (impulso == 0) {
dispValues[0] = ((4096.0 / ch[0]) * input[0] - 128) / 19.2 - 40;
} else if (impulso == 1) {
dispValues[1] = (((4096.0 / ch[1]) * input[1]) - 128) / 384.0 + 0.5;
} else if (impulso == 2) {
dispValues[2] = (1024.0 / ch[2]) * input[2];
if (dispValues[2] >= 231.00 && dispValues[2] <= 281.00) {
dispValues[2] = 1;
}
}
}
}