I've seen a few people posting code, so here's mine. I'd use the "format for forum" but it's throwing an exception. I blame the structure of my code, which is very much programming by accident.
It's messy, but it (mostly) works. I'm posting the current state as an inspiration for others.
/*
- Morse Code Decoder and door opener
-
- Josh Myer josh@joshisanerd.com
-
- 20090103 rev 0 -- ugly and unfortunate, but mostly functional (my first arduino code)
-
- Hook a debounced switch up to digital pin 2, like you would for the Button demo.
-
- This code reads morse code from digital02, turning it into ASCII characters.
- If you key in "SOS" (... --- ...), it will turn on the LED on digital13.
-
- The intended application is to let me key in a password at my apartment's
- front gate and have it automatically let me into the building, instead of
- fumbling around for keys.
-
- There's still lots of stuff to do and clean up, but I wanted to share the idea
- and the current implementation to help spur people on.
*/
#include <avr/pgmspace.h>
#include <string.h>
int ledPin = 13; // choose the pin for the LED
int inputPin = 2; // choose the input pin (for a pushbutton)
int val = 0; // variable for reading the pin status
#define THRESHOLD 3
#define DELAY_TIME 10 // ms
int n_since_zero = 0;
int n_in_zero = 0;
#define NCHARS 26+10+3
char morse_chars[NCHARS] = {
'A',
'B',
'C',
'D',
'E',
'F',
'G',
'H',
'I',
'J',
'K',
'L',
'M',
'N',
'O',
'P',
'Q',
'R',
'S',
'T',
'U',
'V',
'W',
'X',
'Y',
'Z',
'0',
'1',
'2',
'3',
'4',
'5',
'6',
'7',
'8',
'9',
'.',
',',
'?',
};
char* morse_strings[NCHARS] = {
".-",
"-...",
"-.-.",
"-..",
".",
"..-.",
"--.",
"....",
"..",
".---",
"-.-",
".-..",
"--",
"-.",
"---",
".--.",
"--.-",
".-.",
"...",
"-",
"..-",
"...-",
".--",
"-..-",
"-.--",
"--..",
"-----",
".----",
"..---",
"...--",
"....-",
".....",
"-....",
"--...",
"---..",
"----.",
".-.-.-",
"--..--",
"..--..",
};
#define PAUSE 0
#define DIT 1
#define DAH 2
#define DDLEN 5
char passwd[] = "SOS";
char chars_rx[10];
int char_cursor= 0;
int ditsdahs[DDLEN];
int dd_cursor = 0;
void setup() {
pinMode(ledPin, OUTPUT); // declare LED as output
pinMode(inputPin, INPUT); // declare pushbutton as input
for (int i = 0; i < DDLEN; i++) {
ditsdahs = 0;
- }*
- dd_cursor = 0;*
- for (int i = 0; i < 10; i++) {*
chars_rx = 0;
* }*
* char_cursor = 0;
_ Serial.begin(9600);_
_}_
void dd_print() {
_ Serial.print(" > DD BUF: ");_
_ for(int i = 0; i < DDLEN; i++) {_
_ Serial.print(ditsdahs);
Serial.print(", ");
}
Serial.println();
}_
boolean dd_eq(charbuf) {
* if(ditsdahs[0] == PAUSE) return false;*
* int i;*
_ for(i = 0; ditsdahs != PAUSE && i < DDLEN; i++) {
if (ditsdahs == DIT && buf == '-') {
* return false;
}
if (ditsdahs == DAH && buf == '.') {
return false;
}
}
/
* Serial.print("Got to the end of ");
Serial.print(buf);
Serial.print("; checking strlen=");
Serial.print(strlen(buf));
Serial.print(" == i=");
Serial.println(i);
/
if (i != strlen(buf)) return false;
* return true;
}
void dd_decode() {
for(int i = 0; i < NCHARS; i++) {
/
* Serial.print("dd_decode: check i=");*
* Serial.print(i);
Serial.print(" , string="");_
Serial.print(morse_strings);
_ Serial.print("", c=");_
Serial.print(morse_chars);
_ Serial.println();
/_
if (dd_eq(morse_strings)) {
char c = morse_chars*;
char_emit(c);
_ break;
}
}
for (int i = 0; i < DDLEN; i++) {
ditsdahs = 0;
}_
dd_cursor = 0;
_}
void check_passwd() {
Serial.print("Check password: ");_
Serial.print(chars_rx);
_ Serial.println();_
if (0 != strstr(chars_rx, passwd)) {
_ digitalWrite(ledPin, HIGH);
}
/
* for (int i = 0; i < 10; i++) {
chars_rx = 0;
}_
char_cursor = 0;
_ /
}
void char_emit(char c) {
* Serial.print("Got a char: c=");
Serial.println(c);_
chars_rx[char_cursor] = c;
char_cursor++;
if (char_cursor >= strlen(passwd)) {
check_passwd();
_ }
}_
void dd_emit(int v) {
ditsdahs[dd_cursor] = v;
dd_cursor++;
dd_print();
_ if (v == PAUSE)_
dd_decode();
_}
void dit() {_
dd_emit(DIT);
_ Serial.println("DIT");
}
void dah() {_
dd_emit(DAH);
_ Serial.println("DAH");
}
void pause() {_
dd_emit(PAUSE);
_ Serial.println("PAUSE");
}
void loop(){
val = digitalRead(inputPin); // read input value*
* if (val == LOW) {_
if (n_since_zero > 5) {
_ / Serial.print("CROSS LOW AFTER ");
* Serial.println(n_since_zero);*
/_
if (n_since_zero > 16) {
_ dah();
}
else {
dit();
}
}_
n_since_zero = 0;
n_in_zero++;
_ }
else {_
n_since_zero++;
_ }_
if (n_in_zero == 20) {
_ pause();
}_
if (n_since_zero > THRESHOLD) {
if (n_in_zero > 0) {
_ // Serial.print("CROSS HIGH AFTER ");
//Serial.print(" > ");_
// Serial.println(n_in_zero);
_ // Serial.println("**************************");
}_
n_in_zero = 0;
_ // digitalWrite(ledPin, HIGH);
}
else {
// digitalWrite(ledPin, LOW);
}_
delay(DELAY_TIME);
_}*_