Skip to content

Instantly share code, notes, and snippets.

@krobro
Created March 24, 2015 22:06
Show Gist options
  • Save krobro/4eb81265cb09cc28a8c7 to your computer and use it in GitHub Desktop.
Save krobro/4eb81265cb09cc28a8c7 to your computer and use it in GitHub Desktop.
Convert light signals received via light sensor from morse code to text.
//
// morse_decoder
//
// Convert light signals received via light sensor from morse code to text
//
// We are using port S1 on the Duinobot board for the light sensor.
// The value of THRESHOLD is dependent on your ambiant light ... it
// may need to be adjusted for your environment.
// A morse dot is assumed to be 500 msec (see DOT) ... have not tried any
// other speeds yet.
//
// NOTES:
// - Ports S0-S5 are analog input pins
// - S0 is identical to Arduino A0 and so on (S5=A5)
// - To use these ports as Digital pins add 14
//
#define DEBUG_PRINTLN(str,data) Serial.print(F("_DEBUG: ")); Serial.print(str); Serial.println(data);
#define DEBUG_PRINTLN(str,data) // DO NOTHING
#define THRESHOLD 800
#define DOT 250
char morse[6];
int morse_index = 0;
uint8_t _port;
boolean _state;
unsigned long _millisStart;
unsigned long _millisEnd;
static char *_list[26] = {".-", // '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'
// convert_morse
//
// convert morse code (such as "--..") to a
// letter ('z')
//
// code - the morse code to convert
//
// Returns - converted letter or '?' on failure
//
// Notes: inefficient linear search ... too slow?
//
char convert_morse(char *code)
{
DEBUG_PRINTLN("CONVERT: ", code);
for (int x = 0; x < 26; x++)
{
if (strcmp(_list[x], code) == 0)
{
DEBUG_PRINTLN("GOT: ", (char)(x+'a'));
return (char)(x + 'a');
}
}
// could not decode what was read
return '?';
}
void setup() {
Serial.begin(115200);
delay(2000);
_millisStart = _millisEnd = 0;
_state = LOW;
_port = 1;
Serial.println (" ");
Serial.println ("Finished setup ...");
}
void loop() {
boolean state;
// read the light sensor ; sensor can be HIGH or LOW
if (analogRead(_port) <= THRESHOLD)
{
state = LOW;
}
else
{
state = HIGH;
}
// check to see if the current state of the sensor
// matches the last state of the sensor.
if (state != _state)
{
_millisEnd = millis();
if (_state == HIGH)
{
// Just finished a HIGH state and transitioned to a low state
// did we just process a dot or a dash?
if (_millisEnd - _millisStart < DOT + DOT/10 )
{
morse[morse_index] = '.';
}
else
{
morse[morse_index] = '-';
}
// just finished reading another dot or dash
// append the dot or dash to the morse to decode
morse_index++;
morse[morse_index] = '\0';
}
else
{
// just finished a low state and transitioned to a high state
// Was a character, letter or word just finished?
if ( _millisEnd - _millisStart < DOT + DOT/10 )
{
// single dot low state ... finished a dot or a dash
}
else if ( _millisEnd - _millisStart > DOT*3 - DOT/10 )
{
// 3 dot low state or 7 dot low state ... finished a letter
char c = convert_morse(morse);
if (c =='?') {
Serial.println(" *** Failed to decode properly ... retrying ...");
}
else
{
Serial.print(c);
}
morse_index = 0;
if ( _millisEnd - _millisStart > DOT*7 - DOT/10 )
{
// 7 dot low state ... finished a word
Serial.print(' ');
}
}
}
// set the current state
_state = state;
_millisStart = _millisEnd;
}
}
@mjamaalahmad
Copy link

What light sensors have you used along with the project? Also please can you specify the minimum distance between sensors and the light source so that the light can be detected by the sensor.

@Vohyeah12
Copy link

What light sensor do you is with this project?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment