.... . .-.. .-.. --- / .-- --- .-. .-.. -..
Code: Select all
#include <Bounce.h>
#define BUTTON 2
#define LED 13
/*
Arduino Telegraph Key - turns your Arduino into a simple morse-code device.
Pressing the button will transmit dits and dahs to the serial port, while
lighting up the LED plugged into pin 13 (or on-board LED).
The Bounce library is required for this program to work:
http://www.arduino.cc/playground/Code/Bounce
You may do whatever you like with this code!
*/
// First set up our bounce instance, with a 20ms delay
Bounce bouncer = Bounce(BUTTON,20);
// Define variables for clicks and pauses
int clickDuration;
int pauseDuration;
void setup() {
pinMode(BUTTON, INPUT);
pinMode(LED, OUTPUT);
Serial.begin(9600);
}
void loop() {
// Update the bouncer
bouncer.update();
int buttonState = bouncer.read();
clickDuration = 0;
pauseDuration = 0;
while (buttonState == HIGH) {
// Measure how long the button is pressed
digitalWrite(LED, HIGH);
clickDuration++;
delay(1);
bouncer.update();
buttonState = bouncer.read();
}
if (clickDuration > 0 && clickDuration < 200) {
Serial.print("."); // Print a 'dit' for presses less than 200ms
} else if (clickDuration > 200) {
Serial.print("-"); // Print a 'dah' for presses more than 200ms
}
while (buttonState == LOW) {
// Measure the pause between presses
digitalWrite(LED, LOW);
pauseDuration++;
delay(1);
bouncer.update();
buttonState = bouncer.read();
}
if (pauseDuration > 1000) {
Serial.print(" "); // Print a space for pauses greater than 1 second
}
if (pauseDuration > 3000) {
Serial.print("/ "); // Print a / for pauses greater than 3 seconds
}
}