49 lines
981 B
C++
49 lines
981 B
C++
const int dot_pin = 2;
|
|
const int dash_pin = 3;
|
|
|
|
float wpm = 10.0f;
|
|
|
|
char state = 'R'; // Ready, Send, Wait
|
|
int remaining = 0;
|
|
int last_update = -1;
|
|
|
|
char last_send = 'X';
|
|
|
|
void setup() {
|
|
Serial.begin(115200);
|
|
|
|
pinMode(dot_pin, INPUT_PULLUP);
|
|
pinMode(dash_pin, INPUT_PULLUP);
|
|
}
|
|
|
|
void loop() {
|
|
int dit = 60.0f / (50.0f * wpm) * 1000.0f;
|
|
|
|
int dt = millis() - last_update;
|
|
last_update = millis();
|
|
remaining -= dt;
|
|
|
|
if (state == 'S' && remaining <= 0) {
|
|
state = 'W';
|
|
remaining = dit;
|
|
} else if (state == 'W' && remaining <= 0) {
|
|
state = 'R';
|
|
} else if (state == 'R') {
|
|
if (digitalRead(dot_pin) == LOW) {
|
|
state = 'S';
|
|
remaining = dit;
|
|
}
|
|
if (digitalRead(dash_pin) == LOW) {
|
|
state = 'S';
|
|
remaining = 3 * dit;
|
|
}
|
|
}
|
|
|
|
char current_send = state == 'S' ? '2' : '0';
|
|
|
|
if (current_send != last_send) {
|
|
Serial.print(current_send);
|
|
last_send = current_send;
|
|
}
|
|
delay(1); // Limit update rate under 1kHz
|
|
}
|