I have a Windows program which sends data to my arduino once a second via a serial port. The data sent to the arduino is simply 3 numbers which are terminated with a carrige return.
arduino captures the serial data into and array. I've then tried converting the array value using the atoi command, but it seems to stop my program running.
I would be grateful for any suggestions on how to convert the serial to a int value or how to get the atoi option workingl
My code is as follows.
const int MAX_LEN = 3; // the max digits in the number plus decimal point
char strValue[MAX_LEN + 1]; // add one for the terminating null
int index = 0;
void loop()
{
millis();
GetAz();
ButtonPress();
llegirPos();
delay(100);
}
void GetAz(){
if( Serial.available())
{
char ch = Serial.read();
if(index < MAX_LEN && ch != 13) // 13 is ascii value of carriage return
{
strValue[index++] = ch; // add the ASCII character to the string;
}
else
{
// here when buffer full or on the first non digit
strValue[index] = 0; // terminate the string with a 0
int value = atoi(strValue); // use atof to convert the string int
index = 0; // reset the index for the next string
lcd.setCursor(10,3);
Serial.println(strValue);
lcd.print(strValue);
}
}
}
what inputs have you tried over the serial port and what has the response been?
I'm wondering if maybe the serial program you're using isn't actually sending the carriage return character.
Assuming there is at least one character that is not a digit seperating the numbers, you could do something like this:
int value = 0;
void GetAz(){
if( Serial.available())
{
char ch = Serial.read();
if(ch >= '0' && ch <= '9') // is ch a number?
value = value * 10 + ch - '0'; // yes, accumulate the value
else
{
if(value > 0)
{
lcd.setCursor(10,3);
Serial.println(value);
lcd.print(value);
}
value = 0; // reset the value on a non digit
}
}
}
mem's suggestion could still be made to work, if you have control of the sending program. If the sender always sent 3 digit numbers (360 or 012 or 005), you could simply loop to read all 3 digits. If the program sent "360" or " 12" or " 5", you could treat the spaces as 0s.
13 (the carriage return) is not an ascii numeric digit and will identify the end of the numeric sequence. Did you try the code? ( I must admit I have not, but if you are sending a carriage return between values it should work)
It looks like you are missing the carriage return (ascii 13) that you were expecting. You need some way to delimit the messages you receive, otherwise you can not be sure you are starting at the beginning of a message. What is sending the data?
Also, where are those -1 values coming from? are you running the code I posted?
Hi mem it's a windows Vb application i wrote. I think it's definately sending it, as the carrige return is what is used to store the data in the array strValue which works perfectly when doinf a serial serial print with my code.
I'm wondering if i can add the ascii value manually in your look then try the atoi function.
I'm wondering if i can add the ascii value manually in your look then try the atoi function.
My advice is to get the incoming data working as expected. If you are sending an carriage return, why is the read routine not displaying it. Can you post the code that you used to get the output in reply #9
AWOL, we know that was 3 threes. We wan't to find out what happened to the carriage return that is supposed to follow and why -1 is being printed if Serial.available is being called before every read.
My apologies, it looks like my source sending the data wasn't actually sending the carrige return after all. it looks like i forgoat to save some changes in my code before closing, and then came back and compiled the next day missing the carrige return.