Formatting Array Loop element retrieval for if else decision logic

Need help with formatting… I want to read the elements within an Array (in order - loop), and use each element result as an if/else decision. It does not appear to be reading the elements, and/or my “if/else statements” are not formatted correctly. No matter what value is in the Array elements, it only drops down to my last “else” dosomethingerrorroutine process)!

Please advise!

My Loop Code:

int Mywords = {‘1’,‘2’,‘1’,‘3’,‘2’,‘2’,‘2’,‘3’,‘2’,‘1’,‘1’,‘1’,‘7’,‘1’,‘2’,‘1’,‘3’,‘2’,‘2’,‘2’,‘3’,‘2’,‘1’,‘1’,‘1’};

for (int X=0; X<= 24; X++) {

if (Mywords == 1) {dosomethingA}

if else (Mywords == 2) {dosomethingB}

if else (Mywords == 3) {dosomethingC}

if else (Mywords == 7) {dosomethingD}

else {dosomethingErrorRoutine}

} …

Now post the real code.
In code tags.

int Mywords []= {'1','2','1','3','2','2','2','3','2','1','1','1','7','1','2','1','3','2','2','2','3','2','1','1','1'};
if (Mywords [X] == 1) {dosomethingA}

The array has chars in it such as '1'
You are testing for integers such as 1
Either put integers in the array or test for a char value

Thank you AWOL and HeliBob! That was the problem, I had ‘1’,‘2’,… instead of 1,2,…

So now it works great! I can put in a sequence of morse code dits and das into the Mywords array and it lights the LED13 accordingly. My first C++ App ever! YEAY!

My next coding adventure will be to add a new level of logic, using an additional “Alphabet” Array to allow actual letters (a, b, c…) to be translated into the dits and dahs before using this “Mywords” Array.

Thanks again!

Rob

/*
8March2020 Robert Hampton (Radiobiker)

Perform Morse Code (CW) lights blinking based on ARRAY (single CW character elements), using FOR and If/Else Loops.

Array Characters must only contain 1= Dot (1 unit long), 2 = Dash (3 units long),
3 = Dark (3 units long), 7 = Dark (7 units long), 9 = End of table character string.

Each Array Character contains one CW Element (example: 1,2,1 = “R” (or dit-dot-dit),
then 3 (dark between characters), or 7 (dark between words).

Using Arduino pcb LED 13

*/

void setup() {
int Mywords = 0;
}

// the loop function runs until last character in string
void loop() {
// initialize digital pin LED_13 as an output.

pinMode(13, OUTPUT);

// Array Loop counter “X”.

int X = 0;

// Error Loop “Y” for element result ELSE.

int Y = 0;

//Array of Morse Code Dits and Dahs

int Mywords = {1,2,1,3,2,2,2,3,2,1,1,1,7,1,2,1,3,2,2,2,3,2,1,1,1};

for (int X=0; X <= 24; X++) {

Serial.print(X, DEC); // print as an ASCII-encoded decimal
Serial.print("\t");

//Short 1x Dot
if (Mywords == 1){
digitalWrite(13, HIGH); // turn the LED on (HIGH is the voltage level)
delay(500); // wait for a second
digitalWrite(13, LOW); // turn the LED off by making the voltage LOW
delay(500);
}
// long 3x Dash
else if (Mywords == 2){
digitalWrite(13, HIGH); // turn the LED on (HIGH is the voltage level)
delay(900); // wait for a second
digitalWrite(13, LOW); // turn the LED off by making the voltage LOW
delay(500);
}
// 3x Silence Dark
else if (Mywords == 3){
digitalWrite(13, LOW); // turn the LED off by making the voltage LOW
delay(600);
}
// Long 7x silence Dark
else if (Mywords == 7){
digitalWrite(13, LOW);
delay (1000);
}
// Bad element count or character in Array.
else {
for (int Y = 0; Y <= 10; Y++){
digitalWrite(13, HIGH); // turn the LED on (HIGH is the voltage level)
delay(700); // wait for a second
digitalWrite(13, LOW); // turn the LED off by making the voltage LOW
delay(300);
digitalWrite(13, HIGH); // turn the LED on (HIGH is the voltage level)
delay(200); // wait for a second
digitalWrite(13, LOW); // turn the LED off by making the voltage LOW
delay(900);
}
}
}
}

if (Mywords [X] == 1) {dosomethingA}


if else (Mywords [X] == 2) {dosomethingB}


if else (Mywords [X] == 3) {dosomethingC}


if else (Mywords [X] == 7) {dosomethingD}


else {dosomethingErrorRoutine}
if (Mywords [X] == 1) {dosomethingA;}


else if (Mywords [X] == 2) {dosomethingB;}


else if (Mywords [X] == 3) {dosomethingC;}


else if (Mywords [X] == 7) {dosomethingD;}


else {dosomethingErrorRoutine;}

This should make it more clear,

if (Mywords [X] == 1)
{
  dosomethingA;
}
else 
{
  if (Mywords [X] == 2) 
  {
    dosomethingB;
  }
  else 
  {
    if (Mywords [X] == 3) 
    {
      dosomethingC;
    }
    else 
    {
      if (Mywords [X] == 7) 
      {
        dosomethingD;
      }
      else 
      {
        dosomethingErrorRoutine;
      }
    }
  }
}

And this makes it simpler,

switch ( Mywords[ X ] )
{
  case  1  :
  dosomethingA;
  break;
  case  2  :
  dosomethingB;
  break;
  case  3  :
  dosomethingC;
  break;
  case  7  :
  dosomethingD;
  break;
  default  :  // everything else
  dosomethingErrorRoutine;
}
int Mywords []= {1,2,1,3,2,2,2,3,2,1,1,1,7,1,2,1,3,2,2,2,3,2,1,1,1};

Do the program a favour and save memory by declaring this as an array of bytes rather than ints and while you are at it declare it as const because as far as we have seen your program does not change it

Do yourself a favour by allowing the compiler to calculate the number of entries and use the result as the limit in for loops etc

const byte Mywords []= {1,2,1,3,2,2,2,3,2,1,1,1,7,1,2,1,3,2,2,2,3,2,1,1,1};
const byte NUMBER_OF_ENTRIES = sizeof(Mywords) / sizeof(Mywords[0]);

Doing that ensures that you will not read or write beyond the array and you can add/remove entries and the code will still work without having to find any/all references to its length in the code