I got a sensor that send me 16 bits in ascii values, the sensor send the values in ascii (between 500 and 64000). If the sensor is on middle range (32000) i get with the fuction Serial1.read() that same values in hexadecimal value of the ascii so i recive 51 50 48 48 48 (3 2 0 0 0).
I need that value (32000) to make math fuctions with that so i cant use a string to do that, so, anyone have any ideia what can i do?
char c;
void loop() {
int i = 0;
char buf[32];
portOne.write('x');
delay(200);
do {
c = portOne.read();
if(c != -1)
buf[i] = c;
i++;
delay(10);
} while ( c != -1);
Serial.print("x = ");
Serial.print((buf));
and that gave anwers like
ldadswdnajsndlka x = 32000
the number was correct and i like that, but the just needed to put off the "iuhuiasdansdn",
then i tried with the atoi fuction just changing the last line for Serial.print(atoi(buf));
it that gave me:
x = 32000
x = -32000
its working for -500 to -32000 then it goes to 32000 and get lowering to 500 for the other side of the inclinometer.
That wasnt bad but what i need is 500 to 64000 (and thats what sensor send i tested it on realterm).
Any idea what it could be ?
thanks
do {
c = portOne.read();
if(c != -1)
buf[i] = c;
i++;
delay(10);
} while ( c != -1);
Serial.print("x = ");
Serial.print((buf));
buf is not a string. You should not be passing it to functions, like atoi() or Serial.print() that expect strings.
A string is a NULL terminated array of chars. buf IS an array of chars, but it is NOT NULL terminated, therefore it is NOT a string.
You need to fix that.
You should also learn about Serial.available().
You should also learn about sending end-of-record markers, and reading properly delimited records. Then, you would need that stupid delay() in the loop.
The atoi() function converts a string (which you don't have) to an int (a signed int). The strtoul() function deals with unsigned longs. That might be more useful.