Serial read problem

trying to type something in serial monitor and have it prints out with ending of this char ''; text[x] should be 1 when x is 1; 2 when x is 2; but it doesn't print anything out instead.

code

char text[70];
int x=0;
void setup(){
      Serial.begin(300);
     
}


void loop(){

      if(Serial.available()> 0){
      text[x]=Serial.read();
      x++;
      if(text[x]==char(92)){    //  \, dec: 92, hex: 5C, oct: 134, bin: 1011100  
      Serial.print ("received:");
      Serial.println (text);  
      x=0;
      }
      }
      
      Serial.print("text:");
      Serial.println(text);     
     
      Serial.print("text[x]");
      Serial.println(text[x]);       
      
      Serial.print("x:");
      Serial.println(x);     
      
      Serial.print("----------------------");
      Serial.println();  

          
}

debug results

--------
text:1
text[x]
x:1
--------
text:12
text[x]
x:2
--------
text:123
text[x]
x:3
--------
      text[x]=Serial.read();
      x++;
      if(text[x]==char(92)){    //  \, dec: 92, hex: 5C, oct: 134, bin: 1011100

You are filling in text[x], changing x to x+1, and then comparing text[x+1] to ''.

johnwasser:

      text[x]=Serial.read();

x++;
      if(text[x]==char(92)){    //  , dec: 92, hex: 5C, oct: 134, bin: 1011100




You are filling in text[x], changing x to x+1, and then comparing text[x+1] to '\'.

using '' gives me errors when compiling:

Serial:14: error: missing terminating ' character
Serial.ino: In function 'void loop()':
Serial:15: error: expected `)' before ';' token
Serial.ino: At global scope:
Serial:21: error: expected constructor, destructor, or type conversion before '.' token
Serial:22: error: expected constructor, destructor, or type conversion before '.' token
Serial:24: error: expected constructor, destructor, or type conversion before '.' token
Serial:25: error: expected constructor, destructor, or type conversion before '.' token
Serial:27: error: expected constructor, destructor, or type conversion before '.' token
Serial:28: error: expected constructor, destructor, or type conversion before '.' token
Serial:30: error: expected constructor, destructor, or type conversion before '.' token
Serial:31: error: expected constructor, destructor, or type conversion before '.' token
Serial:40: error: expected declaration before '}' token

using x+1 does gives me text[x] equals to something, but text gives me nothing.

Try this:

char text[70];
int x=0;

void setup(){
  Serial.begin(300);
}


void loop() {
  if (Serial.available()) {
    char c = Serial.read();
    text[x++] = c;
    text[x] = '\0';  // Add null terminator
    if(c == '\\') {
      Serial.print ("received:");
      Serial.println (text);  
      x=0;
    }
    Serial.print("text: ");
    Serial.println(text);     

    Serial.print("text[x-1]: ");
    Serial.println(c);       

    Serial.print("x: ");
    Serial.println(x);     

    Serial.println("----------------------");
  }
}

johnwasser:
Try this:

char text[70];

int x=0;

void setup(){
  Serial.begin(300);
}

void loop() {
  if (Serial.available()) {
    char c = Serial.read();
    text[x++] = c;
    text[x] = '\0';  // Add null terminator
    if(c == '\') {
      Serial.print ("received:");
      Serial.println (text); 
      x=0;
    }
    Serial.print("text: ");
    Serial.println(text);

Serial.print("text[x-1]: ");
    Serial.println(c);

Serial.print("x: ");
    Serial.println(x);

Serial.println("----------------------");
  }
}

This works perfectly, but I don't understand:
1, why x+1? array aren't supposed to start from 0? isn't text[x++] = c; the same thing as text[1] = c; when it starts copying bytes from Serial.read()?

2, text[x] = '\0'; // Add null terminator; won't this overwrite the last character?

3, why need to use another variable char c? since text array is already a char type;

thank you so much

I think you should do done reading on the post-increment operator. That will answer your first two questions. For the third, using char c allows you to not have to put the backslash in the array.

      text[x]=Serial.read();
      x++;
      if(text[x]==char(92)){    //  \, dec: 92, hex: 5C, oct: 134, bin: 1011100

Work out the values of x by hand and you will see what's wrong!

\ is an escape char try '\' and not ''

Mark

arduinomagbit:
1, why x+1? array aren't supposed to start from 0? isn't text[x++] = c; the same thing as text[1] = c; when it starts copying bytes from Serial.read()?

2, text[x] = '\0'; // Add null terminator; won't this overwrite the last character?

3, why need to use another variable char c? since text array is already a char type;

  1. x++ means increment x AFTER the expression has been evaluated:
x = 0;
text[x++] = c;
// is equivalent to:
x = 0;
text[x] = c;
x += 1;
  1. No, the value of x is incremented AFTER it is used.
x = 0;
text[x++] = c;
text[x] = '\0';
// is equivalent to:
x = 0;
text[x] = c;
x += 1;
text[x] = '\0';
  1. Using 'c' to store the received character is easier than storing it in the array and referring to it as element x-1:
x = 0;
text[x++] = Serial.read();
// Now that x has been incremented the character is in text[x-1];
if (text[x-1] == '\\') {

johnwasser:

arduinomagbit:
1, why x+1? array aren't supposed to start from 0? isn't text[x++] = c; the same thing as text[1] = c; when it starts copying bytes from Serial.read()?

2, text[x] = '\0'; // Add null terminator; won't this overwrite the last character?

3, why need to use another variable char c? since text array is already a char type;

  1. x++ means increment x AFTER the expression has been evaluated:
x = 0;

text[x++] = c;
// is equivalent to:
x = 0;
text[x] = c;
x += 1;




2) No, the value of x is incremented AFTER it is used.


x = 0;
text[x++] = c;
text[x] = '\0';
// is equivalent to:
x = 0;
text[x] = c;
x += 1;
text[x] = '\0';




3) Using 'c' to store the received character is easier than storing it in the array and referring to it as element x-1:


x = 0;
text[x++] = Serial.read();
// Now that x has been incremented the character is in text[x-1];
if (text[x-1] == '\')

Thanks, it cleared up everything now