My conditional statements aren't working.

So, I am trying to translate a string from the Serial port into morse code. The wiring was easy, but then I tried to work on the code, and I have been trying for a week to make my code work. The problem is that the first 'if' statement in my code seems to execute unconditionally. It just prints "C" over and over again, even though it should only work when I put "a" into it!

Here's my code, for reference:

#define morseLED 2
int i;
char morse[50];
char inChar;

void setup() {
 pinMode(morseLED, OUTPUT);
 Serial.begin(9600);
 while(! Serial);
 Serial.print("please enter your input: ");
 Serial.print("(lower case only please).\n\n");
}

void loop() {
 // enters the string values in
 while(Serial.available() > 0)
 {
     if(i < 19)
     {
         inChar = Serial.read();
         morse[i] = inChar;
         i++;
         morse[i] = '\0';
     }
 }
 samFB();
}
//blinks a "."
void dot() {
 digitalWrite(morseLED, HIGH);
 delay(500);
 digitalWrite(morseLED, LOW);
 delay(100);
}
//blinks a "-"
void dash() {
 digitalWrite(morseLED, HIGH);
 delay(1000);
 digitalWrite(morseLED, LOW);
 delay(100);
}

// holds all values for the translation
void samFB() {
 for(i = 0; i < 24; i++){
   if(morse[i] == "a") {
     dot();
     dash();
     Serial.print(morse[i]); // Problem statement
   }
// rest of the code
}

What do you guys think? Is there a problem with my input code? My functions? I only started this a month ago, and I am at a loss.

PS, the array indicator thingys are bracketed "[i)" so that I don't have to worry about accidentally italicizing my entire code.

In future post your code using the code button </> so it looks like this and is easy to copy to a text editor

#define morseLED 2
int i;
char morse[50];
char inChar;

void setup() {
  pinMode(morseLED, OUTPUT);
  Serial.begin(9600);
  while(! Serial);
  Serial.print("please enter your input: ");
  Serial.print("(lower case only please).\n\n");
}

void loop() {
  // enters the string values in
  while(Serial.available() > 0)
  {
      if(i < 19)
      {
          inChar = Serial.read();
          morse[i) = inChar;
          i++;
          morse[i) = '\0';
      }
  }
  samFB();
}
//blinks a "."
 void dot() {
  digitalWrite(morseLED, HIGH);
  delay(500);
  digitalWrite(morseLED, LOW);
  delay(100);
}
//blinks a "-"
void dash() {
  digitalWrite(morseLED, HIGH);
  delay(1000);
  digitalWrite(morseLED, LOW);
  delay(100);
}

// holds all values for the translation
void samFB() {
  for(i = 0; i < 24; i++){
    if(morse[i) == "a") {
      dot();
      dash();
      Serial.print(morse[i)); // Problem statement
    }
// rest of the code
}

...R

    if(morse[i) == "a") {

      Serial.print(morse[i)); // Problem statement
expected ']' before ')' token

Look closely.

codingTribulations:
PS, the array indicator thingys are bracketed "[i)" so that I don't have to worry about accidentally italicizing my entire code.

That's what code tags are for.

One possible problem is creating the indexing variable i as global. The use of it in loop() may interfere with its use in samFB(). A variable used for that purpose should be local to the section of code where it is used.

Also, in loop() you check if i < 19 but you seem to have no code to set i back to 0

You should also be aware that while(Serial.available() > 0) does not guarantee that all the data arrives, The Arduino can empty the serial input buffer very much faster than data arrives. Have a look at the examples in Serial Input Basics - simple reliable ways to receive data.

...R

First, thank you for the tips on the matter of tagging my code. I used the teletype format, and in case you hadn't noticed, this was the first post that I have made on this forum, so thanks for doubly educating me on these matters!

Second, the tips on my code also seem pretty obvious to me at the moment. I replaced the if with a for. To be honest, I copied that from somebody else, and I actually prefer the for format. Much more succinct.

I made the edits you suggested, and I am going to run my code once I get home. I'll let you know how it goes!

codingTribulations:
in case you hadn't noticed, this was the first post that I have made on this forum,

It is good practice BEFORE your first post to spend some time studying how the Forum operates and reading the How to use the Forum

Second, the tips on my code also seem pretty obvious to me at the moment. I replaced the if with a for. To be honest, I copied that from somebody else, and I actually prefer the for format.

Your preference for displaying code is not relevant. What matters is making it convenient for the people who give up their free time to help you.

...R

Robin2:
Your preference for displaying code is not relevant. What matters is making it convenient for the people who give up their free time to help you.

...R

Fair enough. Sorry for not doing my due diligence on this matter.

I have another issue now. The serial monitor is only printing the first two letters of my first string, and then the code stops. All I get is pl, and then it does not accept my inputs. I did not have this problem previously. Any tips?

Here is my setup code again:

void setup() {
  pinMode(morseLED, OUTPUT);
  Serial.begin(9600);
  while(! Serial);
  Serial.print("please enter your input: ");
  Serial.print("(lower case only please).\n\n");
}

Thank you for the help. I would not have noticed the problem without your advice.

I don't see anything wrong with the snippet in Reply #6 (but I can be blind to obvious problems). What happens if you put it in a program with an empty loop() function?

What happens if you comment out one of the Serial.print() lines? (either one)

...R

   if(morse[i) == "a") {

The character in the ith element of the array will never, in a million years, equal the string "a".

It might, or might not, equal the character 'a'.