#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <Bounce2.h>
#include <Ticker.h>
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define BTN 13
Adafruit_SSD1306 display(SCREEN_WIDTH,SCREEN_HEIGHT,&Wire,-1);
char dataIn;
float circumfrance = 1.2767;
int prevTime;
int timeout;
float time;
float v;
int cnt;
bool messageEnd = true;
Bounce btn = Bounce();
Bounce safe = Bounce();
static float safedData[60];
int sDLength = sizeof(safedData)/sizeof(safedData[0]);
int speicherPos = 0;
bool nullen = false;
void arrayNullen ()
{
for (int i = 0; i <= sDLength;i++)
{
safedData[i] = 0;
}
}
void werteSpeichern (float v)
{
safedData[speicherPos] = v;
speicherPos++;
if (speicherPos >= 60)
{
speicherPos = 0;
}
}
void anzeigen (float v)
{
display.clearDisplay();
display.setCursor(0,10);
display.print("km/h:");
display.setCursor(0,40);
display.print(v);
display.display();
}
void vMessen ()
{
nullen = false;
if (prevTime > 0)
{
timeout = millis();
time = (float)(millis()-prevTime)/1000;
v = circumfrance/time*3.6;
anzeigen(v);
//Serial.println(speicherPos);
werteSpeichern(v);
}
prevTime = millis();
}
void vRuecksetzen()
{
if (millis()-prevTime > 6000 && !nullen)
{
v = 0;
anzeigen(v);
werteSpeichern(v);
nullen = true;
}
}
void arrayAusgeben ()
{
for (int j = 0; j < 60; j++)
{
Serial.println(safedData[j]);
}
}
Ticker t1(vMessen, 10);
Ticker t2(vRuecksetzen,100);
Ticker t3(arrayAusgeben,1000);
void setup()
{
Serial.begin(9600);
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
display.clearDisplay();
display.setTextSize(2);
display.setTextColor(WHITE);
display.setCursor(0,10);
btn.attach( BTN , INPUT_PULLUP );
btn.interval(10);
safe.attach( 12 , INPUT_PULLUP );
safe.interval(10);
t1.start();
t2.start();
t3.start();
arrayNullen ();
}
void loop()
{
btn.update();
safe.update();
if (btn.fell())
{
t1.update();
}
t2.update();
if (safe.fell())
{
t3.update();
}
}