Go Down

Topic: Need for help : Weird Serial.print and Array read problems (Read 642 times) previous topic - next topic

binooetomo

Feb 04, 2011, 10:12 am Last Edit: Feb 04, 2011, 10:17 am by binooetomo Reason: 1
Dear All.
I have very basic sketch below :
Code: [Select]

#include<string.h>
//-------------VARS to SEND------------------------------
int sendvarlen = 6;
char* sendvarname[6]={"s0","s1","s2","s3","s4","s5"};
char* sendvarval[6]={"0","0","0","0","0","0"};
//-------------------------------------------------
long timetosend=millis();
void setup()
{
 Serial.begin(9600);
 delay(1000);
 Serial.println("I'm Here !!!");
 delay(1000);
}
void loop()
{
 if(millis() - timetosend > 1000)
 {
   readsensor();
   Serial.println("=============================");
   devdata();
   Serial.println("=============================");
   timetosend=millis();
 }
}

void readsensor()
{
 Serial.println("Analog Sensor Read results");
 char myread[10]="";
 for (int i = 0 ; i < sendvarlen ; i++)
 {
   memset(myread, '\0', 10);
   sendvarval[i]="";
   itoa(analogRead(i),myread,10);
   delay(50);
   strcpy(sendvarval[i],myread);
   
   Serial.print(sendvarname[i]);
   Serial.print("=");
   Serial.println(sendvarval[i]);
 }
}

void devdata()
{
 char mydata[500];
 strcpy(mydata, "My Sensordata is : ");
 for (int i=0; i<sendvarlen; i++)
 {
   strcat(mydata,sendvarname[i]);
   strcat(mydata,"=");
   strcat(mydata,sendvarval[i]);
   if (i < sendvarlen-1)
   {
     strcat(mydata,"&");
   }
 }
 Serial.print(mydata);
 Serial.println(" ");
}


And below is the results (dumped using) : tail -f /dev/ttyUSB1

Code: [Select]
Analog Sensor Read results

s0=198

s1=244

s2=351

s3=440

s4=412

s5=273

73

My Sensordata is : s0=273&s1=273&s2=273&s3=273&s4=273&s5=273

73

Analog Sensor Read results

s0=191

s1=262

s2=378

s3=449

s4=381

s5=234

34

My Sensordata is : s0=234&s1=234&s2=234&s3=234&s4=234&s5=234

34



Some weird thing from the results :
1. There is no "=============================" printed out.
2. all the "value" after "My Sensordata is" is just the last value got by readsensor()
- 273 at the 1st result sequence , and
- 234 at the 2st result sequence
3. There is always 2 last character reprinted at the end of readsensor() and devdata()
- 73 at the 1st result sequence , and
- 34 at the 2st result sequence

Kindly please give me your enlightment to fix this problem.

My IDE version : 0022
My Board : Arduino 2009 , ATMega328.
My OS : Ubuntu (Jaunty)

Sincerely
-bino-

PaulS

Code: [Select]
    strcpy(sendvarval[i],myread);
sendvarval[0] is a pointer. It points to space that is initialized to hold one character. You are copying more than one character to that location. Oops.

Try
Code: [Select]
char sendvarval[6][10];
instead.

When you write out of bounds, you are writing over some other memory locations. Potentially you are overwriting the first character of the "=========================" string with a NULL, so when you print that memory space, only the NULL is seen, so nothing gets printed.

binooetomo


Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy