#include <EncButton.h>
#define RIGHT_LED_PIN 6
#define STOP_LED_PIN 7
#define LEFT_LED_PIN 8
#define RIGHT_BTN_PIN 3
#define STOP_BTN_PIN 4
#define LEFT_BTN_PIN 5
EncButton<EB_TICK, RIGHT_BTN_PIN> rightBtn(INPUT_PULLUP);
EncButton<EB_TICK, STOP_BTN_PIN> stopBtn(INPUT_PULLUP);
EncButton<EB_TICK, LEFT_BTN_PIN> leftBtn(INPUT_PULLUP);
bool isClickRightBtn = false;
bool isClickStopBtn = false;
bool isClickLeftBtn = false;
bool isEnableRightLed = false;
bool isEnableLeftLed = false;
bool isEnableStopLed = false;
uint32_t timeEnableRightLed = 0;
uint32_t timeEnableLeftLed = 0;
void setup() {
Serial.begin(9600);
pinMode(RIGHT_LED_PIN, OUTPUT);
pinMode(STOP_LED_PIN, OUTPUT);
pinMode(LEFT_LED_PIN, OUTPUT);
digitalWrite(RIGHT_LED_PIN, LOW);
digitalWrite(STOP_LED_PIN, LOW);
digitalWrite(LEFT_LED_PIN, LOW);
}
void loop() {
bool enableStopLed = clickStopBtn();
bool enableRightLed = clickRightBtn();
bool enableLeftLed = clickLeftBtn();
/*
Здесь дополнительная логика обработки нажатий кнопок
Например выключаем левый поворотник когда включается правый
if (enableRightLed) {
enableLeftLed = false;
isClickLeftBtn = false;
}
if (enableLeftLed) {
enableRightLed = false;
isClickRightBtn = false;
}
*/
blinkRightLed(enableRightLed, 1500, 500);
blinkLeftLed(enableLeftLed, 500, 200);
blinkStopLed(enableStopLed, 500, 500);
}
bool clickRightBtn()
{
rightBtn.tick();
if (rightBtn.click()) {
Serial.print("rightBtn.click() - ");
Serial.print(isClickRightBtn);
Serial.print(" - ");
Serial.println(millis());
isClickRightBtn = !isClickRightBtn;
}
return isClickRightBtn;
}
bool clickLeftBtn()
{
leftBtn.tick();
if (leftBtn.click()) {
Serial.print("leftBtn.click() - ");
Serial.print(isClickLeftBtn);
Serial.print(" - ");
Serial.println(millis());
isClickLeftBtn = !isClickLeftBtn;
}
return isClickLeftBtn;
}
bool clickStopBtn()
{
stopBtn.tick();
if (stopBtn.click()) {
Serial.print("stopBtn.click() - ");
Serial.print(isClickStopBtn);
Serial.print(" - ");
Serial.println(millis());
isClickStopBtn = !isClickStopBtn;
}
return isClickStopBtn;
}
void blinkRightLed(bool enable, int timeEnable, int timeDisable)
{
if (isEnableRightLed) {
blinkLed(RIGHT_LED_PIN, millis() - timeEnableRightLed, timeEnable, timeDisable);
}
if (enable && !isEnableRightLed) {
isEnableRightLed = true;
timeEnableRightLed = millis();
} else if (!enable && isEnableRightLed) {
isEnableRightLed = false;
digitalWrite(RIGHT_LED_PIN, LOW);
}
}
void blinkLeftLed(bool enable, int timeEnable, int timeDisable)
{
if (isEnableLeftLed) {
blinkLed(LEFT_LED_PIN, timeEnableRightLed - millis(), timeEnable, timeDisable);
}
if (enable && !isEnableLeftLed) {
isEnableLeftLed = true;
timeEnableRightLed = millis();
digitalWrite(LEFT_LED_PIN, HIGH);
} else if (!enable && isEnableLeftLed) {
isEnableLeftLed = false;
digitalWrite(LEFT_LED_PIN, LOW);
}
}
void blinkStopLed(bool enable, int timeEnable, int timeDisable)
{
if (enable && !isEnableStopLed) {
isEnableStopLed = true;
digitalWrite(STOP_LED_PIN, HIGH);
} else if (!enable && isEnableStopLed) {
isEnableStopLed = false;
digitalWrite(STOP_LED_PIN, LOW);
}
}
void blinkLed(int pin, uint32_t time, int timeEnable, int timeDisable)
{
int timeCicle = time % (timeEnable + timeDisable);
bool enable = timeCicle < timeEnable;
if (enable) {
digitalWrite(pin, HIGH);
} else {
digitalWrite(pin, LOW);
}
}
nano:12
nano:11
nano:10
nano:9
nano:8
nano:7
nano:6
nano:5
nano:4
nano:3
nano:2
nano:GND.2
nano:RESET.2
nano:0
nano:1
nano:13
nano:3.3V
nano:AREF
nano:A0
nano:A1
nano:A2
nano:A3
nano:A4
nano:A5
nano:A6
nano:A7
nano:5V
nano:RESET
nano:GND.1
nano:VIN
nano:12.2
nano:5V.2
nano:13.2
nano:11.2
nano:RESET.3
nano:GND.3
led1:A
led1:C
led2:A
led2:C
btn1:1.l
btn1:2.l
btn1:1.r
btn1:2.r
btn2:1.l
btn2:2.l
btn2:1.r
btn2:2.r
led3:A
led3:C
btn3:1.l
btn3:2.l
btn3:1.r
btn3:2.r