Problems with reading serial into an array.
hi all,
i've got problem i'm desperate solve , have been struggling time.
i have telescope read it's position in degrees (0-359) every second through serial cable , write array.
my arduino program check's contents of array every 2 mins displays on lcd (e.g 159). continuous loop, checking array every 2 mins.
my problem first read of array works fine (e.g. 159), 2mins later postion of telescope hasn't changed , should still read 159, i'm doing wrong array get's truncated , read 15 instead of 159).
the value serial 3 numbers stored array.
my code below
[code]
// include library code:
#include <liquidcrystal.h>
// initialize library numbers of interface pins
liquidcrystal lcd(12, 11, 5, 4, 3, 2);
unsigned long previousmillis;
void setup()
{
lcd.begin(20, 4);
serial.begin(9600);
}
const int max_len = 4; // max digits in number plus decimal point
char strvalue[max_len + 1]; // add 1 terminating null
int index = 0;
int x;
int y;
int z = 360;
int result;
int movesteps;
void loop()
{
lcd.setcursor(9,3);
lcd.print(x);
getaz();
x = y;
printdome();
printaz();
buttonpress();
llegirpos();
if (millis() - previousmillis > 120000){
previousmillis = millis();
getvalue2();
printdome();
void getaz(){
if( serial.available())
{
char ch = serial.read();
if(index < max_len && ch != 13) // 13 ascii value of carriage return
{
strvalue[index++] = ch; // add ascii character string;
}
else
{
// here when buffer full or on first non digit
strvalue[index] = '\0'; // terminate string 0
index = 0; // reset index next string
}
}
}
void getvalue2(){
y = atoi(strvalue); // use atof convert string float
lcd.setcursor(9,3);
lcd.print(y);
previousmillis = millis();
}
i've got problem i'm desperate solve , have been struggling time.
i have telescope read it's position in degrees (0-359) every second through serial cable , write array.
my arduino program check's contents of array every 2 mins displays on lcd (e.g 159). continuous loop, checking array every 2 mins.
my problem first read of array works fine (e.g. 159), 2mins later postion of telescope hasn't changed , should still read 159, i'm doing wrong array get's truncated , read 15 instead of 159).
the value serial 3 numbers stored array.
my code below
[code]
// include library code:
#include <liquidcrystal.h>
// initialize library numbers of interface pins
liquidcrystal lcd(12, 11, 5, 4, 3, 2);
unsigned long previousmillis;
void setup()
{
lcd.begin(20, 4);
serial.begin(9600);
}
const int max_len = 4; // max digits in number plus decimal point
char strvalue[max_len + 1]; // add 1 terminating null
int index = 0;
int x;
int y;
int z = 360;
int result;
int movesteps;
void loop()
{
lcd.setcursor(9,3);
lcd.print(x);
getaz();
x = y;
printdome();
printaz();
buttonpress();
llegirpos();
if (millis() - previousmillis > 120000){
previousmillis = millis();
getvalue2();
printdome();
void getaz(){
if( serial.available())
{
char ch = serial.read();
if(index < max_len && ch != 13) // 13 ascii value of carriage return
{
strvalue[index++] = ch; // add ascii character string;
}
else
{
// here when buffer full or on first non digit
strvalue[index] = '\0'; // terminate string 0
index = 0; // reset index next string
}
}
}
void getvalue2(){
y = atoi(strvalue); // use atof convert string float
lcd.setcursor(9,3);
lcd.print(y);
previousmillis = millis();
}
apparently, got cut off before code posted.
the getaz function concerns me. getaz may called thousands of times per second. if data has been placed in serial buffer, getaz read whatever there, regardless if there represents complete packet. ideally, keep reading data until end of packet (the 13) has arrived.
in addition, strvalue array not null-terminated after each character added. therefore, when atoi called, in getvalue2, function not guaranteed return meaningful data.
the getaz function concerns me. getaz may called thousands of times per second. if data has been placed in serial buffer, getaz read whatever there, regardless if there represents complete packet. ideally, keep reading data until end of packet (the 13) has arrived.
in addition, strvalue array not null-terminated after each character added. therefore, when atoi called, in getvalue2, function not guaranteed return meaningful data.
Arduino Forum > Forum 2005-2010 (read only) > Software > Syntax & Programs > Problems with reading serial into an array.
arduino
Comments
Post a Comment