Take the 2-minute tour ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

I was wondering if you guys could help me solve an arduino Coding problem. I've simplified the code to highlight the issue that I'm having. I've created a program to read streaming serial data to an array. This array needs to be printed at a specific time interval, delays in this case are insufficient, and I have resorted to using timer.h which can be found here http://playground.arduino.cc/Code/Timer . My intention was to read to an array of size index, and increment index. When index becomes a value of 80, it is supposed to save the the array (inData) to another array("prntfd"). ("prntfd") is printed every 5 seconds. In this I hoped to decouple the serial timing, and the print timing of a serial data array.

Unfortunately the code only prints "c", and a print line. The code is below. Any insights would be much appreciated

    //Sketch feeds serial data to array (inData) of size (index). 
//A timer library is then used to print the data every 5 seconds
//debugs: a,b,c are inserted at various parts of the code.
//Serial data from inChar should print every 5 seconds just after C.


//Include timer libraries and serial library
#include <Event.h>
#include <Timer.h>
#include <NewSoftSerial.h>

//set serial pins, serial feed array "inData" and its index "index"
NewSoftSerial nss(3,4);
Timer t;
char inData[80];
static char prntfd[80];
int index = 0;

void setup()
{
  Serial.begin(9600);
  nss.begin(9600);
  t.every(5000, takeReading);//set call time to function "take reading"
}

void loop()
{

  t.update();

  char inChar = nss.read();
  if (index <(80))
  {
    inData[index++] = inChar;
    inData[index] = '\0';
  }
  else if (index == 80)
  inData == prntfd;
  index = 0;
}
void takeReading()
{
  Serial.println("c");
  Serial.println(prntfd);


}
share|improve this question
    
Helping fix your code is off-topic here. –  Winston Ewert Feb 7 '13 at 22:21
add comment

closed as off topic by Glenn Rogers, Jeff Vanzella, svick, palacsint, Yuushi Feb 7 '13 at 1:38

Questions on Code Review Stack Exchange are expected to relate to code review request within the scope defined by the community. Consider editing the question or leaving comments for improvement if you believe the question can be reworded to fit within the scope. Read more about reopening questions here.If this question can be reworded to fit the rules in the help center, please edit the question.

1 Answer

The error is in

inData == prntfd;

First, "==" is a test not an assignment. Hence prntfd is not being updated. Secondly, it is backwards Thirdly, inData and prntfd are char arrays, not a type of "String" where "=" only works on String and not char array.

For char arrays you need to copy blocks of memory with something like the following command using the avrlibc's char * strcpy ( char * dest, const char * src )

strcpy(prntfd, inData);
share|improve this answer
    
    
The answer provided contained a link which has complete descriptions: > The strncpy() function is similar to strcpy(), except that not more than n bytes of src are copied. Thus, if there is no null byte among the first n bytes of src, the result will not be null-terminated. > > In the case where the length of src is less than that of n, the remainder of dest will be padded with nulls. –  mpflaga Feb 7 '13 at 14:17
add comment

Not the answer you're looking for? Browse other questions tagged or ask your own question.