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.
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.
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;
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.
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;
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;
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';
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] == '\\') {
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;
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] == '\')