Hello,
for learning purpose step by step, I started to program my own RTC time clock with a Rotary encoder to set time. (Long click to go from "show the time to set the time", short click to validate)
the thing is when I test the encoder by itself, it works fine. But embedded in my code, I have instability with the encoder.
Can you tell me how to add a debounce code in my code (WITHOUT USING DELAY) ?
many thx.
#include <RTClib.h>
#include <TM1637Display.h>
#include <OneButton.h>
#include <Encoder.h>
#define encSwitch 3
#define pinA 6
#define pinB 7
#define clkDisplayCLK 10
#define clkDisplayDIO 11
DateTime now;
OneButton button(encSwitch, true);
RTC_DS3231 rtc;
Encoder myEnc(pinA,pinB);
TM1637Display clkDisplay(clkDisplayCLK, clkDisplayDIO);
uint8_t blinkT[] = {0x00, 0x00, 0x00, 0x00};
unsigned int oldPosition = 0;
unsigned int newPosition = 0;
byte menuState = 0;
uint8_t clear_segment[] = {0x00, 0x00, 0x00, 0x00}; // declaring array for clearing
void menuFunc() { //simple click on encoderswitch
if (menuState ==1){
menuState ++;
}
else if (menuState == 2) {
menuState = 0;
}
else {
menuState = 0;
}
}
void longPressStart() { // long click for time change
if (menuState == 0) {
menuState ++;
}
else{
menuState = 0;
}
}
void setup() {
// put your setup code here, to run once:
pinMode(encSwitch, INPUT_PULLUP);
clkDisplay.setSegments(clear_segment);
button.attachClick(menuFunc);
button.attachLongPressStart(longPressStart);
button.setDebounceTicks(80);
clkDisplay.setBrightness(7);
rtc.begin();
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
}
void loop() {
// put your main code here, to run repeatedly:
button.tick();
now = rtc.now();
if (menuState == 0) { // show time with colon blink
if ((millis()/1000) % 2 == 0) {
clkDisplay.showNumberDecEx((now.hour() * 100) + now.minute(), 0x80 >> 1, 1, 4, 0);
}
else {
clkDisplay.showNumberDecEx((now.hour() * 100) + now.minute(), 0, 1, 4, 0);
}
}
else if (menuState == 1) { // change hour with hour digits blinking
myEnc.write(now.hour());
newPosition = myEnc.read();
if (newPosition != oldPosition) {
oldPosition = newPosition;
}
rtc.adjust(DateTime(now.year(), now.month(), now.day(), newPosition, now.minute(), now.second()));
if ((millis()/1000) % 2 == 0) {
clkDisplay.showNumberDecEx((now.hour() * 100) + now.minute(), 0x80 >> 1, 1, 4, 0);
}
else {
if (now.minute()<10) {
blinkT[0] = 0x00;
blinkT[1] = 0x00;
blinkT[2] = clkDisplay.encodeDigit(0);
blinkT[3] = clkDisplay.encodeDigit(now.minute());
clkDisplay.setSegments(blinkT);
}
else {
clkDisplay.showNumberDecEx(now.minute(), 0x80 >> 1, false, 4, 0);
}
}
}
else if (menuState == 2) { // change minutes with minutes digits blinking
myEnc.write(now.minute());
newPosition = myEnc.read();
if (newPosition != oldPosition) {
oldPosition = newPosition;
}
rtc.adjust(DateTime(now.year(), now.month(), now.day(), now.hour(), newPosition, now.second()));
now = rtc.now();
if ((millis()/1000) % 2 == 0) {
clkDisplay.showNumberDecEx((now.hour() * 100) + now.minute(), 0x80 >> 1, 1, 4, 0);
}
else {
blinkT[0] = clkDisplay.encodeDigit((now.hour()-(now.hour()%10))/10);
blinkT[1] = clkDisplay.encodeDigit(now.hour()%10);
blinkT[2] = 0x00;
blinkT[3] = 0x00;
clkDisplay.setSegments(blinkT);
}
}
}