#include <Wire.h>
#include <OneButtonTiny.h>
#include <LiquidCrystal_I2C.h>
#include <EEPROM.h>
#include <Debouncer.h>
#include <SSD1306Ascii.h>
#include <SSD1306AsciiWire.h>
#define I2C_ADDRESS 0x3C
#define INCLUDE_SCROLLING 1
#define INITIAL_SCROLL_MODE SCROLL_MODE_AUTO
SSD1306AsciiWire oled;
#define TRIGGER_PIN 2 //10
#define ECHO_PIN 3 //11
#define ENDSCH_PIN 5 //5
#define TAST_PIN 6 //6
#define RELAIS_PIN 7 //4
#define LED_ZUHAUSEPIN 8 //5
unsigned long previousMillis = 0; // Store old millis
boolean bHalbSekTakt = false;
boolean bHalbSekPuls = false;
boolean bSekTakt = false;
boolean bSekPuls = false;
bool bEndsch;
int iHalbSekZaehler;
int duration_ms = 50;
Debouncer debouncer(ENDSCH_PIN, duration_ms);
OneButtonTiny TastOB = OneButtonTiny(
TAST_PIN, // Input pin for the button
true, // Button is active LOW
true // Enable internal pull-up resistor
);
LiquidCrystal_I2C lcd(0x27,16,2); // set the LCD address to 0x27 for a 16 chars and 2 line display
void setup() {
Wire.begin();
Wire.setClock(400000L);
oled.begin(&Adafruit128x64, I2C_ADDRESS);
pinMode(LED_BUILTIN, OUTPUT);
pinMode(TRIGGER_PIN, OUTPUT);
pinMode(ECHO_PIN, INPUT);
pinMode(RELAIS_PIN, OUTPUT); //Relais
pinMode(LED_ZUHAUSEPIN, OUTPUT); //Relais
pinMode(TAST_PIN, INPUT_PULLUP);
pinMode(ENDSCH_PIN, INPUT_PULLUP);
Serial.begin(9600);
/*
lcd.init();
lcd.backlight();
delay(250);
lcd.noBacklight();
delay(100);
lcd.backlight();
delay(100);
*/
// Single Click event attachment
TastOB.attachClick(handleClickTast);
// Long Press Start event attachment
TastOB.attachLongPressStart(handleLongPressedTast);
// Double Click event attachment with lambda
TastOB.attachDoubleClick(handleDoubleClickTast);
delay(100);
// lcd.noBacklight();
// bLcdBacklight = false;
oled.setFont(System5x7); // Auswahl der Schriftart
// oled.setFont(fixed_bold10x15); // Auswahl der Schriftart
// oled.setFont(fixednums15x31); // Auswahl der Schriftart
oled.clear(); //Löschen der aktuellen Displayanzeige
oled.setScrollMode(SCROLL_MODE_AUTO);
// oled.println("123 Liter"); //Text in der ersten Zeile. "Println" sorgt dabei für einen Zeilensprung.
// oled.print("45"); // Text in der zweiten Zeile. Da es keine dritte Zeile gibt, wird hier kein Zeilenumsprung benötigt.
// oled.set1X();
// oled.set2X();
/*
for (int i = 0; i <= 20; i++) {
if (i == 10) {
oled.clear();
}
oled.print("Line ");
oled.println(i);
delay(500);
}
// don't scroll last line.
oled.print("Done");
*/
oled.setFont(Adafruit5x7);
uint32_t m = micros();
oled.clear();
// first row
oled.println("set1X test");
// second row
oled.set2X();
oled.println("set2X test");
// third row
oled.set1X();
oled.print("micros: ");
oled.print(micros() - m);
}
void loop() {
return;
oled.print("123 Liter"); //Text in der ersten Zeile. "Println" sorgt dabei für einen Zeilensprung.
oled.set2X();
for (int i = 0; i < 4; i++) {
// Toggle invert mode for next line of text.
oled.setInvertMode(i%2);
oled.print("\r123 Liter");
delay(500);
}
for (int i = 0; i < 4; i++) {
// Invert all text on screen.
oled.invertDisplay(!(i%2));
delay(1000);
}
int iAbstand;
bool bTaster;
char myDecString1[17];
char myDecString2[17];
debouncer.update(); // you should update debouncer first
if (debouncer.edge()) { // if edge is detected
if (debouncer.rising()) // if edge is rising
bEndsch = false;
Serial.println("rise");
if (debouncer.falling()) // if edge is falling
bEndsch = true;
Serial.println("fall");
}
TastOB.tick();
sekundenZaehlen();
bTaster = !digitalRead(TAST_PIN);
digitalWrite(LED_BUILTIN, bTaster);
if (bHalbSekPuls) {
iAbstand = AbstandMessen();
}
bHalbSekPuls = false;
bSekPuls = false;
}
//============== Taster
static void handleClickTast() {
Serial.println("Clicked!");
int iVal;
//readEEPROM();
//return;
}
static void handleLongPressedTast() {
}
static void handleDoubleClickTast() { //Doppelklick: Einstellungen +- umschalten oder im Standard-Anzeige-Modus: Relais ein/ausschalten
Serial.println("Doubleclicked");
}
void sekundenZaehlen() {
unsigned long currentMillis = millis();
if ((currentMillis - previousMillis) > 500) {
previousMillis = currentMillis;
// DigiKeyboard.println("Halbe Sekunde");
bHalbSekPuls = true;
bHalbSekTakt = !bHalbSekTakt;
iHalbSekZaehler += 1;
if (iHalbSekZaehler == 2) {
iHalbSekZaehler = 0;
bSekTakt = !bSekTakt;
bSekPuls = true;
} else {
return;
}
}
//Sekunden runterzählen
// DigiKeyboard.println("Sekunde");
}
//============== Abstand
int AbstandMessen() {
int iDistance;
long duration;
digitalWrite(TRIGGER_PIN, LOW);
delayMicroseconds(2);
digitalWrite(TRIGGER_PIN, HIGH); //Ping
delayMicroseconds(10); //Need to futs with this value now to make it work better on digispark
digitalWrite(TRIGGER_PIN, LOW);
// iDistance = pulseIn(ECHO_PIN, HIGH,10000L) / 58;
duration = pulseIn(ECHO_PIN, HIGH,10000L);
iDistance = duration * 0.034 / 2;
if (iDistance < 1) iDistance = 99;
// Serial.println(duration);
// iDistance = duration * 0.034 / 2;
/// iDistance = microsecondsToCentimeters(duration);
// duration = pulseIn(ECHO_PIN, HIGH);
// iDistance = microsecondsToCentimeters(duration);
return iDistance;
}
//============== LED
void LedOnOffBlink(int iMode, int iPin) { //0: aus 1: ein 2: langsam 3: schnell
if (iMode == 0) {
digitalWrite(iPin, LOW);
}
else if (iMode == 1) {
digitalWrite(iPin, HIGH);
}
else if (iMode == 2) {
digitalWrite(iPin, bSekTakt);
}
else if (iMode == 3) {
digitalWrite(iPin, bHalbSekTakt);
}
}