Theoretisch auf dem Tisch funktioniert alles, die Aufnahme der Umdrehungen am Moped wollte ich mit einem Reedkontakt realisieren das scheint aber mit den Einstellungen im Sketch nicht zu funktionieren, diesen Code Teil von Basti verstehe ich nicht:
Code: Alles auswählen
void revs() {                           // Interrupt-Routine
  uint32_t jetztMillis = millis();
  if (jetztMillis - lastMillis >= Entprellzeit) {                               // ignorieren wenn <= 10ms (Kontaktpreller)
    geschwindigkeit = RadUmfang * 3.6 / (jetztMillis - lastMillis); // Geschwindigkeit: 1 Interrupt bei jeder Radumdrehung
    lastMillis = millis();
    RotationCounter += 1;
    MotorradFaehrt = true;
Hat jemand zufällig noch eine Idee?
Hier nochmal der komplette Code, da ich viel rausgemacht habe weil ich kein RTC brauche:
Code: Alles auswählen
#include <EEPROM.h>
#include <RunningAverage.h>
#include <Wire.h>
#include <OneButton.h>
#include <U8g2lib.h>
U8G2_SSD1306_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0, /* reset=*/ U8X8_PIN_NONE, /* clock=*/ 16, /* data=*/ 17);   // ESP32 Thing, HW I2C with pin remapping
const uint16_t Entprellzeit = 50; // in ms
volatile uint32_t lastMillis = 0;
RunningAverage myRA(5);
volatile short geschwindigkeit;
const int buttonPin = 2;
const int reedPin = 3;
int screen = 0;
volatile int RotationCounter = 0;
OneButton button(buttonPin, true);
const int RadUmfang = 1690; //Radumfang in MM
char Speed[10];
uint64_t GesamtMillimeter;
uint32_t TripMillimeter;
int GesamtMillimeter_ADDR = 0;
int TripMillimeter_ADDR = GesamtMillimeter_ADDR + 8;    //Gesamtmillimeter ist 64bit Zahl --> braucht 8 Byte im EEPROM
char TripKm[10];
char GesamtKm[10];
boolean reset = false;
boolean MotorradFaehrt = false;
boolean ShowLogo = false;
boolean beimNaechstenStopDatenSichern = false;
#define logo_width 128
#define logo_height 64
const unsigned char logo[] PROGMEM = {
 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,0x50,0x01,0x00,
 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x80,0xba,0x02,
 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x54,0x85,
 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xab,0x2a,
 0x0a,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x58,0x7d,
 0x55,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x50,0xf5,
 0x97,0x2c,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xc0,0xff,0xf5,
 0x5f,0xe9,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x7c,0x15,
 0xfb,0x95,0xfc,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x86,
 0xe4,0x15,0x25,0x5f,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
 0xa9,0x1f,0xff,0x5b,0xb5,0x15,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
 0x00,0xe5,0x6c,0xeb,0xff,0xf7,0x6f,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
 0x00,0x00,0xf9,0x9b,0x52,0xab,0x7f,0xab,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
 0x00,0x00,0x00,0x91,0xbe,0xd6,0xbf,0xf3,0xaf,0x01,0x00,0x00,0x00,0x00,0x00,
 0x00,0x00,0x00,0x00,0xf5,0x9f,0x66,0xdf,0xdf,0x57,0x01,0x00,0x00,0x00,0x00,
 0x00,0x00,0x00,0x00,0x00,0xe9,0xb7,0xd4,0xcd,0xfa,0x56,0xff,0x1f,0x00,0x00,
 0x00,0x00,0x00,0x00,0x00,0x00,0x2a,0x6f,0x77,0xef,0xba,0x35,0xab,0xf6,0x01,
 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xe4,0x5a,0xf3,0x77,0xdb,0xb6,0xfe,0x9b,
 0x02,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x5d,0xed,0xbd,0x6f,0xbb,0xb7,0xed,
 0xbf,0x04,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xff,0x3f,0xfe,0x65,0xea,0x73,
 0x52,0x52,0x0d,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xbe,0xa5,0x77,0x5f,0xfb,
 0x76,0xbf,0xbf,0x12,0x00,0x00,0x00,0x00,0x00,0x00,0xf0,0xf6,0x4a,0xfe,0x6f,
 0xff,0x77,0xf9,0xf7,0x4f,0x00,0x00,0x00,0x00,0x00,0xc0,0x7f,0xbf,0xf5,0xde,
 0xcd,0xda,0xeb,0xf6,0xfe,0xbf,0x00,0x00,0x00,0x00,0x00,0x7e,0xd5,0x6e,0x57,
 0xf3,0x5f,0x7d,0xf7,0xa8,0x6f,0x7f,0x01,0x00,0x00,0x00,0xe0,0x83,0xb6,0xb5,
 0xad,0x54,0x5f,0xf5,0xb5,0xda,0xbb,0x44,0x00,0x00,0x00,0x00,0x3c,0xfe,0x2b,
 0x55,0x52,0x4b,0xb5,0xbe,0xf7,0xf5,0xdf,0xfb,0x02,0x00,0x00,0x00,0xa6,0xbb,
 0xfe,0xff,0xff,0x94,0x7c,0xf8,0xe7,0xb8,0xad,0xff,0x01,0x00,0x00,0x80,0xe9,
 0xd7,0xf7,0xf7,0xef,0x37,0x69,0x63,0xef,0xfb,0xef,0xdf,0x03,0x00,0x00,0x40,
 0x7e,0xfd,0xbd,0xdd,0xbe,0x5f,0xd5,0xfb,0xed,0x7d,0xaf,0xb4,0x02,0x00,0x00,
 0x80,0xdf,0x77,0xff,0x7f,0xfb,0x3d,0x89,0xfb,0x6f,0xed,0xbb,0xd5,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,0x7a,0xa7,0x54,0xeb,0x5e,0x95,0x07,0x00,0xa0,
 0xb1,0xdd,0x6f,0xab,0x12,0x55,0x92,0x76,0x8d,0x5f,0xbf,0x6b,0x25,0x00,0x00,
 0xe0,0xba,0xf3,0x66,0x4d,0xd6,0xb6,0x6d,0x75,0x7b,0xf5,0x65,0x95,0x12,0x00,
 0x00,0x80,0xa4,0xdd,0xdb,0x9a,0xa8,0x6e,0xd5,0xeb,0x56,0xaf,0xfe,0x37,0x05,
 0x00,0x00,0xc0,0xb5,0x75,0xdd,0x55,0x2b,0x5b,0x25,0xdf,0xfd,0x02,0x00,0x00,
 0x00,0x00,0x00,0x80,0xaa,0x55,0x37,0x5b,0xd5,0x77,0x4a,0xaa,0x01,0x00,0x00,
 0x00,0x00,0x00,0x00,0x80,0x71,0xeb,0xbf,0xa4,0x6a,0x5f,0x95,0x80,0x02,0x00,
 0x00,0x00,0x00,0x00,0x00,0x40,0x65,0xdb,0x76,0xbb,0xdb,0xbe,0xa2,0x00,0x00,
 0x00,0x00,0x00,0x00,0x00,0x00,0x80,0x4b,0xd7,0xdf,0xef,0xee,0x2b,0x75,0x00,
 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xd2,0xae,0xbb,0xf6,0x7d,0xcf,0x1e,
 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x95,0x5d,0xad,0x5f,0xf7,0x55,
 0x07,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x26,0xbb,0xde,0xbb,0x77,
 0xda,0x07,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x58,0xf6,0x7a,0xdd,
 0xdb,0xf5,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x90,0xec,0xe5,
 0x6f,0x35,0x7f,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x50,0x99,
 0x2f,0xea,0xee,0xae,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x40,
 0x65,0xfd,0x5d,0xdb,0x5b,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
 0x80,0xc8,0x7b,0xf7,0x7f,0x37,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
 0x00,0x00,0x24,0xd6,0xff,0xd6,0x04,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
 0x00,0x00,0x00,0x98,0x50,0x55,0xab,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
 0x00,0x00,0x00,0x00,0xc0,0x05,0x88,0x14,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
};
#define tachoSymbol_width 48
#define tachoSymbol_height 30
const unsigned char tachoSymbol[] PROGMEM = {
  0x00, 0x00, 0xf0, 0x0f, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0x00, 0x00,
  0x00, 0xc0, 0xff, 0xff, 0x03, 0x00, 0x00, 0xf0, 0xff, 0xff, 0x0f, 0x00,
  0x00, 0xfc, 0xff, 0xff, 0x3f, 0x00, 0x00, 0xfe, 0xcf, 0xf3, 0x7f, 0x00,
  0x00, 0xff, 0xc1, 0x83, 0xff, 0x00, 0x80, 0x7f, 0xc0, 0x03, 0xfe, 0x01,
  0xc0, 0x1f, 0xc0, 0x03, 0xf8, 0x03, 0xe0, 0x0f, 0xc0, 0x03, 0xf0, 0x07,
  0xf0, 0x87, 0x81, 0x01, 0xe0, 0x0f, 0xf8, 0x83, 0x03, 0x00, 0xc0, 0x0f,
  0xf8, 0x81, 0x07, 0x00, 0x80, 0x1f, 0xfc, 0x80, 0x07, 0x00, 0x00, 0x3f,
  0x7c, 0x00, 0x0f, 0x00, 0x00, 0x3e, 0x7e, 0x00, 0x1f, 0x00, 0x00, 0x7e,
  0x3e, 0x00, 0x3e, 0x00, 0x00, 0x7c, 0x3e, 0x00, 0x3e, 0x00, 0x00, 0x7c,
  0x3e, 0x00, 0x7c, 0x00, 0x00, 0x7c, 0x1f, 0x00, 0xfc, 0x03, 0x00, 0xf8,
  0x1f, 0x00, 0xf8, 0x0f, 0x00, 0xf8, 0x1f, 0x00, 0xf8, 0x1f, 0x00, 0xf8,
  0xff, 0xff, 0x7f, 0xfe, 0xff, 0xff, 0xfe, 0xff, 0x3f, 0xfc, 0xff, 0x7f,
  0xfe, 0xff, 0x3f, 0xfc, 0xff, 0x7f, 0xfc, 0xff, 0x3f, 0xfc, 0xff, 0x3f,
  0xf0, 0xff, 0xff, 0xff, 0xff, 0x0f, 0x00, 0x00, 0xf0, 0x0f, 0x00, 0x00,
  0x00, 0x00, 0xf0, 0x0f, 0x00, 0x00, 0x00, 0x00, 0xc0, 0x03, 0x00, 0x00
};
#define route_width 34
#define route_height 34
const unsigned char route_bits[] PROGMEM = {
  0x00, 0x00, 0x00, 0x7c, 0x00, 0x00, 0x00, 0x00, 0xee, 0x00, 0x00, 0x00,
  0x00, 0xbb, 0x01, 0x00, 0x00, 0x00, 0x7d, 0x03, 0x00, 0x00, 0x80, 0xc7,
  0x03, 0x00, 0x00, 0x80, 0x87, 0x02, 0x00, 0x00, 0x80, 0xc7, 0x03, 0x00,
  0x00, 0x00, 0x6d, 0x01, 0x00, 0x00, 0x00, 0x7b, 0x01, 0x00, 0x00, 0x00,
  0x83, 0x01, 0x00, 0x00, 0x00, 0x86, 0x00, 0x00, 0x00, 0x00, 0xc4, 0x00,
  0x00, 0x00, 0x00, 0x6c, 0x00, 0x00, 0x00, 0x00, 0x68, 0x00, 0x00, 0x00,
  0x00, 0x38, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x20, 0x00, 0x00, 0x10,
  0x00, 0xfc, 0x01, 0x00, 0x00, 0x00, 0x06, 0x03, 0x00, 0x1e, 0x00, 0xfa,
  0x02, 0xe0, 0x0f, 0x00, 0xdf, 0x06, 0x7f, 0x00, 0x00, 0x8d, 0xc7, 0x07,
  0x00, 0x00, 0x85, 0x07, 0x3f, 0x00, 0x00, 0x8f, 0x07, 0xf8, 0x01, 0x00,
  0xfa, 0x02, 0x80, 0x1f, 0x00, 0x26, 0x03, 0x00, 0x7e, 0x00, 0x04, 0xe1,
  0xff, 0x7f, 0x00, 0x8c, 0xf1, 0x00, 0x00, 0x00, 0x88, 0xc0, 0x00, 0x00,
  0x00, 0xd8, 0x80, 0x01, 0x00, 0x00, 0x70, 0x00, 0x03, 0x00, 0x00, 0x30,
  0x00, 0x06, 0x00, 0x00, 0x20, 0xff, 0x07, 0x00, 0x00, 0x80, 0x1f, 0x00,
  0x00, 0x00
};
void setup()   {
 
  pinMode(reedPin, INPUT_PULLUP);   // Reed-Sensor an Pin D3
  pinMode(buttonPin, INPUT_PULLUP); // Taster an Pin D2
  EIFR = (1 << INTF1);                                        //verhindert fälschliches Aufrufen der ISR revs() bei Startup --> https://github.com/arduino/Arduino/issues/510
  attachInterrupt(digitalPinToInterrupt(reedPin), revs, RISING);
  getMileage();                                   //gespeicherten Tachostand abrufen (eeprom-adresse mit höchstem Zählerstand suchen)
  myRA.clear();
  button.attachClick(klick);
  button.attachLongPressStop(langDruck);
  u8g2.begin();
  u8g2_prepare();
  u8g2.clearBuffer();
  u8g2.drawXBMP( 0, 0, logo_width, logo_height, logo);
  u8g2.sendBuffer();
  delay(2000);
}
void loop() {
  button.tick();
  u8g2.clearBuffer();
  if (MotorradFaehrt) {
    myRA.addValue(geschwindigkeit);           // Wert zur Mittelwerberechnung hinzufügen
    GesamtMillimeter += RadUmfang;
    TripMillimeter += RadUmfang;
    MotorradFaehrt = false;
    if (geschwindigkeit > 15) beimNaechstenStopDatenSichern = true;   //Flag setzen um Tachostand beim nächsten Stop wegzuschreiben
  }
  if (millis() - lastMillis >= 30000) {               //wenn keine Radumdrehung innerhalb von 30 Sek stattgefunden hat --> Logo zeigen
    u8g2.drawXBMP( 0, 0, logo_width, logo_height, logo);
    ShowLogo = true;
    if (beimNaechstenStopDatenSichern) {            //Tachostand nur dann ins EEPROM schreiben, wenn vorher über 15km/h gefahren wurde
      saveMileage();
      beimNaechstenStopDatenSichern = false;
    }
  } else {
    draw();
  }
  u8g2.sendBuffer();
  if (millis() - lastMillis >= 3000) {
    myRA.clear();                                   //wenn keine Radumdrehung innerhalb von 3 Sek stattgefunden hat --> Geschwindigkeit nullen
  }
  if (RotationCounter >= 10) {                      //Tachostand ca. alle 10 umdrehungen sichern
    RotationCounter = 0;
    saveMileage();
  }
}
void draw() {
  short VAnzeige = myRA.getAverage();        // Mittelwert errechnen
  int hunderter = (TripMillimeter / 100000) % 10;
  int tausender = TripMillimeter / 1000000;
  sprintf(GesamtKm, "%6lukm", (uint32_t)(GesamtMillimeter / 1000000));
  sprintf(TripKm, "%5d.%dkm", tausender, hunderter);
  sprintf(Speed, "%3d", VAnzeige);
  switch (screen) {
    case 0:
      u8g2.drawXBMP( 80, 0, tachoSymbol_width, tachoSymbol_height, tachoSymbol);
      u8g2.setFont(u8g2_font_logisoso38_tn);
      u8g2.drawStr( 0, 15, Speed);
      u8g2.setFont(u8g2_font_logisoso20_tr);
      u8g2.drawStr( 75, 32, "kmh");
      break;
    case 1:
      u8g2.setFont(u8g2_font_logisoso20_tr);
      u8g2.drawStr( 0, 10, "Trip:");
      u8g2.drawStr( 0, 40, TripKm);
      u8g2.drawXBMP( 80, 0, route_width, route_height, route_bits);
      break;
    case 2:
      u8g2.drawStr( 0, 40, TripKm);
      u8g2.drawStr( 5, 10, GesamtKm);
      break;
  }
}
void u8g2_prepare(void) {
  u8g2.setFontRefHeightExtendedText();
  u8g2.setDrawColor(1);
  u8g2.setFontPosTop();
  u8g2.setFontDirection(0);
}
void revs() {                           // Interrupt-Routine
  uint32_t jetztMillis = millis();
  if (jetztMillis - lastMillis >= Entprellzeit) {                               // ignorieren wenn <= 20ms (Kontaktpreller)
    geschwindigkeit = RadUmfang * 3.6 / (jetztMillis - lastMillis);             // Geschwindigkeit: 1 Interrupt bei jeder Radumdrehung
    lastMillis = millis();
    RotationCounter += 1;
    MotorradFaehrt = true;
  }
}
void klick() {   //durchtoggeln der Screens
  screen++;
  if (screen > 2) screen = 0;
  if (ShowLogo) {  //falls Logo angezeigt und Knopf gedrückt wird --> Logo-Timer wieder auf 30sek setzen und Geschwindigkeit anzeigen
    ShowLogo = false;
    lastMillis = millis();
    screen = 0;
  }
}
void langDruck() {
  if (screen == 2)  {
    TripMillimeter = 0;   //Trip zurücksetzen
    EEPROM.put(TripMillimeter_ADDR, TripMillimeter);
  }
}
void getMileage() {     //EEPROM-Adresse mit dem höchsten Tachostand suchen
  uint64_t GesamtMillimeter_vgl;
  EEPROM.get(GesamtMillimeter_ADDR, GesamtMillimeter);    //Absprungbasis bei Adresse=0
  for (int pos = 0; pos <= 1008; pos = pos + 12) { //EEProm.length = 0-1023
    EEPROM.get(pos, GesamtMillimeter_vgl);
    if (GesamtMillimeter_vgl > GesamtMillimeter) {  //falls höherer KM-Stand gefunden wurde, diesen übernehmen und weitersuchen
      GesamtMillimeter = GesamtMillimeter_vgl;
      GesamtMillimeter_ADDR =  pos;
    }
  }
  TripMillimeter_ADDR = GesamtMillimeter_ADDR + 8;
  EEPROM.get(GesamtMillimeter_ADDR, GesamtMillimeter);
  EEPROM.get(TripMillimeter_ADDR, TripMillimeter);
}
void saveMileage() {
  GesamtMillimeter_ADDR += 12;                  //immer in neue Zelle schreiben --> verlängert EEPROM Lebensdauer (Gesamtmillimeter 8 Byte, TripMillimeter 4 Byte -> Sprung um 12Byte
  if (GesamtMillimeter_ADDR > 1008) {
    GesamtMillimeter_ADDR = 0;
  }
  TripMillimeter_ADDR = GesamtMillimeter_ADDR + 8;              //Gesamtmillimeter ist 64bit Zahl --> braucht 8 Byte im EEPROM
  EEPROM.put(GesamtMillimeter_ADDR, GesamtMillimeter);
  EEPROM.put(TripMillimeter_ADDR, TripMillimeter);
}
			
	
					