Op 30 augustus 2020 14:51:04 schreef SvenTech:
heb nu dit geschreven met de debounce functie:code:
#include "Countimer.h" int btnhours = 2; //button voor instellen uren int btnminutes = 3; //button voor instellen minuten int btnseconds = 4; //button voor instellen secondes int btnstart = 5; //Start button int hourvalue = 1; //uren int minutevalue = 0; //minuten int secondsvalue = 0; //seconden int readinghours = 0; int readingminutes = 0; int readingseconds = 0; int readingstart = 0; int relaypin = 6; //Pin naar relais int btnstatehours; int btnstateminutes; int btnstateseconds; int btnstatestart; int lastbtnstatehours = LOW; int lastbtnstateminutes = LOW; int lastbtnstateseconds = LOW; int lastbtnstatestart = LOW; unsigned long lastDebouncehours = 0; unsigned long lastDebounceminutes = 0; unsigned long lastDebounceseconds = 0; unsigned long lastDebouncestart = 0; unsigned long debouncedelay = 50; #include <SPI.h> #include <Wire.h> #include <Adafruit_GFX.h> #include <Adafruit_SSD1306.h> #define OLED_RESET 4 Adafruit_SSD1306 display(OLED_RESET); Countimer timer; void setup() { display.begin(SSD1306_SWITCHCAPVCC, 0x3C); display.clearDisplay(); pinMode(btnhours, INPUT); //ingang voor uren button pinMode(btnminutes, INPUT); //ingang voor minuten button pinMode(btnseconds, INPUT); //ingang voor seconden button pinMode(btnstart, INPUT); //ingang voor startbutton pinMode(relaypin, OUTPUT); //uitgang naar relais Serial.begin(9600); //begin serial monitor timer.setCounter(hourvalue, minutevalue, secondsvalue, timer.COUNT_DOWN, onComplete); //instellen timer // Print current time every 1s on serial port by calling method refreshClock(). timer.setInterval(refreshClock, 1000); } void refreshClock() { Serial.print("Current count time is: "); Serial.println(timer.getCurrentTime()); display.setTextSize(1); display.setTextColor(WHITE); display.setCursor( 40, 20 ); display.print(timer.getCurrentTime()); display.display(); display.clearDisplay(); } void onComplete() { Serial.println("Complete!!!"); display.setTextSize(1); display.setTextColor(WHITE); display.setCursor( 40, 20 ); display.print("DONE"); display.display(); display.clearDisplay(); digitalWrite(relaypin, LOW); //hier zetten we de motor uit } void loop() { // Run timer readinghours = digitalRead(btnhours); readingminutes= digitalRead(btnminutes); readingseconds = digitalRead(btnseconds); readingstart = digitalRead(btnstart); timer.run(); if (readinghours != lastbtnstatehours) { // reset the debouncing timer //debounce voor de uren button lastDebouncehours = millis(); } if ((millis() - lastbtnstatehours) > debouncedelay) { if (readinghours != btnstatehours) { btnstatehours = readinghours; } } if (btnstatehours = HIGH) { (hourvalue++); } if (readingminutes != lastbtnstateminutes) { // reset the debouncing timer //debounce voor de minutes button lastDebounceminutes = millis(); } if ((millis() - lastbtnstateminutes) > debouncedelay) { if (readingminutes!= btnstateminutes) { btnstateminutes = readingminutes; } } if (btnstateminutes = HIGH) { (minutevalue++); } if (readingseconds != lastbtnstateseconds) { // reset the debouncing timer //debounce voor de seconds button lastDebounceseconds = millis(); } if ((millis() - lastbtnstateseconds) > debouncedelay) { if (readingseconds!= btnstateseconds) { btnstateseconds = readingseconds; } } if (btnstateseconds = HIGH) { (secondsvalue++); } if (hourvalue >= 99) { hourvalue = 0; //max uren begrenzen } if (minutevalue >= 59) { minutevalue = 0; //max minuten begrenzen } if (secondsvalue >= 59) { secondsvalue = 0; //max secondes begrenzen } if (readingstart != lastbtnstatestart) { // reset the debouncing timer //debounce voor de start button lastDebouncestart = millis(); } if ((millis() - lastbtnstatestart) > debouncedelay) { if (readingstart != btnstatestart) { btnstatestart = readingstart; } } if (readingstart = HIGH){ timer.start(); digitalWrite(relaypin, HIGH); //hier zetten we de motor aan } }