public
anonymous / RedditMorse
Created

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

  • Download Gist
RedditMorse
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
// 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;
}
}
}
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.