Add keyer logic to Arduino
This commit is contained in:
parent
5b30348b98
commit
dbb1225685
1 changed files with 49 additions and 0 deletions
49
keyer/keyer.ino
Normal file
49
keyer/keyer.ino
Normal file
|
|
@ -0,0 +1,49 @@
|
|||
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
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue