#include "led_control.h"
#include <TimerOne.h>
volatile Led led(9);
const char message[] = "...---...";
const int size = 9;
volatile int pos = 0;
volatile bool is_on = false;
volatile int count = 0;
const int dash_len = 3;
void setup() {
pinMode(2, INPUT_PULLUP); // подтягивающий резистор на входе прерывания
attachInterrupt(0, IinterruptLedSos, FALLING); //разрешение внешнего прерывания (вывод 2 Arduino UNO)
Timer1.initialize(200000);
Timer1.attachInterrupt(LedSos);
Timer1.stop();
}
void IinterruptLedSos() {
static unsigned long prev_millis;
if (prev_millis + 200 < millis()) {
Timer1.start();
}
prev_millis = millis();
}
void LedSos() {
if (pos == size) {
led.switch_off();
pos = 0;
Timer1.stop();
return;
}
if (message[pos] == '-') {
DashBlink();
} else {
DotBlink();
}
}
void DashBlink() {
if (count == 0) {
led.change_state();
is_on = true;
} else if (count == dash_len) {
led.change_state();
count = 0;
is_on = false;
++pos;
return;
}
++count;
}
void DotBlink() {
led.change_state();
if (is_on) {
++pos;
}
is_on = !is_on;
}
void loop() {
}