Go Down

Topic: morse code flasher - lookup table question (Read 3 times) previous topic - next topic

AlphaTango

Billroy - Thanks for the response and example.  I'm impressed.  When I looked at the code, I was immediately overwelmed.  LOL!  Too complex for me at this juncture.  Thanks none the less.

Krupski - yeah, this is more what I had in mind.  So, are you telling me that I can literally use the individual characters for the index??  I had no idea.  That makes things simpler.  Is that * a reference to pointer in this instance?  If so, you're shifting thru the symbols, correct?

MaJiG

Here is some ideas:

"e" should be a dit, not a dah, yes?

In Morse code the length of a dah is usually three times a dit, I believe.

I would use three values for the dit and dah values: 1 = dit, 3 = dah, and 0 = no blink.  The need for the third value is to allow you to pad the unused array elements with a value since some only use one like "e", while others like "1" use five

Use a two-dimensional array and initialize the second element with your dit and dah data.  The offset into the first element of the array can be the character's ASCII value.  Thus, if you have an int array named didah, then didah[65] = "1","3","0","0","0" which represents "A" (ascii 65) and would return dit dah 0 0 0

You could then use a function that is passed the value from each of the values in didah[65]. The value could then be multiplied by 500 to achieve the delay time.

I'm sure you get the idea...

read a character from the Serial object
convert character to ASCII and store in a variable
then pass the variable into a function that enters a for/loop to read each of the elements at that offset and passes the array value to your blink function
the blink function turns ON the LED for 500 * the number milliseconds -- 1/2 second for a dit, 1.5 seconds for a dah, and 0 seconds for the padding
don't forget the pause between letters

I hope I've been clear.

Good luck, and ask if you have further question.

AlphaTango

Thanks MaJiG!  I think you were clear, but it's a little more complicated than I can comprehend at this point.  It's been a long time since I coded anything.  I think what Krupski said is more what I had in mind, although you have given me some ideas to make it even simpler.

Thanks!

MaJiG

#8
Feb 11, 2013, 04:02 am Last Edit: Feb 11, 2013, 05:26 am by MaJiG Reason: 1
Here is a example that only recognizes a or b or c (either case).

Code: [Select]

const int spd = 300;    // milliseconds for a dit

// the character array (only holds abc)
int didah[3][5] =
{
 { 1, 3, 0, 0, 0 },
 { 3, 1, 1, 1, 0 },
 { 3, 1, 3, 1, 0 }
};

void setup()
{
 pinMode(13, OUTPUT);    // pin 13 has the onboard LED, so no external parts
 
 Serial.begin(9600);
 Serial.println("---==START==---");
}

void loop()
{
 int mychar = 0;
 
 // check for a character
 if (Serial.available() > 0)
 {
    //here: there is a char coming in, so let's read it in
    mychar  = Serial.read();    // mychar now holds an ASCII value of the typed char
   
    if( mychar > 96 )
    {
      mychar -= 97 ;
    }
    else
    {
      mychar -= 65 ;
    }
   
    // we need to keep within the array's limits
    if( (mychar >= 0) && (mychar <= 3) )
    {
       morsechar( mychar );
    }
 }
}

void morsechar( int x )
{
    for(int i = 0 ; i < 5 ; i++ )
    {
       Serial.println(didah[x][i]);
       blip( didah[x][i] );
    }  
}

void blip( int d )
{
 if( (d == 1) || (d == 3) )
 {
   d *= spd ;
   
   digitalWrite(13, HIGH);
   delay( d );
 
   digitalWrite(13, LOW);
   delay(spd * 5);       // arbitrary 5 dit off delay
 }
}


you would need to expand the array to accommodate all the chars and adjust or eliminate the -= adjustment.

It runs on a bare Arduino UNO so it is easy to see.

Try it and let me know.

lloyddean

There's morse code all around us ...

Code: [Select]

#if defined(ARDUINO) && ARDUINO >= 100
  #include "Arduino.h"
#else
  #include "WProgram.h"
#endif

#include <ctype.h>

#if   defined(_BOARD_CEREBOT_MX4CK_)
const uint8_t       pinLED          = 64;               // ON-BOARD LED1
#elif defined(_BOARD_UNO_)
const uint8_t       pinLED          = 13;               // ON-BOARD LED1
#else
#error "!!!Target is unknown board!!!"
#endif


const unsigned long DELAY_CASE      = 150UL;

const unsigned long DELAY_DOT       = DELAY_CASE;       // MILLISECONDS
const unsigned long DELAY_DOT       = DELAY_INTRA;
const unsigned long DELAY_DASH      = 3 * DELAY_CASE;
const unsigned long DELAY_CHAR      = 3 * DELAY_CASE;
const unsigned long DELAY_WORD      = 7 * DELAY_CASE;
const unsigned long DELAY_MESSAGE   = 3 * 1000;         // 3 SECONDS

const uint8_t       LED_OFF         = LOW;              // SYNONYMN FOR READABILITY
const uint8_t       LED_ON          = HIGH;             // SYNONYMN FOR READABILITY


#define SIZEOF_ARRAY(ARRAY)         (sizeof(ARRAY) / sizeof(ARRAY[0]))

enum { EOS, DOT, DASH, MASK = 3 };

struct sequence_pair_t
{
    const char      character;                          // character
    const uint16_t  encoding;                           // sequence, zero terminated 'C' string
};

