[SOLVED] EMIC 2 Butchers Text (One letter at a time!)

Delta_G:
How's it work now?

There is no speech, and Serial2 returns nothing but empty lines.

Delta_G:
print inChar immediately after you read it. See what that says it is.

Odd... It returns nothing! (when I enter 'hi!') But when I restart the program and enter '*' it exits like it's supposed to in the initial if statement...

Oh wait never mind. I typed Serial instead of Serial2.
It returns:

h
i
!

Some Serial Monitor output:

inChar: 
h
inData: 
inChar: 
i
inData: 
inChar: 
!
inData:

Notice how inData is nothing, but inChar is something! What the heck?

Current Code:

void program2() {
  Serial2.println(">Running Program 2");
  Serial2.println("All data lines closed during this operation");
  Serial2.println("===TEXT TO SPEECH===");
  Serial2.println("Enter '*' to Exit");
  char inData[20]; // Allocate some space for the string
  char inChar = -1; // Where to store the character read
  byte index = 0; // Index into array; where to store the character
  char endMarker = '!';
  char Exit = '*';

  while (1 < 2) {
    while (Serial.available() > 0) {
      inChar = Serial.read(); // Read a character
      Serial.println("inChar: ");
      Serial.print(inChar);
      if (inChar == Exit) {
        Serial.println(">Program Complete");
        return;
      }

      else if (inChar != endMarker && inChar < 19) // One less than the size of the array
      {
        inData[index] = inChar; // Store it
        index++; // Increment where to write next
        inData[index] = '\0'; // Null terminate the string
        Serial.println("inData: ");
        Serial.print(inData);
      } else {
        inData[index] = '\0'; // terminate the string
        index = 0;
        Serial.println("inData: ");
        Serial.print(inData);
        emic.speak(inData);
      }
    }
  }
}

Delta_G:

Serial.println("inData: ");

This line is the same in two places, the else if and the else. Make them different somehow and try one more time so we can see which direction the code is going. Maybe it's not hitting the else if section.

An interesting thought... This is so weird. If you can't figure it out, then I don't think I can... Yikes.

Should I try re-writing this code? (if we can't find the mistake)

I changed the second "inData" (In the else statement) to "inData2"

It's exactly the opposite of what you said!!! Woah! :slight_smile: :slight_smile: :slight_smile:

There are no "inData"s displayed on the monitor. There are ONLY "inData2"s

What should I do to correct this?

inChar: 
h
inData2: 

inChar: 
i

inData2: 

inChar: 
!

inData2:

How is this line ever going to return true?

      if (inChar == Exit) {

Edit: I see it now :slight_smile:

Isn't 'index' counting the length of the array?

      else if (inChar != endMarker && inChar < 19) // One less than the size of the array

indev2:
Isn't 'index' counting the length of the array?

      else if (inChar != endMarker && inChar < 19) // One less than the size of the array

OH! DUH!
You mean replace "inChar" (in that line) with "index"

Yes! Thank you @indev for finding the error! Fresh eyes when looking at code can sometimes help!

Also, thank you so much @Delta_G for enlightening me on more complex serial communication! You were REALLY helpful! (Probably one of the best conversations/experiences I've had on the forums so far)

Would it be OK to expand the Char array to accept more characters? (Perhaps 30 instead of 20?) Or would that use too much memory or something? What's the limit?

stupid-questions:
Would it be OK to expand the Char array to accept more characters? (Perhaps 30 instead of 20?) Or would that use too much memory or something? What's the limit?

Limits are proportional to the amount of SRAM you have on your MCU to run your whole code, and you should always aim to have some headroom. Another 10 bytes for your array sounds reasonable. As you're using predetermined lengths, you have some control at least, arrays are so much better than Strings :slight_smile:
https://www.arduino.cc/en/Tutorial/Memory

From looking at it, it seems as if it accepts single chars... but not char arrays.

What do you think this overload accepts?

	void speak(char *msg);

Hint: That's a pointer to a char or char array.

Delta_G:
Can't believe I didn't see that one.

It happens to the best of us!

PaulS:
Hint: That's a pointer to a char or char array.

That's what I thought, but I wasn't sure

indev2:
Another 10 bytes for your array sounds reasonable. As you're using predetermined lengths, you have some control at least, arrays are so much better than Strings :slight_smile:
https://www.arduino.cc/en/Tutorial/Memory

Ok, thanks!