Go Down

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


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 :-(


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()

void loop()
 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.cursorTo(2, 5);
 sam = (sam++);
 lcd.cursorTo(2, 10);



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.


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 !!


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?


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:


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


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.



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