Request for ideas for lookup table functionality

I'm trying to think of a way to make my Arduino Mega pick a random word from a list, flash all the segments of an LCD in the morse code for that word, then give me a few seconds to try to guess the word, then the LCD would display the word.

I'm looking for a good method to store and access the morse code for each letter. I like the idea of a binary code that starts with a one, then has zeroes and ones for dots and dashes, respectively. For example, "P" (dot dash dash dot) would be 0b10110.

I was thinking to have a long array of words, pick one at random, then access each letter in the word in a for loop. I'm just not sure how to use the letter as a key to access the binary morse integer.

I guess I could use a whole lot of if else statements, but that doesn't sound efficient or pretty. I was thinking to use some sort of map or lookup table, but I saw other posts saying that a map is kind of resource intensive for an Arduino.

Suggestions?

How much of this project do you have working?

Ideas are good. Characters have numerical values already, see ASCII code for that.

The Morse patterns could be in an array big enough for all the characters you are interested in.

The ASCII code numerical equivalent of a character could be used as an index into that array.

You could do some calculations. 'A' is 65, any letter after that is just one more, so a character minus 'A' or 65 would be an index 0 to 25 for the 26 letters A..Z.

HTH

a7

I have an LCD that I can output words onto and turn on and off. I think I can figure out how to use my little binary code to turn the segments on and off at the correct times.

Oh, that'd be great, thanks!

Besides the timing to dots and dashes, you also need to get the timing correct for the space between letters and the space between words. There are many dozens of Arduino related code around that produce Morse code sounds and you can easily convert to light flashing.

consider


const char *list [] = {
    "its",
    "easy",
    "to",
    "tell",
    "the",
    "depth",
    "of",
    "a",
    "well"
};
#define N_LIST      (sizeof(list)/sizeof(char *))

// -----------------------------------------------------------------------------
void
genMorseStub (
    const char *p )
{
    // stub - do nothing
}

// -----------------------------------------------------------------------------
void
loop ()
{
    int n = random (0, N_LIST);

    genMorseStub (list [n]);
    delay (2000);
    Serial.println (list [n]);
}

// -----------------------------------------------------------------------------
void
setup ()
{
    Serial.begin (9600);

}

It shouldn't be too hard. Between letters is the length of three dots and between words is length of seven dots. I may take a look how others have done it, though.

Morse Code Timing

  • Dit: 1 unit
  • Dah: 3 units
  • Intra-character space (the gap between dits and dahs within a character): 1 unit
  • Inter-character space (the gap between the characters of a word): 3 units
  • Word space (the gap between two words): 7 units

I hadn't started thinking about that part yet, but thanks for the suggestion. What is genMorseStub for, though?

...

Oh ha, I thought the comment saying "Do nothing" meant that it was supposed to remain empty.

All of the Arduino encoding & decoding for Morse Code is freely available for butchering:
https://forum.arduino.cc/t/arduino-magic-morse-with-nokia-84x84-display-minty-magic-morse/165942

Lean 'nuff to run on an old UNO or a bare atmega328P-PU at 8MHz.

Ray

Cool, thanks!

Certainly. It was a long time ago, so many current best-practices for Arduino code may be absent. More about the Minty build here:

Magic Morse on Arduino | Trybotics

Because Magic Morse is a true algorithm, commonly implemented tree-searches are not applicable... every Morse character is an integer which can be reversed back to Morse dots/dashes.

The original was coded in PICAXE basic:
Notes behind Magic Morse | PICAXE Forum

Re:

The Excel snippet shows that by using the DAH as the primary weighting element, a simple algorithm can be constructed for decoding Morse Code into a unique index in the range of 0-255. DAH's are weighted by received position in the stream and DAH's and DIT's are counted and stored in 3-bits to represent the number of Morse elements in a character. When the DAH weighting is added to the DIT+DAH counts, a unique number is created... called Magic Morse.

Yeah, this seems like a good strategy. Is there an advantage to your algorithm that encodes the length of the code in the magic morse number, as opposed to what I was thinking, where the first one in the binary number signals that the code is made up of the number of remaining bits? I was thinking a for loop that starts at the leftmost bit and scans right until it sees a one, then it would know the length from what's left over.

Nice to have a bidirectional algorithm to base everything on: I have a ping pong ball with LEDs inside that blink the temperature every 30 seconds... red, blue, green represents the 3 ranges inside the living room:
R 89 - 80
G 79 - 70
B 69 - 60

Lol. Is it wired to something, or is power and everything contained in the ping pong ball?

Pool sized Ping-Pong ball:

$1 mirror from dollar store; sits on battery pack. Sticky copper tape used for conductors .. ugly bug-style point-point wiring for tiny85... covered in conformal epoxy just like military repairs :wink:


That is actually really cool looking.

Also, I hadn't thought about it, but maybe I will include morse transmission practice like in your program too. And making a transmitter sounds fun.

1 Like