#include <LedControl.h>
#include <iarduino_RTC.h>
#include <DHT11.h>
#include <Wire.h>
#include <VirtualButton.h>
#define VB_DEB 50
#define VB_CLICK 100
struct MyBtn : public VButton {
MyBtn(uint8_t pin) {
_pin = pin;
pinMode(_pin, INPUT_PULLUP);
}
bool tick() {
return poll(!digitalRead(_pin));
}
uint8_t _pin;
};
MyBtn setButt(2);
MyBtn upButt(3);
MyBtn downButt(4);
LedControl LC = LedControl(12, 11, 10, 3);
iarduino_RTC watch(RTC_DS3231);
DHT11 dht11(5);
const int brightSetPin = A0;
const int dividerDotsPin = 6;
int dividerDotsState = LOW;
int mode = 0;
long previousMillis = 0;
unsigned long buttonTimer = 0;
void ButtonSetControl();
void ShowTime();
void Divider();
void setup() {
delay(300);
Serial.begin(9600);
watch.begin();
Wire.begin();
for (int ledS = 0; ledS < 3; ledS++) {
LC.shutdown(ledS, false);
LC.clearDisplay(ledS);
}
pinMode(dividerDotsPin, OUTPUT);
//watch.settime(0,28,18,10,1,24,2) ;
}
void loop() {
static byte tik = 0;
if (millis() - buttonTimer > 20) {
buttonTimer = millis();
setButt.tick();
upButt.tick();
downButt.tick();
ButtonSetControl();
if (tik >= 30) {
watch.gettime();
Divider();
ShowTime();
tik = 0;
}
tik++;
}
Serial.println(mode);
}
void ButtonSetControl() {
watch.blinktime(mode, 2);
if ((setButt.click()) || (setButt.step())) {
mode++;
if (mode > 7) { mode = 0; }
}
if (mode) {
if ((upButt.click()) || (upButt.step())) {
switch (mode) {
/* сек */ case 1:
watch.settime(0, -1, -1, -1, -1, -1, -1);
break;
/* мин */ case 2:
watch.settime(-1, (watch.minutes == 59 ? 0 : watch.minutes + 1), -1, -1, -1, -1, -1);
break;
/* час */ case 3:
watch.settime(-1, -1, (watch.Hours == 23 ? 0 : watch.Hours + 1), -1, -1, -1, -1);
break;
/* дни */ case 4:
watch.settime(-1, -1, -1, (watch.day == 31 ? 1 : watch.day + 1), -1, -1, -1);
break;
/* мес */ case 5:
watch.settime(-1, -1, -1, -1, (watch.month == 12 ? 1 : watch.month + 1), -1, -1);
break;
/* год */ case 6:
watch.settime(-1, -1, -1, -1, -1, (watch.year == 99 ? 0 : watch.year + 1), -1);
break;
/* д.н.*/ case 7:
watch.settime(-1, -1, -1, -1, -1, -1, (watch.weekday == 6 ? 0 : watch.weekday + 1));
break;
}
}
if ((downButt.click()) || (downButt.step())) {
switch (mode) {
/* сек */ case 1:
watch.settime(0, -1, -1, -1, -1, -1, -1);
break;
/* мин */ case 2:
watch.settime(-1, (watch.minutes == 0 ? 59 : watch.minutes - 1), -1, -1, -1, -1, -1);
break;
/* час */ case 3:
watch.settime(-1, -1, (watch.Hours == 0 ? 23 : watch.Hours - 1), -1, -1, -1, -1);
break;
/* дни */ case 4:
watch.settime(-1, -1, -1, (watch.day == 1 ? 31 : watch.day - 1), -1, -1, -1);
break;
/* мес */ case 5:
watch.settime(-1, -1, -1, -1, (watch.month == 1 ? 12 : watch.month - 1), -1, -1);
break;
/* год */ case 6:
watch.settime(-1, -1, -1, -1, -1, (watch.year == 0 ? 99 : watch.year - 1), -1);
break;
/* д.н.*/ case 7:
watch.settime(-1, -1, -1, -1, -1, -1, (watch.weekday == 0 ? 6 : watch.weekday - 1));
break;
}
}
}
}
void ShowTime() {
int temperature = dht11.readTemperature();
int humidity = dht11.readHumidity();
int brightSetVol = analogRead(brightSetPin) / 50;
if (brightSetVol < 0) { brightSetVol = 2; }
for (int ledS = 0; ledS < 3; ledS++) {
LC.setIntensity(ledS, brightSetVol);
}
LC.setDigit(0, 0, watch.Hours / 10, false);
LC.setDigit(0, 1, watch.Hours % 10, true);
LC.setDigit(0, 2, watch.minutes / 10, false);
LC.setDigit(0, 3, watch.minutes % 10, true);
LC.setDigit(0, 4, watch.seconds / 10, false);
LC.setDigit(0, 5, watch.seconds % 10, false);
switch (watch.weekday) {
case 0: // ПН
LC.setRow(0, 6, 0b01110110);
LC.setRow(0, 7, 0b00110111);
break;
case 1: // ВТ
LC.setRow(0, 6, 0b01111111);
LC.setRow(0, 7, 0b00001111);
break;
case 2: // СР
LC.setRow(0, 6, 0b01001110);
LC.setRow(0, 7, 0b01100111);
break;
case 3: // ЧТ
LC.setRow(0, 6, 0b00110011);
LC.setRow(0, 7, 0b00001111);
break;
case 4: // ПТ
LC.setRow(0, 6, 0b01110110);
LC.setRow(0, 7, 0b00001111);
break;
case 5: // СБ
LC.setRow(0, 6, 0b01001110);
LC.setRow(0, 7, 0b00011111);
break;
case 6: // НД
LC.setRow(0, 6, 0b00110111);
LC.setRow(0, 7, 0b00111101);
break;
}
LC.setDigit(1, 0, watch.day / 10, false);
LC.setDigit(1, 1, watch.day % 10, true);
LC.setDigit(1, 2, watch.month / 10, false);
LC.setDigit(1, 3, watch.month % 10, true);
LC.setDigit(1, 4, 2, false);
LC.setDigit(1, 5, 0, false);
LC.setDigit(1, 6, watch.year / 10, false);
LC.setDigit(1, 7, watch.year % 10, false);
if (temperature < 0) {
LC.setRow(2, 0, 0b0000001);
}
LC.setDigit(2, 1, temperature / 10, false);
LC.setDigit(2, 2, temperature % 10, false);
LC.setRow(2, 3, 0b01100011);
LC.setRow(2, 4, 0b01001110);
LC.setDigit(2, 5, humidity / 10, false);
LC.setDigit(2, 6, humidity % 10, false);
LC.setRow(2, 7, 0b00110111);
}
void Divider() {
unsigned long currentMillis = millis();
int interval = 500;
if (mode == 0) {
if (currentMillis - previousMillis >= interval) {
previousMillis = currentMillis;
dividerDotsState = !dividerDotsState;
digitalWrite(dividerDotsPin, dividerDotsState);
}
if (mode != 0) {
digitalWrite(dividerDotsPin, LOW);
}
}
}