Arrays

Hi and thanks for any replies in advance.

I’m used to programming in various languages so don’t need to be to basic.

Almost all languages that I’ve used I’ve been able to create a 2 dimensional array which I don’t think can be done here.

I’ve created a set of arrays for Morse Code.

As there doesn’t seem to be a function to check the length of the array that I know of I’ve had to create an array to hold the length of each array and then I know how far to look into it.

This is all very long winded and makes for an awful lot of code.

To explain I have…

int codeLen[38] {2,4,4,…} - This then holds 38 numbers for then length of each array that holds the character.

I then have

boolean codeA = {0,1}
boolean codeB = {1,0,0,0}


As you can see it’s not very good to shorten.

If I was to do this in most other languages I would have a two dimensional array that held all of the int arrays. This would then let me call just one array from code and only have a few lines.

With the way I have done this I have to create a switch case statement to cover all 38 characters resulting in lots of code.

Does anyone know of a shorter way to do this?

Below is my function that has the switch case in it and you can see how long it will get.

void runCode(char letter){
  int codeSize = 0;
  
  switch (letter) {
    case 'A':
      codeSize=codeLen[0];
      for(int i=0;i<codeSize;i++){
          ledFlasher(codeA[i]);
      }
      break;
    case 'B':
      codeSize=codeLen[1];
      for(int i=0;i<codeSize;i++){
          ledFlasher(codeB[i]);
      }
      break;
    default:
      break;
    //
  }
}

For morse, the way I’ve seen it implemented before is to have an unsigned Integer for Each pattern (using 1 for dash, 0 for dot).

BUT
Since the length of the character can vary, Each byte had one extra “1” bit set to show the start of the bits.

In operation, each bit is shifted out of the integer until just this 1 remains.

Naturally these integers can then be held in an array. You could also hold a separate array of chars for their translation.

Langy: Almost all languages that I've used I've been able to create a 2 dimensional array which I don't think can be done here. (...)´

Why you say that? You can create 2 dimension arrays but you can't create 2 dimension array without any length:

char array1[]={1, 2, 3, 4, 5};   // this will work
char array2[][2]={1, 2, 3, 4, 5, 6};    // this will work
char array3[][]={1, 2, 3, 4, 5, 6};     // this DON'T work
char array4[3][]={1, 2, 3, 4, 5, 6};    // this DON'T work

You sound very confused. Of course C/C++ has two dimensional arrays.

@KenF

Naturally these integers can then be held in an array.

Int's !!!!!

Two byte arrays can be used (or one 2d byte array). One holds the Morse pattern and one the number of dots+dashes (bits). index by the ascii code you want to send.

Mark

holmes4: @KenF Int's !!!!!

Bytes could be used for any of the commonly used codes. There is, however, the 8 dots that are used to convey an error. Using my scheme of one extra bit (to mark the end of the pattern) would then make this a 9 bit pattern to be stored.

Just to clarify my suggested scheme, the patterns would be defined thus. (bits would be read from right to left)

byte patterns[]={
  B00000110, //A = DOT DASH
  B00010001, //B = DASH DOT DOT DOT
  B00010101, //C = DASH DOT DASH DOT
  //etc..
};

My latest book, Arduino Projects for Amateur Radio pretty much uses Ken’s approach for encoding ASCII to Morse. A snippet of the code for the array is:

char ltab[] = {
  0b101,              // A
  0b11000,            // B 
  0b11010,            // C
  0b1100,             // D
  0b10,               // E
  0b10010,            // F
  0b1110,             // G

Reading from left to right, the first binary 1 is a marker that says the code for the character follows. A 0 is a dit and a 1 is a dah. The function that actually “sends” the code is:

void sendcode(char code)
{
  int i;

  for (i=7; i>= 0; i--) {  // Look for start bit
    if (code & (1 << i))
      break;
  }
  for (i--; i>= 0; i--) {  // Remaining bits are the actual Morse code
    if (code & (1 << i))
      dah();
    else
      dit();
  }
  mydelay(2*ditlen);	// space between letters
#ifdef DEBUG
  Serial.print("");
#endif
}

Since you have programming experience, this is pretty simple stuff and works quite well. In “the old days” when you had a Morse code exam, you had to be able to send and receive 13 wpm (about 65 characters/minute). On an Arduino, the code should handle up to about 300 wpm.

econjack: My latest book, Arduino Projects for Amateur Radio pretty much uses Ken's approach for encoding ASCII to Morse. A snippet of the code for the array is:

I daresay you may well have been the inspiration of something I've seen at some time.

If you put the bits in reverse order though. You make the sending routine a tad smaller.

thus

void sendcode(char code)
{
while(code > 1)
    {
      if (code & 1)
       dah();
     else
       dit();
    mydelay(2*ditlen);    // space between letters
    code = code >> 1;
    }
}

Many thanks for all the replies. 1. I was just using the reference on the website and I'm sure it did not show anything more than a one dimensional array, so im pleased I've been shown the way on that. 2. Hadn't thought about using bits, very good. I know the Arduino is space restricted so I want to make the most of this, where on a pic I would just go for the easier lazier option to read.

I will take a proper look later when I get the time.

The idea of bits can reduce the array back to single dimension a reduce the amount of code. I also wanted to create a console entry to enter text which this way will also resolve that.

The initial project is to play a dedicated massage on LEDs, but I did want to advance that for personal use of the console.

Again many thanks, I will try an add the finished example to the projects to share when I'm finished.

Hopefully I will not need much more help now.