Ersatz Fahrrad Computer durch Uno

-Micky:
Nur finde ich das man viel zu eckige Zahlen erhält. Man kann ja nur 8 Zeichen neu definieren.

Ja klar, irgendwo ist Schluss mit den Textdisplay-Tricksereien.

Anbei nochmal eine Version mit großen "abgerundeten" Ziffern.

//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);


void loadchars() {                        // This subroutine programs the custom character data into the LCD
// the 8 arrays that form each segment of the custom numbers
byte LT[8] = 
{
  B00111,
  B01111,
  B11111,
  B11111,
  B11111,
  B11111,
  B11111,
  B11111
};
byte UB[8] =
{
  B11111,
  B11111,
  B11111,
  B00000,
  B00000,
  B00000,
  B00000,
  B00000
};
byte RT[8] =
{
  B11100,
  B11110,
  B11111,
  B11111,
  B11111,
  B11111,
  B11111,
  B11111
};
byte LL[8] =
{
  B11111,
  B11111,
  B11111,
  B11111,
  B11111,
  B11111,
  B01111,
  B00111
};
byte LB[8] =
{
  B00000,
  B00000,
  B00000,
  B00000,
  B00000,
  B11111,
  B11111,
  B11111
};
byte LR[8] =
{
  B11111,
  B11111,
  B11111,
  B11111,
  B11111,
  B11111,
  B11110,
  B11100
};
byte MB[8] =
{
  B11111,
  B11111,
  B11111,
  B00000,
  B00000,
  B00000,
  B11111,
  B11111
};
byte block[8] =
{
  B11111,
  B11111,
  B11111,
  B11111,
  B11111,
  B11111,
  B11111,
  B11111
};
// assignes each segment a write number
  lcd.createChar(0,LT);
  lcd.createChar(1,UB);
  lcd.createChar(2,RT);
  lcd.createChar(3,LL);
  lcd.createChar(4,LB);
  lcd.createChar(5,LR);
  lcd.createChar(6,MB);
  lcd.createChar(7,block);
}

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} }
 { {0, 1, 2},   {3, 4, 5} },        // data to display "0"
 { {1, 2, 254}, {4, 7, 4} },          // data to display "1"
 { {6, 6, 2},   {3, 4, 4} },            // data to display "2"
 { {6, 6, 2},   {4, 4, 5} },            // data to display "3"
 { {3, 4, 7},   {254, 254, 7} },      // data to display "4"
 { {3, 6, 6},   {4, 4, 5} },            // data to display "5"
 { {0, 6, 6},   {3, 4, 5} },          // data to display "6"
 { {1, 1, 2},   {254, 254, 7} },        // data to display "7"
 { {0, 6, 2},   {3, 4, 5} },        // data to display "8"
 { {0, 6, 2},   {254, 254, 7} }       // data to display "9"
};

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=random(999);  // nur zum Testen
    
  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;
  }
}