#include <encButton.h>
#include <SevSeg.h>
SevSeg sevseg;
#define vd A0
#define CLK 2
#define DT 3
#define SW 4
encButton eb(CLK, DT, SW);
int number = 0;
int state = 0;
unsigned long vd_timer = millis();
int vd_delay = 200;
unsigned long pass_check_timer = millis();
int pass_check_delay = 15000;
int post_pass_delay = 30000;
int password[4] = {9, 3, 8, 5};
int current_digit = 0;
unsigned long password_timer = millis();
bool password_checker;
int password_delay = 1000;
int number_member = 0;
void setup() {
Serial.begin(9600);
byte numDigits = 1;
byte digitPins[] = {13};
byte segmentPins[] = {5, 6, 7, 8, 9, 10, 11, 12};
bool resistorsOnSegments = false;
byte hardwareConfig = COMMON_ANODE;
bool updateWithDelays = false;
bool leadingZeros = false;
bool disableDecPoint = false;
sevseg.begin(hardwareConfig, numDigits, digitPins, segmentPins, resistorsOnSegments,
updateWithDelays, leadingZeros, disableDecPoint);
pinMode(vd, OUTPUT);
attachInterrupt(0, isr, CHANGE); // прерывание на 2 пине! CLK у eb
}
void isr() {
eb.tick();
}
void vd_blink() {
switch(state) {
case 0:
digitalWrite(vd, LOW); break;
case 1:
if (millis() - vd_timer < vd_delay) {
digitalWrite(vd, HIGH);
} else {
digitalWrite(vd, LOW);
}
if (millis() - vd_timer >= vd_delay * 2) vd_timer = millis();
break;
case 2:
digitalWrite(vd, HIGH); break;
}
}
void check_password() {
if (millis() - password_timer > password_delay && state == 1) number_member = number;
if (number_member == password[current_digit] && state == 1) current_digit++;
if (millis() - pass_check_timer > post_pass_delay) {
state = 0;
password_checker = false;
}
if (millis() - pass_check_timer > pass_check_delay && password_checker == false) {
state = 0;
current_digit = 0;
number_member = 0;
}
if (current_digit > 3) password_checker = true;
if (password_checker) {
state = 2;
current_digit = 0;
number_member = 0;
}
}
void loop() {
eb.tick();
if (eb.tick()) {
switch (eb.action()) {
case EB_PRESS:
state = 1;
pass_check_timer = millis();
break;
case EB_HOLD:
state = 0;
password_checker = false;
break;
case EB_TURN:
password_timer = millis();
number += 1 * eb.dir;
break;
}
}
number = constrain(number, 0 , 9);
check_password();
vd_blink();
sevseg.setNumber(number);
sevseg.refreshDisplay();
Serial.print(current_digit);
Serial.print(", ");
Serial.println(password_checker);
}