Arduino wait for characters from serial monitor input

Hello,

I would like to use the arduino's serial monitor's input to give data to my project. However, when I tried to input a paragraph of characters, I could only input less than a sentence. Would appreciate guidance as I am new to these things. Eg: input 50 words, output 10 words.

char message1[1000];

if (Serial.available())
  {
    for (int i=0; i<1000;i++)
    if((message1[i] = Serial.read()) == -1) 
      { 
        message1[i] = 0; 
        break; 
      }

    Serial.print("Message: ");
    Serial.print(message1);
  }

I suspect that it takes time for the input to input into message1. Serial.read()=-1 occurs before the entire input can be inputted into message1, thus it cuts off the paragraph into less than a sentence.

char message1[1000];

if (Serial.available())
{
for (int i=0; i<1000;i++)
message1[i] = Serial.read(); //Input alphabets or numbers

Serial.print("Message: ");
Serial.print(message1);
}

But it produces gibberish to cover the unused spaces I declared. eg: input hello, message1= helloYYYYYYYYYYYYYY

Hope to get a reply soon! Thanks! Appreciate the guidance.

I'm sure you did not type in italics :wink: Please post code using code tags. Edit your post

Place
** **[code]** **
before the code.
Place
** **[/code]** **
after the code.

And please don't post snippets but full code.

There is an internal software buffer for the receivingg of data; size is (I think) 64 bytes. So after 64 bytes, data will be lost.

Also, you check if one or more bytes are available; it does not mean that 1000 bytes are available.

I suggest that you read Serial Input Basics - Updated; and stop flooding the Arduino with data.

You can use software handshake (xon/xoff) as I described in Reading instructions from a file - Project Guidance - Arduino Forum. It can not be done with Serial Monitor, but e.g. a program like RealTerm (under Windows) will support it if you set it up correctly.

Hi Sterretje,

Thank you for your reply! Have edited my post. Btw, that technically is my whole code. That's the part that needs editing, the other parts of my main code is not relevant in this issue, thus I did not copy it down.

I have read Serial Input Basics - updated - Introductory Tutorials - Arduino Forum as suggested by you. So I won't be able to send anything more than 64bytes? That's pretty little...

I'm actually using Node-Red to input data to the serial port, I only used the Serial Monitor to check if my string input from Node-Red has successfully became message1 in my code. Thus, I feel RealTerm is not needed as I have a way to input data to the serial port other than the arduino serial monitor.

Is there anyway you can recommend which allows me to input huge bytes of characters to a variable? Basically fully utilizing the "char message1[1000]". Like message1 can be a paragraph of words?

Hope to hear from you soon! Thanks so much!

Btw, that technically is my whole code.

Nonsense.

You need to become acquainted with Serial.available and some of the serial handling tutorials.

