Pages: [1]   Go Down
Author Topic: timer printing and array of serial data issue  (Read 466 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Full Member
***
Karma: 0
Posts: 111
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


   

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

Code:
//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);

 
}
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 613
Posts: 49270
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
  char inChar = nss.read();
  if (index <(80))
  {
    inData[index++] = inChar;
    inData[index] = '\0';
  }
Read and store a character, even if there is not one to read. Not the best idea I've seen this week.

Why is the 80 in parentheses?

takeReading() is an interrupt service routine. In interrupt service routines, interrupts are disabled. The Serial.print() function stores data in a buffer, and returns, immediately, if there is room in the buffer. If there is not, it blocks waiting for there to be room in the buffer. Since the buffer is not being emptied during an ISR, you should not be putting stuff into it in an ISR.

Bottom line: Do NOT call Serial.print(), Serial.println(), or Serial.write() in an ISR.
Logged

Offline Offline
Full Member
***
Karma: 0
Posts: 111
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Do you have any recommendations or direction for methods I might pursue to do what I am attempting?
Logged

Offline Offline
Full Member
***
Karma: 0
Posts: 111
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I ask b/c it would seem that it isn't possible to use interrupts to time outputs from the micro-controller. Here is an example from the library. Does it only work because any analog values are going to be lower than the 64 byte serial buffer?

Code:
    #include "Timer.h"
     
     
    Timer t;
    int pin = 13;
     
     
    void setup()
    {
      Serial.begin(9600);
      pinMode(pin, OUTPUT);
      t.oscillate(pin, 100, LOW);
      t.every(1000, takeReading);
    }
     
     
    void loop()
    {
      t.update();
    }
     
     
    void takeReading()
    {
      Serial.println(analogRead(0));
    }
Logged

California
Offline Offline
Faraday Member
**
Karma: 88
Posts: 3380
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Do you have any recommendations or direction for methods I might pursue to do what I am attempting?

Using timer interrupts for printing a value every 5 seconds is overkill. The Blink without delay example demonstrates how to do something at an interval without blocking your code.
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 613
Posts: 49270
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Every 5 seconds, plus or minus how many milliseconds? Consider that it will take time to shift out all the data.

You could reduce that time by picking up the speed - 9600 is way too slow.
Logged

UK
Offline Offline
Shannon Member
****
Karma: 223
Posts: 12631
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

This array needs to be printed at a specific time interval, delays in this case are insufficient

In what way are they insufficient?
Logged

I only provide help via the forum - please do not contact me for private consultancy.

Offline Offline
Full Member
***
Karma: 0
Posts: 111
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Paul,

I am looking for something that doesn't rely on how long it takes to shift out the data. I realize that it does take time to shift out the data. i'm only looking to print every 10 seconds. That being said, it needs to be uncoupled from the the looping times, because (in my case) the looping times will vary

Logged

UK
Offline Offline
Shannon Member
****
Karma: 223
Posts: 12631
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

So, code it so that the repeat interval is independent of the loop execution time.
Logged

I only provide help via the forum - please do not contact me for private consultancy.

Pages: [1]   Go Up
Jump to: