Skip to content

Instantly share code, notes, and snippets.

Created January 15, 2014 04:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/8430879 to your computer and use it in GitHub Desktop.
Save anonymous/8430879 to your computer and use it in GitHub Desktop.
Some really gross code i wrote for the Teensy 3.1 (http://www.pjrc.com/teensy/) Arduino compatible board to read input from a button (morse key) and search for an 'a' or 'z'. With Reddit Enhancement Suite installed, this will upvote or downvote
// http://www.pjrc.com/teensy/td_libs_Bounce.html
#include <Bounce.h>
const int buttonPin = 0;
const int ditLength = 300;
const int dahLength = ditLength * 3;
const int letterSpaceLength = ditLength * 3;
const int wordSpaceLength = ditLength * 7;
Bounce pushbutton = Bounce(buttonPin, 10);
unsigned long downAt = 0;
unsigned long upAt = 0;
unsigned long timeout = 0;
boolean timeoutBool = false;
String myLetter = "";
void setup() {
pinMode(buttonPin, INPUT_PULLUP);
Serial.begin(38400);
Serial.println("READY - OK");
}
void processMorse(String myString) {
Serial.print("Checking '");
Serial.print(myString);
Serial.print("' - ");
if (myString == ".-") {
Serial.println("'A'");
Keyboard.write('a');
} else if (myString == "--..") {
Serial.println("'Z'");
Keyboard.write('z');
} else {
Serial.println("UNKNOWN");
}
}
void loop() {
// test how long we've been waiting for a space
if ((millis() - timeout) > letterSpaceLength && timeoutBool == true) {
//Serial.println("Timeout");
processMorse(myLetter);
timeoutBool = false;
myLetter = "";
}
if (pushbutton.update()) {
if (pushbutton.fallingEdge()) {
// button pushed down
downAt = millis();
} else if (pushbutton.risingEdge()) {
// button released
upAt = millis();
unsigned long diff = upAt - downAt;
if (diff <= ditLength) {
// we got a dit
myLetter += ".";
//Serial.print(".");
timeout = millis();
timeoutBool = true;
} else if (diff > ditLength) {
// we got a dah
myLetter += "-";
//Serial.print("-");
timeout = millis();
timeoutBool = true;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment