#include <RunningAverage.h>
#include <Wire.h>
#include "OneButton.h"
#include <U8g2lib.h>
#include <dht.h>
U8G2_SSD1306_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0, /* reset=*/ U8X8_PIN_NONE);
#define DHT22_PIN 5 // DHT22 sensor
dht DHT;
int hum;
int temp;
const uint16_t Entprellzeit = 13; // in ms
volatile uint32_t lastMillis = 0;
RunningAverage myRA(7);
volatile short geschwindigkeit;
const int buttonPin = 4;
const int reedPin = 2;
int screen = 0;
volatile int RotationCounter = 0;
OneButton button(buttonPin, true);
const int RadUmfang = 1862;
char Speed[10];
boolean reset = false;
boolean MotorradFaehrt = false;
boolean ShowLogo = false;
boolean beimNaechstenStopDatenSichern = false;
#define logo_width 123
#define logo_height 64
const unsigned char logo[] PROGMEM = {
};
#define ha_width 49
#define ha_height 38
const unsigned char ha[] PROGMEM = {
0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0xC0, 0x07, 0x00,
0x00, 0x00, 0x00, 0x00, 0xC0, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00, 0xC0,
0x07, 0x00, 0x00, 0x00, 0x00, 0x00, 0xC0, 0x07, 0x00, 0x00, 0x00, 0x00,
0x00, 0xC0, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00, 0xC0, 0x07, 0x00, 0x00,
0x00, 0x00, 0x00, 0xC0, 0xFF, 0x1F, 0x00, 0x00, 0x00, 0x00, 0xC0, 0xFF,
0x1F, 0x00, 0x00, 0x00, 0x00, 0xC0, 0xFF, 0x1F, 0x00, 0x00, 0x00, 0x00,
0xC0, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00, 0xC0, 0x07, 0x00, 0x00, 0x00,
0x00, 0x00, 0xC0, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00, 0xC0, 0x07, 0x00,
0x00, 0x00, 0x00, 0x00, 0xC0, 0xFF, 0x1F, 0x00, 0x00, 0x00, 0x00, 0xC0,
0xFF, 0x1F, 0x00, 0x00, 0x00, 0x00, 0xC0, 0xFF, 0x1F, 0x00, 0x00, 0x00,
0x00, 0xC0, 0x07, 0x08, 0x00, 0x00, 0x00, 0x00, 0xC0, 0x07, 0x00, 0x00,
0x00, 0x00, 0x00, 0xC0, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00, 0xC0, 0x07,
0x00, 0x00, 0x00, 0x00, 0x00, 0xC0, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00,
0xC0, 0xFF, 0x1F, 0x00, 0x00, 0x00, 0x00, 0xC0, 0xFF, 0x1F, 0x00, 0x00,
0x00, 0x00, 0xC0, 0xFF, 0x1F, 0x00, 0x00, 0x00, 0x00, 0xC0, 0x07, 0x00,
0x00, 0x00, 0x00, 0x00, 0xC0, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00, 0xC0,
0x07, 0x00, 0x00, 0x00, 0x00, 0x00, 0xC0, 0x07, 0x00, 0x00, 0x00, 0x78,
0x00, 0xE0, 0x0F, 0x00, 0x1E, 0x00, 0xFE, 0x03, 0xF0, 0x0F, 0x80, 0xFF,
0x00, 0xFF, 0xFF, 0xF0, 0x1F, 0xFE, 0xFF, 0x01, 0xFF, 0xFF, 0xF0, 0x1F,
0xFE, 0xFF, 0x01, 0x07, 0xFF, 0xF0, 0x1F, 0xFE, 0xC1, 0x01, 0x00, 0xFC,
0xF0, 0x0F, 0x3E, 0x00, 0x00, 0x00, 0x00, 0xE0, 0x0F, 0x00, 0x00, 0x00,
0x00, 0x00, 0xC0, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, };
void setup() {
// Serial.begin(9600); // open the serial port at 9600 bps:
pinMode(reedPin, INPUT_PULLUP);
pinMode(buttonPin, INPUT_PULLUP);
EIFR = (1 << INTF1);
attachInterrupt(digitalPinToInterrupt(reedPin), revs, FALLING);
myRA.clear();
button.attachClick(klick);
u8g2.begin();
u8g2_prepare();
u8g2.clearBuffer();
u8g2.drawXBMP( 7, 0, logo_width, logo_height, logo);
u8g2.sendBuffer();
}
void loop() {
button.tick();
dth();
u8g2.clearBuffer();
if (MotorradFaehrt) {
myRA.addValue(geschwindigkeit);
MotorradFaehrt = false;
if (geschwindigkeit > 30) beimNaechstenStopDatenSichern = true;
}
if (micros() - lastMillis >= 10000000000) {
u8g2.drawXBMP( 7, 0, logo_width, logo_height, logo);
ShowLogo = true;
if (beimNaechstenStopDatenSichern) {
beimNaechstenStopDatenSichern = false;
}
} else {
draw();
}
u8g2.sendBuffer();
if (micros() - lastMillis >= 1000) {
myRA.clear();
}
if (RotationCounter >= 10) {
RotationCounter = 0;
}
}
void dth() {
int chk = DHT.read22(DHT22_PIN);
hum = DHT.humidity;
temp= DHT.temperature;
}
void draw() {
short VAnzeige = myRA.getAverage(); // Mittelwert errechnen
sprintf(Speed, "%3d", VAnzeige);
switch (screen) {
case 1:
u8g2.setFont(u8g2_font_logisoso38_tn);
u8g2.drawStr( 0, 15, Speed);
u8g2.setFont(u8g2_font_logisoso20_tr);
u8g2.drawStr( 75, 32, "kmh");
break;
case 0:
u8g2.setFont(u8g2_font_logisoso20_tr);
u8g2.setCursor(90, 40);
u8g2.println("C");
u8g2.setCursor(45, 40);
u8g2.print(temp);
u8g2.drawXBMP( 40, 0, ha_width, ha_height,ha);
break;
}
}
void u8g2_prepare(void) {
u8g2.setFontRefHeightExtendedText();
u8g2.setDrawColor(1);
u8g2.setFontPosTop();
u8g2.setFontDirection(0);
}
void revs() { // Interrupt-Routine
uint32_t jetztMillis = micros();
if (jetztMillis - lastMillis >= Entprellzeit) {
geschwindigkeit = RadUmfang * 200.200 / (jetztMillis - lastMillis);
lastMillis = micros();
RotationCounter += 1;
MotorradFaehrt = true;
}
}
void klick() { //durchtoggeln der Screens
screen++;
if (screen > 1) screen = 0;
if (ShowLogo) { //falls Logo angezeigt und Knopf gedrückt wird --> Logo-Timer wieder auf 10sek setzen und Geschwindigkeit anzeigen
ShowLogo = false;
lastMillis = micros();
screen = 0;
}
}
Hello, I want to provide temperature display in the km counter section.
The necessary codes are above, but there is an error when the km counting process starts, that is, when the 2nd pin is reed, there are constant changes in temperature and efficiency, but when there is no input on the 2nd pin, everything works normally.
When the km counting process I want is done, I want to display it without changing the degree part. How should I make a change in the software?
Thank you to those who have already replied.