const sequence_pair_t  morse_lookup[] =
{
    // STRING OF 2-BIT VALUES ENCODE THE MORSE SEQUENCE, LSB -> MSB
    //
    // 00   END-OF-SEQUENCE
    // 01   '.'
    // 10   '-'
    // 11   UNUSED

    // EXAMPLES:
    //
    //  { 'A',    0b0000000000001101 }    // ".-"     EOS  EOS  EOS  EOS  EOS  EOS DASH  DOT
    //, { '0',    0b0000001010101010 }    // "-----"  EOS  EOS  EOS DASH DASH DASH DASH DASH

   
    // --- Numeric
      { '0',    0b0000001010101010 }    // "-----"
    , { '1',    0b0000001010101001 }    // ".----"
    , { '2',    0b0000001010100101 }    // "..---"
    , { '3',    0b0000001010010101 }    // "...--"
    , { '4',    0b0000001001010101 }    // "....-"
    , { '5',    0b0000000101010101 }    // "....."
    , { '6',    0b0000000101010110 }    // "-...."
    , { '7',    0b0000000101011010 }    // "--..."
    , { '8',    0b0000000101101010 }    // "---.."
    , { '9',    0b0000000110101010 }    // "----."
   
    // --- Alphabetic
    , { 'A',    0b0000000000001001 }    // ".-"
    , { 'B',    0b0000000001010110 }    // "-..."
    , { 'C',    0b0000000001100110 }    // "-.-."
    , { 'D',    0b0000000000010110 }    // "-.."
    , { 'E',    0b0000000000000001 }    // "."
    , { 'F',    0b0000000001100101 }    // "..-."
    , { 'G',    0b0000000000011010 }    // "--."
    , { 'H',    0b0000000001010101 }    // "...."
    , { 'I',    0b0000000000000101 }    // ".."
    , { 'J',    0b0000000010101001 }    // ".---"
    , { 'K',    0b0000000000100110 }    // "-.-"
    , { 'L',    0b0000000001011001 }    // ".-.."
    , { 'M',    0b0000000000001010 }    // "--"
    , { 'N',    0b0000000000000110 }    // "-."
    , { 'O',    0b0000000000101010 }    // "---"
    , { 'P',    0b0000000001101001 }    // ".--."
    , { 'Q',    0b0000000010011010 }    // "--.-"
    , { 'R',    0b0000000000011001 }    // ".-."
    , { 'S',    0b0000000000010101 }    // "..."
    , { 'T',    0b0000000000000010 }    // "-"
    , { 'U',    0b0000000000100101 }    // "..-"
    , { 'V',    0b0000000010010101 }    // "...-"
    , { 'W',    0b0000000000101001 }    // ".--"
    , { 'X',    0b0000000010010110 }    // "-..-"
    , { 'W',    0b0000000010100110 }    // "-.--"
    , { 'Z',    0b0000000001011010 }    // "--.."
   
    // --- Punctuation
    , { ',',    0b0000101001011010 }    // "--..--"
    , { '.',    0b0000100110011001 }    // ".-.-.-"
    , { ':',    0b0000010101101010 }    // "---..."
    , { '?',    0b0000010110100101 }    // "..--.."
    , { '-',    0b0000100101010110 }    // "-....-"
    , { '/',    0b0000000110010110 }    // "-..-."
    , { '(',    0b0000100110100110 }    // "-.--.-"
    , { ')',    0b0000100110100110 }    // "-.--.-"
   
//  , { '\'', ".----." }  // the windows IDE has problem with escaped character literals!
//  , { '\"', ".-..-." }  // the windows IDE has problem with escaped character literals!
};


void blinkDot()
{
    digitalWrite(pinLED, LED_ON);

    delay(DELAY_DOT);

    digitalWrite(pinLED, LED_OFF);
}


void blinkDash()
{
    digitalWrite(pinLED, LED_ON);

    delay(DELAY_DASH);

    digitalWrite(pinLED, LED_OFF);
}


void blinkCharacterSequence(uint16_t encoding)
{
    do
    {
        switch ( encoding & MASK )
        {
            case DOT:   blinkDot();      break;
            case DASH:  blinkDash();     break;
        }

        if ( encoding >>= 2 )
        {
            delay(DELAY_INTRA);
        }

    } while ( EOS != (encoding & MASK) );
}


void blinkCharactor(char ch)
{
    const int cEntries = SIZEOF_ARRAY(morse_lookup);
   
    for ( int i = cEntries; i--; )
    {
        if ( ch == morse_lookup[i].character )
        {
            blinkCharacterSequence(morse_lookup[i].encoding);

            return;
        }
    }
}


void loop()
{
    // Initialize message using "string" syntax which has invisible
    // terminating 0

    static const char  szMessage[] = { "Hello AlphaTango" };

    for ( int i = 0; szMessage[i]; )
    {
        blinkCharactor(toupper(szMessage[i++]));
       
        delay(((' ' != szMessage[i]) ? DELAY_CHAR : DELAY_WORD));
    }
   
    delay(DELAY_MESSAGE);
}


void setup()
{
    pinMode(pinLED, OUTPUT);
    digitalWrite(pinLED, LED_OFF);
   
    delay(DELAY_MESSAGE);
}


Go Up