(By the way, they're not 'Y's - look closely)

kenchia10:
I have read Serial Input Basics - updated - Introductory Tutorials - Arduino Forum as suggested by you. So I won't be able to send anything more than 64bytes? That's pretty little...

This is wrong.

The constant numChars at the top of my example programs determines how many characters you have space for. Change it to suit your requirement.

...R

You are aware, I hope, that the 1000 character array will use half of your SRAM, on any 329-based Arduino.

The Arduino is not really meant to be a word processor.

Robin2:
This is wrong.

The constant numChars at the top of my example programs determines how many characters you have space for. Change it to suit your requirement.

...R

I was referring to the software RX buffer in HardwareSerial, not to yours :wink:

sterretje:
I was referring to the software RX buffer in HardwareSerial, not to yours :wink:

I had not seen that. I was responding to the OP's comment. I think he has mixed up the hardware serial buffer and the buffer in his own program.

...R

AWOL:
Nonsense.

You need to become acquainted with Serial.available and some of the serial handling tutorials.

(By the way, they're not 'Y's - look closely)

Yeah it is not exactly 'Y', it is 'ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ', have no idea what it is.
This is the whole code of mine, almost the same as what I sent.

char message1[1000];

void setup() 
{
  // Open serial communications and wait for port to open:
  Serial.begin(9600);
}

void loop() 
{
  if (Serial.available())
  {
    for (int i=0; i<1000;i++)
    if((message1[i] = Serial.read()) == -1) 
      { 
        message1[i] = 0; 
        break; 
      }

    Serial.print("Message: ");
    Serial.println(message1);
  }
 
  else
  {
  }    

  delay(1000);
}

Thank you all for your reply.. Appreciate it.. Sorry I'm new to all these, do not understand what you guys are talking about hahaha

Using the code that I have, I can only send a max of 63 Characters even though I have declared 1000. Anything more than 63 will be lost. Any way to extend this limit?

The odd character is the representation of -1, which is what Serial.read returns when there is nothing to read(which will be most of the time).
So, simply don't read when there's nothing to read, and all will be well.

Read Robin2's serial handling basics tutorial.

Hey,

Thanks for your reply! Yeah have tried out Robin2's code, I guess my code caused the problem of limiting it to 64 bytes.

However, I tried his code and changed the bytes from 32 to 10000 and inputted a string with 1.9k characters, but it failed to print. It only printed the first 14 characters of the 1.9k characters I inputted. Any suggestions on why this is happening?

const byte numChars = 10000;
char receivedChars[numChars];

boolean newData = false;

void setup() {
    Serial.begin(9600);
    Serial.println("<Arduino is ready>");
}

void loop() {
    recvWithStartEndMarkers();
    showNewData();
}

void recvWithStartEndMarkers() {
    static boolean recvInProgress = false;
    static byte ndx = 0;
    char startMarker = '<';
    char endMarker = '>';
    char rc;
 
    while (Serial.available() > 0 && newData == false) {
        rc = Serial.read();

        if (recvInProgress == true) {
            if (rc != endMarker) {
                receivedChars[ndx] = rc;
                ndx++;
                if (ndx >= numChars) {
                    ndx = numChars - 1;
                }
            }
            else {
                receivedChars[ndx] = '\0'; // terminate the string
                recvInProgress = false;
                ndx = 0;
                newData = true;
            }
        }

        else if (rc == startMarker) {
            recvInProgress = true;
        }
    }
}

void showNewData() {
    if (newData == true) {
        Serial.print("This just in ... ");
        Serial.println(receivedChars);
        newData = false;
    }
}

I inputted this

It is the news that Coldplay fans who couldn’t get hold of tickets for the UK band’s sold-out concert in Singapore next year have been yearning for – Chris Martin and co will stage a second concert here on 31 March 2017. In addition, concert promoter Live Nation Lushington said 2,200 extra CAT 5 (Standing Pen A) and 1,000 CAT 6 (Standing Pen B) tickets will be sold for Coldplay’s first concert on 1 April 2017. Tickets for both concert days will go on sale on 25 Nov (Friday) at 10am (Singapore time). There will also be a limit of four tickets across all categories per transaction, the organiser said. They can be purchased via sportshubtix.sg, from the hotline at +65 3158 7888, and at the Sports Hub Tix Box Office at Kallang Wave Mall and all SingPost outlets. The concert is part of the band’s “A Head Full of Dreams” tour, which also happens to be their first appearance in Singapore in eight years. While the National Stadium can hold up to 55,000 people, the maximum number of attendees at a concert held at the venue is around 50,000, due mainly to stage set-up. The demand to catch Coldplay’s performance has led to a rush for allocated and public sale concert tickets, priced from $78 to $298. In the latest batch, up to 14,000 public sale tickets were sold out in less than 90 minutes on Monday, prompting Live Nation Lushington to call it a “never-before-seen response”. Resellers have been offering tickets on e-commerce sites like Carousell at inflated prices, with some going for more than $1,000. Live Nation Lushington warned Coldplay fans on Monday not to buy tickets from resellers. “We will also continue to curtail the secondary tickets being sold at inflated prices. We have since voided a number of tickets found on the resale market as this contravenes our terms and conditions of sale.“We would like to urge all fans to refrain from purchasing tickets through unauthorised resellers as these may have already been voided and holders will be denied access to venue.”

Hope to hear from you soon! Thanks!

const byte numChars = 10000;

Yeah. Good luck with that.

So it is not possible? 10000 is only 10kbps, if you actually compare to file transfer, download speed, , etc. 10kbps is very little/slow.

May I know the max number of characters that the arduino can handle? Will the model of the arduino affect? Am currently using an Uno, I do have a Mega in hand.

Look at your first line

const byte numChars = 10000;

There are two problems right there. First, a byte can only hold numbers up to 255 so you need an int.

Second the TOTAL memory in an Uno is 2000 bytes and a considerable amount of that will be needed for other data so where do you imagine you will find space for 10,000 characters?

A little more thinking seems to be required.

What is your project about?

...R

Yes understand better now. As I am new to all these, I did not know the limitations of the Uno, perhaps I have to research more on that.

What do you mean by "hold numbers up to 255?". 255 of what, Characters?

Well my project requires sending pictures by converting them into string, so I am testing how much data I can input into an arduino. Assuming 1 character=1byte from my contextual knowledge? I am not sure of that...

Appreciate the guidance. Thanks!

kenchia10:
What do you mean by "hold numbers up to 255?". 255 of what, Characters?

You need to do some background reading to understand what a byte is and the other datatypes that are used in computing.

A byte is a unit of memory made up of 8 binary bits. So the maximum value that it can represent is 28-1. In binary arithmetic counting starts at 0 so the 256 values that can be represented (only 1 at a time) in a byte are 0 to 255.

Without studying this very basic stuff you are going to find it very difficult to understand the advice you get here and you may also find it difficult to get your descriptions of problems understood.

...R

kenchia10:
May I know the max number of characters that the arduino can handle? Will the model of the arduino affect? Am currently using an Uno, I do have a Mega in hand.

Read the specs of the boards; https://www.arduino.cc/en/Main/Products; follow links for the respective boards and look for RAM.

kenchia10:
Well my project requires sending pictures by converting them into string, so I am testing how much data I can input into an arduino. Assuming 1 character=1byte from my contextual knowledge? I am not sure of that...

You will have to specify a little better. What must the Arduino do with it once it has received the data?