#include <TimerOne.h>
#include <Bounce2.h>;
const int led = 9; // вывод со светодиодом
int ledState = LOW; //состояние светодиода
volatile unsigned long blinkCount = 0;
volatile unsigned long blinkLongCount = 0;
volatile unsigned long count = 0;
volatile unsigned long totalCount = 0;
bool isBlinkStart = true;
bool isBlinkLongStart = false;
bool isButtonPressed = false;
int lastButton = LOW;
// Создаем объект
Bounce debouncer = Bounce();
const int button = 2;
void setup(void)
{
pinMode(led, OUTPUT);
Timer1.initialize(200000);
Timer1.attachInterrupt(blinkLED);
Serial.begin(9600);
pinMode(button, INPUT_PULLUP);
// Даем бибилотеке знать, к какому пину мы подключили кнопку
debouncer.attach(button);
debouncer.interval(5); // Интервал, в течение которого мы не буем получать значения с пина
}
void blinkLED(void)
{
if (isButtonPressed)
{
if (isBlinkStart)
{
if (ledState == LOW)
{
ledState = HIGH;
blinkCount = blinkCount + 1; //количество миганий
Serial.print('.');
} else
{
ledState = LOW;
}
if (blinkCount == 3)
{
ledState = LOW;
blinkCount = 0;
count = 0;
isBlinkStart = false;
isBlinkLongStart = true;
++totalCount;
if (totalCount == 2)
{
Serial.println();
totalCount = 0;
isButtonPressed = false;
isBlinkLongStart = false;
isBlinkStart = true;
}
}
}
if (isBlinkLongStart)
{
++count;
if (ledState == LOW && count == 3)
{
ledState = HIGH;
blinkLongCount = blinkLongCount + 1; //количество миганий
count = 0;
Serial.print('-');
} else
{
ledState = LOW;
}
if (blinkLongCount == 3)
{
ledState = LOW;
blinkLongCount = 0;
isBlinkStart = true;
isBlinkLongStart = false;
}
}
digitalWrite(led, ledState);
}
}
void loop(void)
{
debouncer.update();
// Получаем значение кнопки
int curButton = debouncer.read();
if (curButton == LOW && lastButton == HIGH)
{
isButtonPressed = !isButtonPressed;
}
lastButton = curButton;
delay(100);
}