Hier die geänderte Version:
//Anzahl Spalten des Display (16)
#define LCD_WIDTH 16
//Anzahl Zeilen des Display (2)
#define LCD_HEIGHT 2
// Pin für Reed-Kontakt, Digital-2 für Interrupt 0
#define REEDPIN 2
// Hardware-Interrupt für den Reed-Pin
#define REEDINTERRUPT 0
// Radumfang in mm
#define RADUMFANG 2260
#include <LiquidCrystal.h>
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
// build 2-line digit font data array
// Digits are 3 characters wide.
byte bignums[10][2][3] = {
// Define which characters to use for each number. 255 is a solid block; 254 is a space
// The format is { {TopLeft, TopMiddle, TopRight}, {BottomLeft, BottomMiddle, BottomRight} }
{ {255, 0, 255}, {255, 1, 255} }, // data to display "0"
{ {0, 255, 254}, {1, 255, 1} }, // data to display "1"
{ {0, 5, 255}, {255, 1, 1} }, // data to display "2"
{ {0, 5, 255}, {1, 1, 255} }, // data to display "3"
{ {255, 1, 255}, {254, 254, 255} }, // data to display "4"
{ {255, 5, 5}, {1, 1, 255} }, // data to display "5"
{ {255, 5, 5}, {255, 1, 255} }, // data to display "6"
{ {0, 0, 255}, {254, 255, 254} }, // data to display "7"
{ {255, 5, 255}, {255, 1, 255} }, // data to display "8"
{ {255, 5, 255}, {254, 254, 255} } // data to display "9"
};
void loadchars() { // This subroutine programs the custom character data into the LCD
lcd.command(64);
// Custom character 0
lcd.write(byte(B11111));
lcd.write(byte(B11111));
lcd.write(byte(B11111));
lcd.write(byte(B00000));
lcd.write(byte(B00000));
lcd.write(byte(B00000));
lcd.write(byte(B00000));
lcd.write(byte(B00000));
// Custom character 1
lcd.write(byte(B00000));
lcd.write(byte(B00000));
lcd.write(byte(B00000));
lcd.write(byte(B00000));
lcd.write(byte(B00000));
lcd.write(byte(B11111));
lcd.write(byte(B11111));
lcd.write(byte(B11111));
// Custom character 2
lcd.write(byte(B11111));
lcd.write(byte(B11111));
lcd.write(byte(B11111));
lcd.write(byte(B00000));
lcd.write(byte(B00000));
lcd.write(byte(B11111));
lcd.write(byte(B11111));
lcd.write(byte(B11111));
// Custom character 3
lcd.write(byte(B00000));
lcd.write(byte(B00000));
lcd.write(byte(B00000));
lcd.write(byte(B00000));
lcd.write(byte(B00000));
lcd.write(byte(B01110));
lcd.write(byte(B01110));
lcd.write(byte(B01110));
// Custom character 4
lcd.write(byte(B00000));
lcd.write(byte(B00000));
lcd.write(byte(B00000));
lcd.write(byte(B01110));
lcd.write(byte(B01110));
lcd.write(byte(B01110));
lcd.write(byte(B00000));
lcd.write(byte(B00000));
// Custom character 5
lcd.write(byte(B11111));
lcd.write(byte(B11111));
lcd.write(byte(B11111));
lcd.write(byte(B00000));
lcd.write(byte(B00000));
lcd.write(byte(B00000));
lcd.write(byte(B11111));
lcd.write(byte(B11111));
// Custom character 6
lcd.write(byte(B11111));
lcd.write(byte(B11111));
lcd.write(byte(B11111));
lcd.write(byte(B00000));
lcd.write(byte(B00000));
lcd.write(byte(B00000));
lcd.write(byte(B11110));
lcd.write(byte(B11111));
// Custom character 7
lcd.write(byte(B00000));
lcd.write(byte(B00000));
lcd.write(byte(B00000));
lcd.write(byte(B00000));
lcd.write(byte(B00000));
lcd.write(byte(B00000));
lcd.write(byte(B00000));
lcd.write(byte(B00000));
lcd.home();
}
void printbigchar(byte digit, byte col) { // This subroutine prints the big font characters on the LCD screen
if (digit > 9) return; // reject anything above 9
for (int i = 0; i < 2; i++) { // count i from 0 to 1
lcd.setCursor(col*4 , i); // set LCD cursor at correct point
for (int j = 0; j < 3; j++) { // count j from 0 to 2
lcd.write(bignums[digit][i][j]); // write proper block to LCD from array
}
lcd.write(254); // write an empty space
}
lcd.setCursor(col + 4, 0); // move the cursor to the top line, col + 4
}
void setup(){
pinMode(REEDPIN, INPUT_PULLUP); // Reedkontakt direkt und ohne Widerstand angeschlossen
lcd.begin(LCD_WIDTH, LCD_HEIGHT);
loadchars();
attachInterrupt(REEDINTERRUPT, reedISR, FALLING);
}
volatile byte reedCountSum;
volatile long reedMillisSum;
unsigned long lastReedMillis;
void reedISR()
{
if (millis()-lastReedMillis>=25) // 25ms entspricht max. 40 Radumdrehungen pro Sekunde
{
reedCountSum++; // eine Radumdrehung zählen
reedMillisSum+=millis()-lastReedMillis; // Zeit addieren
lastReedMillis=millis(); // Zeit merken
}
}
unsigned long gesamtUmdrehungen;
void tachoAnzeige()
{
byte umdrehungen;
unsigned long zeit;
float kph, kilometer;
int kphRounded;
char buffer[10];
noInterrupts(); // Interrupts sperren
umdrehungen=reedCountSum;// Zählvariable umkopieren
reedCountSum=0; // Zählvariable auf 0 zurücksetzen
zeit=reedMillisSum; // Zeitzähler umkopieren
reedMillisSum=0; // Zeitzähler auf 0 zurücksetzen
interrupts(); // Interrupts wieder zulassen
gesamtUmdrehungen+= umdrehungen; // Aufsummieren aller Radumdrehungen
kilometer=(float)gesamtUmdrehungen*(float)RADUMFANG/1000000.0; // Fahrtkilometerzähler
if (umdrehungen>0)
kph=float(RADUMFANG)*(float)umdrehungen/(float)zeit*3.6;
else
kph=0.0;
lcd.setCursor(12, 0);
dtostrf(kilometer,3, 1, buffer);
lcd.print(buffer);
/*
lcd.setCursor(0, 0);
dtostrf(kilometer,9,3,buffer);
lcd.print(buffer);
lcd.print(" km");
lcd.setCursor(0, 1);
lcd.print("KM/H ");
dtostrf(kph,5,1,buffer);
*/
kphRounded=int(kph+0.5); // auf ganze kph gerundet
// Ausgabe kph in grossen Ziffern
kphRounded = kphRounded % 1000; // drop any digits above 999
printbigchar(kphRounded / 100,0); // print the speed hundreds
kphRounded = kphRounded % 100; // drop any digits above 99
printbigchar(kphRounded/10,1); // print the speed tens
kphRounded = kphRounded % 10; // drop any digits above 9
printbigchar(kphRounded,2); // print the speed ones
lcd.setCursor(12, 1);
lcd.print("km/h");
}
unsigned long letzteSekunde=0;
void loop()
{
unsigned long dieseSekunde=millis()/1000;
// Tachoanzeige wird genau einmal pro Sekunde aktualisiert
if (letzteSekunde != dieseSekunde)
{
tachoAnzeige();
letzteSekunde=dieseSekunde;
}
}
Micky