Go Down

Topic: Serial read from sensor (Read 4704 times) previous topic - next topic

point5

Hmmmm, I'm still trying to find out where the problem is.... I've tried cutting the code down so that it does the bare minimum to display the data required but I don't think it's right?  I have also inserted a sample counter to see when the crash happens but my noobness can't even get that right!!!  it counts 1,2,3,10,11,12,13,20 :-(

Yikes

Code: [Select]
#include <LCD4Bit_mod.h>
LCD4Bit_mod lcd = LCD4Bit_mod(2);

#include <Wire.h>

#define address 0x31

#define delayC 2000

#define baudrate 9600

char co_str[10];

char sam_str[4];

int sam;

int co;



const int data_bytes = 7;
byte data_buffer [data_bytes];

void setup()
{
 Wire.begin();
 Serial.begin(baudrate);
 lcd.init();
 lcd.clear();
 lcd.printIn("Value");
}

void loop()
{
 Wire.beginTransmission(address);
 Wire.send('R');
 Wire.endTransmission();
 
 delay(2);
 Wire.requestFrom(address, data_bytes);
 while (!Wire.available ());
 for (int i = 0; i < data_bytes; ++i)
 data_buffer [i] = Wire.receive();
 

 for (int i=0;i<7;i++)
 {
   byte c = data_buffer [i];
   if (i==1) co = c;
   if (i==2) co = (co << 8) | c;
 }
 lcd.cursorTo(2, 0);
 lcd.printIn(itoa(co,co_str,10));
 lcd.cursorTo(2, 5);
 lcd.printIn("ppm");
 sam = (sam++);
 lcd.cursorTo(2, 10);
 lcd.printIn(itoa(sam,sam_str,4));
 
 delay(delayC);

}

PaulS

The itoa function's final argument is the base of the conversion, not the number of digits in the value.

So, sam, at least is displaying correctly.

point5

#32
Nov 01, 2009, 08:11 pm Last Edit: Nov 01, 2009, 08:14 pm by point5 Reason: 1
Thanks Paul, it all makes perfect sense now :-) - I can see when the crash is happening now - at 96 loops this time !!

PaulS

I went back and read the entire thread. I don't understand all of it, but I understand enough, I think.

I'm confused about the crashing part. What's crashing? The arduino? The PC that the arduino is attached to? Something else?

point5

Hi Paul, its the Arduino that crashes (or at lease that's what appears to be happening - the display stops updating but the sensor LED keeps blinking as expected).  Now with the loop counter working I can see that the crash has happened after the following counts:

61,32,10,56,33,10,56,47,11,101,34

I thought a pattern was emerging but I don't think so now.

point5

Hmmm, I'm starting to wonder if this is a timming issue?  Is it possible to get arduino to wait until the bytes are delivered from the sensor before continuing? - the sensor transmits evey 3 seconds.

point5

WORKING :-)

The issue was the delay - changed from 2 to 50.

It would still be good to try and ask Arduino to wait until data is available before printing anything - just not quite sure if this is poss and how?

Thanks for all the help - it is very much appreciated.

Go Up