Pages: 1 2 [3]   Go Down
Author Topic: String or Char Arrays Length Definition  (Read 1944 times)
0 Members and 1 Guest are viewing this topic.
UK
Offline Offline
Shannon Member
****
Karma: 184
Posts: 11183
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

array of 9 elements = 8 valid values. right?

No. The first element is array[0]. Count up to nine elements and see which index values you used.

According to your explanation, there would be 10.

No it wouldn't. Actually do the experiment I suggested. Write down the array index values for an array with nine elements. The first element is array[0]. The second element is array[1]. Keep going until you have listed nine elements, and see what range of numbers you have used within the square brackets.
Logged

I only provide help via the forum - please do not contact me for private consultancy.

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 549
Posts: 46154
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
I would love to have just the ERROR ONCE. Any ideas, please!!!!
Just the usual one. Post the code that generated that output.

Define what is sending the data, with what end of packet marker.
Logged

Offline Offline
Newbie
*
Karma: 1
Posts: 32
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I thought it was clear from before:

Code:
char Name[9] = ""; // My Data array
char inChar; // Where to store the character read
byte index = 0; // Index into array; where to store the character


void setup(){

Serial.begin(9600);

}

void loop(){
  if (Serial.available()>0){
     inChar = Serial.read();
     Name[index] = inChar; // Store it
     if (index>8){
         Serial.println("Invalid Input, please enter 3 to 8 characters");
         index = -1;
         Name[9] = '\0';
         memset(Name, 0, sizeof(Name));
     }
     else if (int(Name[index])==13){  //If Carriage return has been reached
         Serial.println(Name);   
         index = -1; 
         Name[9] = '\0';
         memset(Name, 0, sizeof(Name));         
     }
   index++; // Increment where to write next
  }
}

The inputs are introduced via Serial Monitor. The Outputs are printed lines in the Serial Monitor as well.
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 549
Posts: 46154
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

That code is doing exactly what it should, rather than what you want. You need to answer all the questions if you expect us to help you. Especially the "and with what end of packet marker".

That is important because you should stop reading and storing when it arrives. If that happens with less than 8 characters before it (and more than 2), great. If not, then print the error message, but keep reading and discarding data until that happens.
Logged

Offline Offline
Newbie
*
Karma: 1
Posts: 32
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I'm trying to do so, English is my second language and I'm not fluent in C++ either.

I don't know what to do with this phrase:
Quote
"and with what end of packet marker"

I understand the logic of stopping reading but don't know how... I tried using a Serial Reset as soon as it enters the if(index>smiley-cool [Serial.end() & Serial.begin()] with no luck. I just changed in the Serial Monitor the option beside the baud rate from 'No line ending' to 'Carriage return'.

I'm not being lazy I sometimes don't get everything 'I'm supposed to'...
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 549
Posts: 46154
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
I don't know what to do with this phrase:
You are using the Serial Monitor to send data. At the bottom of the panel, there is a drop down list with options for appending data to what is in the Send box. What option do you have selected? Anything other than none causes the Serial Monitor application to append something to what you enter.

That something can be detected on the Arduino, and used as the end of packet marker. We need to know what that something is.
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 549
Posts: 46154
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

If you are using "Carriage return" as the option, try something like this:

Code:
char inData[9];
byte index = 0;
bool errorPrinted = false;

void loop()
{
   while(Serial.available() > 0)
   {
       char c = Serial.read();
       if(char == '\n')
       {
          // Got the carriage return
          // Use the data in inData, if index in range

          index = 0;
          inData[index] = '\0';
          errorPrinted = false;
       }
       else
       {
          if(index < 8)
          {
             inData[index++] = c;
             inData[index] = '\0';
          }
          else
          {
             if(!errorPrinted)
             {
                 // Print an error

                 errorPrinted = true;
             }
          }
       }
   }
}
Logged

Pages: 1 2 [3]   Go Up
Jump to: