String sent via serial printing every letter on new line

Hi,

I've got arduino reading text sent from Applescript and hopefully I'm packaging that code properly thanks to PaulS tips and tricks that I've read on this forum. This is a big set for me so, many thanks! :slight_smile:

The couple of issues I have at the moment are, When I view the serial in arduino, every time I send the text it just adds it to the line.
i.e. I get: hello worldhello worldhello worldhello world
If I use Serial.println I then get

h
e
l
l
o

w
o
r
l
d

I'm new at this so bare with me, but I'm thinking that "hello world" is being put into an array and the each letter is being printed by Serial.println rather than the whole string.
How would I get the whole string to print on a new line in serial each time?

Heres my code:
Note this is a bit of a mash of PaulS sketch and an LCD sketch as I'm also trying to get this string (hello world sent from applescript) to print to the LCD.

(eventually I that will be the track name from itunes)

// include the library code:
#include <LiquidCrystal.h>

#define SOP '<'
#define EOP '>'

bool started = false;
bool ended = false;

char inData[80];
byte index;


// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

void setup() {
  // set up the LCD's number of columns and rows: 
  lcd.begin(16, 2);
  Serial.begin(9600);
}

void loop() {
  // set the cursor to column 0, line 1
  // (note: line 1 is the second row, since counting begins with 0):
  lcd.setCursor(0, 1);
  // print the number of seconds since reset:
  lcd.print(millis()/1000);
  
    // Read all serial data available, as fast as possible
  while(Serial.available() > 0)
  {
    char inChar = Serial.read();
    if(inChar == SOP)
    {
       index = 0;
       inData[index] = '\0';
       started = true;
       ended = false;
    }
    else if(inChar == EOP)
    {
       ended = true;
       break;
    }
    else
    {
      if(index < 79)
      {
        inData[index] = inChar;
        index++;
        inData[index] = '\0';
      }
    }

    // set the cursor to column 0, line 0
    lcd.setCursor(0, 0);
    Serial.println(inChar);
    lcd.print(inChar);

  }
  
  // We are here either because all pending serial
  // data has been read OR because an end of
  // packet marker arrived. Which is it?
  if(started && ended)
  {
    // The end of packet marker arrived. Process the packet

    // Reset for the next packet
    started = false;
    ended = false;
    index = 0;
    inData[index] = '\0';
  }
}

The other problem I have is that only the last letter of the string "hello world" is being printed to the top line of my LCD.
Is this also because of the way the string is being packaged?

Many thanks for any help.

I'm new at this so bare with me, but I'm thinking that "hello world" is being put into an array and the each letter is being printed by Serial.println rather than the whole string.

It is being put in an array (inData) AND being printed by Serial.println().

Stop printing each character. In the if(started && ended) block, add Serial.println(inData); instead.

The other problem I have is that only the last letter of the string "hello world" is being printed to the top line of my LCD.
Is this also because of the way the string is being packaged?

That is because you reset the cursor before printing each character.

Thanks PaulS.

I've now got the code in the if(started && ended) statement and all is working as I wanted :slight_smile:

  if(started && ended)
  {
    // The end of packet marker arrived. Process the packet
    Serial.println(inData);
    lcd.setCursor(0, 0);
    lcd.print(inData);
    // Reset for the next packet
    started = false;
    ended = false;
    index = 0;
    inData[index] = '\0';
  }

Thanks again for all the time you obviously put in on the forum. It really helps out people like me. Keep up the great work :wink:

Cheers