Pages: 1 2 [3]   Go Down
Author Topic: Serial read from sensor  (Read 4260 times)
0 Members and 1 Guest are viewing this topic.
UK
Offline Offline
Sr. Member
****
Karma: 1
Posts: 278
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
#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);

}
Logged

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

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.
Logged

UK
Offline Offline
Sr. Member
****
Karma: 1
Posts: 278
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks Paul, it all makes perfect sense now :-) - I can see when the crash is happening now - at 96 loops this time !!
« Last Edit: November 01, 2009, 02:14:35 pm by point5 » Logged

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

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?
Logged

UK
Offline Offline
Sr. Member
****
Karma: 1
Posts: 278
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

UK
Offline Offline
Sr. Member
****
Karma: 1
Posts: 278
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

UK
Offline Offline
Sr. Member
****
Karma: 1
Posts: 278
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Pages: 1 2 [3]   Go Up
Jump to: