Show Posts
Pages: 1 ... 12 13 [14] 15 16 ... 19
196  Forum 2005-2010 (read only) / Syntax & Programs / Some simple code help.... on: March 04, 2010, 05:22:12 am
Hi,

I have been 'playing' with datalogging and getting some results.  I have tried tweaking the code but just don't know enough yet to get it right.... hoping someone might have a couple of mo's to point me in the right direction.

The two things I am trying to achieve are:
Include the YEAR in the date (not coming out right at the mo - I think it is something to do with the buffer? - Year was not in the original code)
Change the sample time from 1 second to 5 seconds (eventually I would like to use a variable to set the sample time in seconds... or maybe its already doing this?)

Thanks for any help - I am learning... slowly :-)

The data as output at the moment:

01/03//0, 17:05:24, 016
01/03//0, 17:05:25, 015
01/03//0, 17:05:26, 016
01/03/30, 17:05:27, 015
01/03/30, 17:05:28, 015



The Sketch:

Code:
#include "FileLogger.h"
#include "DS1307.h"
#include <WProgram.h>
#include <Wire.h>

#define Timing 0
#define Accept  1
#define Record  2


byte start[7]= { 'B','e','g','i','n',0x0D,0x0A};
byte buffer[23];
int temp;
byte ASCII[10]={'0','1','2','3','4','5','6','7','8','9'};
unsigned char result;
unsigned char state;
int time=0;
int now=0;
int oldtime=0;


void setup(void)
{
  result = FileLogger::append("data.txt", start, 7);//Initial the SD Card
  while(result)  result = FileLogger::append("data.txt", start, 7);
  RTC.stop();
  RTC.set(DS1307_MIN,05);     //set the minutes
  RTC.set(DS1307_HR,17);       //set the hours
  RTC.set(DS1307_DATE,01);       //set the date
  RTC.set(DS1307_MTH,03);        //set the month
  RTC.set(DS1307_YR,10);         //set the year
  RTC.start();
}

void loop(void)
{
  
  switch(state)
  {
  case Timing:

        time=RTC.get(DS1307_SEC,true);
        delay(200);
        if(time!=oldtime)
        {
          oldtime=time;    
          temp=RTC.get(DS1307_DATE,false);
          buffer[0]=ASCII[(temp/10)];
          buffer[1]=ASCII[(temp%10)];
          buffer[2]='/';
          temp=RTC.get(DS1307_MTH,false);
          buffer[3]=ASCII[(temp/10)];
          buffer[4]=ASCII[(temp%10)];
          buffer[5]='/';
          temp=RTC.get(DS1307_YR,false);
          buffer[6]=ASCII[(temp/10)];
          buffer[7]=ASCII[(temp%10)];
          buffer[8]=',';
          temp=RTC.get(DS1307_HR,false);
          buffer[9]=ASCII[(temp/10)];
          buffer[10]=ASCII[(temp%10)];
          buffer[11]=':';
          temp=RTC.get(DS1307_MIN,false);
          buffer[12]=ASCII[(temp/10)];
          buffer[13]=ASCII[(temp%10)];
          buffer[14]=':';
          //temp=RTC.get(DS1307_SEC,false);
          buffer[15]=ASCII[(time/10)];
          buffer[16]=ASCII[(time%10)];
          buffer[17]=',';
          state=Accept;
        }
    break;

  case Accept:
     temp=analogRead(0);
     buffer[18]=ASCII[(temp/100)];
     buffer[19]=ASCII[((temp%100)/10)];
     buffer[20]=ASCII[(temp%10)];
     buffer[21]=0x0D;
     buffer[22]=0x0A;
     state=Record;
    break;

  case Record:
    result = FileLogger::append("data.txt", buffer, 23);
    if (result==0)
    {
      state=Timing;
    }
    break;

  default:
    state=Timing;
    break;

  }

}
197  Forum 2005-2010 (read only) / Syntax & Programs / Re: LCD/KEY/Temperture code help on: October 09, 2009, 11:45:57 am
Hi Reggie,

That's fantastic - worked a treat, much appreciated.  I will try and construct the simple getCurrentTemp method you suggested so as to learn how it works - hopefully I will start to understand this more complex code a bit better after that :-)

Thanks again
198  Forum 2005-2010 (read only) / Syntax & Programs / LCD/KEY/Temperture code help on: October 09, 2009, 10:52:38 am
Hi,

I have just had some noobie success setting up a temperture sensor outputting to an LCD :-)  The example code requires a key press to refresh the reading.

Would anyone have a couple of mins to show how I might get the code to update the temperature display say every second (ie without a key press)?  Also, is it possible to add degress (symbol)C after the reading on the dsiplay?

The kit I'm using is here: http://www.nuelectronics.com/estore/index.php?main_page=product_info&cPath=1&products_id=2

And the code is:

//example use of LCD4Bit_mod library

#include <LCD4Bit_mod.h>
#include <stdio.h>

#define TEMP_PIN 3
//create object to control an LCD.  
//number of lines in display=1
LCD4Bit_mod lcd = LCD4Bit_mod(2);

void getCurrentTemp(char *temp);

//Key message
int  adc_key_val[5] ={30, 150, 360, 535, 760 };
int NUM_KEYS = 5;
int adc_key_in;
int key=-1;
int oldkey=-1;

void setup() {
  pinMode(13, OUTPUT);  //we'll use the debug LED to output a heartbeat

   // initialize DS18B20 datapin
    digitalWrite(TEMP_PIN, LOW);
    pinMode(TEMP_PIN, INPUT);      // sets the digital pin as input (logic 1)
  lcd.init();
  //optionally, now set up our application-specific display settings, overriding whatever the lcd did in lcd.init()
  //lcd.commandWrite(0x0F);//cursor on, display on, blink on.  (nasty!)
   lcd.clear();
  lcd.printIn("Temperature is");
    
}

void loop() {

  char temp_string[10];
  adc_key_in = analogRead(0);    // read the value from the sensor  
  key = get_key(adc_key_in);                    // convert into key press
      
 if (key != oldkey)                            // if keypress is detected
 {
    delay(50);            // wait for debounce time
    adc_key_in = analogRead(0);    // read the value from the sensor  
    key = get_key(adc_key_in);                    // convert into key press
    if (key != oldkey)                        
    {                  
      oldkey = key;
      getCurrentTemp(temp_string);
      lcd.cursorTo(2, 0);  //line=2, x=0
      lcd.printIn(temp_string);
    }
 }
 
  
  
}

// Convert ADC value to key number
int get_key(unsigned int input)
{
      int k;
    
      for (k = 0; k < NUM_KEYS; k++)
      {
            if (input < adc_key_val[k])
            {
          
    return k;
        }
      }
  
    if (k >= NUM_KEYS)
        k = -1;     // No valid key pressed
    
    return k;
}


void OneWireReset(int Pin) // reset.  Should improve to act as a presence pulse
{
     digitalWrite(Pin, LOW);
     pinMode(Pin, OUTPUT); // bring low for 500 us
     delayMicroseconds(500);
     pinMode(Pin, INPUT);
     delayMicroseconds(500);
}

void OneWireOutByte(int Pin, byte d) // output byte d (least sig bit first).
{
   byte n;

   for(n=8; n!=0; n--)
   {
      if ((d & 0x01) == 1)  // test least sig bit
      {
         digitalWrite(Pin, LOW);
         pinMode(Pin, OUTPUT);
         delayMicroseconds(5);
         pinMode(Pin, INPUT);
         delayMicroseconds(60);
      }
      else
      {
         digitalWrite(Pin, LOW);
         pinMode(Pin, OUTPUT);
         delayMicroseconds(60);
         pinMode(Pin, INPUT);
      }

      d=d>>1; // now the next bit is in the least sig bit position.
   }
  
}

byte OneWireInByte(int Pin) // read byte, least sig byte first
{
    byte d, n, b;

    for (n=0; n<8; n++)
    {
        digitalWrite(Pin, LOW);
        pinMode(Pin, OUTPUT);
        delayMicroseconds(5);
        pinMode(Pin, INPUT);
        delayMicroseconds(5);
        b = digitalRead(Pin);
        delayMicroseconds(50);
        d = (d >> 1) | (b<<7); // shift d to right and insert b in most sig bit position
    }
    return(d);
}


void getCurrentTemp(char *temp)
{  
  int HighByte, LowByte, TReading, Tc_100, sign, whole, fract;

  OneWireReset(TEMP_PIN);
  OneWireOutByte(TEMP_PIN, 0xcc);
  OneWireOutByte(TEMP_PIN, 0x44); // perform temperature conversion, strong pullup for one sec

  OneWireReset(TEMP_PIN);
  OneWireOutByte(TEMP_PIN, 0xcc);
  OneWireOutByte(TEMP_PIN, 0xbe);

  LowByte = OneWireInByte(TEMP_PIN);
  HighByte = OneWireInByte(TEMP_PIN);
  TReading = (HighByte << smiley-cool + LowByte;
  sign = TReading & 0x8000;  // test most sig bit
  if (sign) // negative
  {
    TReading = (TReading ^ 0xffff) + 1; // 2's comp
  }
  Tc_100 = (6 * TReading) + TReading / 4;    // multiply by (100 * 0.0625) or 6.25

  whole = Tc_100 / 100;  // separate off the whole and fractional portions
  fract = Tc_100 % 100;

/*
      if(sign) temp[0]='-';
      else              temp[0]='+';
      
      temp[1]= whole%100+'0';
      temp[2]= (whole-100*temp[1])%10 +'0' ;
      temp[3]= whole-100*temp[1]-10*temp[2] +'0';
      
      temp[4]='.';
      temp[5]=fract%10 +'0';
      temp[6]=fract-temp[5]*10 +'0';
      
      temp[7] = '\0';
*/

      sprintf(temp, "%c%3d%c%2d", (sign==0)?'+':'-', whole, '.', fract);
      
}      
      
      
      
199  Forum 2005-2010 (read only) / Interfacing / Re: Serial read from sensor on: November 02, 2009, 06:33:30 am
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.
200  Forum 2005-2010 (read only) / Interfacing / Re: Serial read from sensor on: November 01, 2009, 05:50:05 pm
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.
201  Forum 2005-2010 (read only) / Interfacing / Re: Serial read from sensor on: November 01, 2009, 04:15:15 pm
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.
202  Forum 2005-2010 (read only) / Interfacing / Re: Serial read from sensor on: November 01, 2009, 02:11:50 pm
Thanks Paul, it all makes perfect sense now :-) - I can see when the crash is happening now - at 96 loops this time !!
203  Forum 2005-2010 (read only) / Interfacing / Re: Serial read from sensor on: November 01, 2009, 01:41:40 pm
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);

}
204  Forum 2005-2010 (read only) / Interfacing / Re: Serial read from sensor on: October 30, 2009, 09:28:20 am
Printing to the serial window rather than the LCD also produces a crash - first try after 35 samples, second time after 49 samples.  The sensor is still blinking away though.
205  Forum 2005-2010 (read only) / Interfacing / Re: Serial read from sensor on: October 30, 2009, 08:44:21 am
Hmmm - this routine has been running happily for an hour or so - that's with the sensor connected.  Not sure what to do next?

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

#define address 0x31

#define delayC 1000

char co_str[10];

void setup()
{
  
  lcd.init();
  lcd.clear();
  lcd.printIn("test");
}

void loop()
{
  
  for (int co=0; co >= 0; co++){
  
  lcd.cursorTo(2, 0);
  lcd.printIn(itoa((long)co,co_str,10));
  lcd.printIn(" number  ");
  delay(delayC);
  }
}
206  Forum 2005-2010 (read only) / Interfacing / Re: Serial read from sensor on: October 30, 2009, 05:40:43 am
Thanks AWOL - will give it a try.
207  Forum 2005-2010 (read only) / Interfacing / Re: Serial read from sensor on: October 30, 2009, 05:32:16 am
ahhh... new problem !!!  It's all working very nicely printing out to an LCD but..... after a few short minutes it seems to crash - ie it all looks the same but the value remains the same (even when I know it should be changing)  Any ideas what could be going on?

The code used is:

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];

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

  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((long)co,co_str,10));
  lcd.printIn(" ppm  ");
  delay(delayC);
}

void getI2Cdata()
{
  delay(2);
  Wire.requestFrom(address, data_bytes);
  while (!Wire.available ());
  for (int i = 0; i < data_bytes; ++i) {
  data_buffer [i] = Wire.receive();
  }
}
  
208  Forum 2005-2010 (read only) / Interfacing / Re: Serial read from sensor on: October 29, 2009, 06:51:54 am
Well, GOD MEMBER is about right - you have fixed it - very, very much appreciated.

Thanks for your input on this and sorry for my lack of skill - I now know a little more :-)

Cheers
209  Forum 2005-2010 (read only) / Interfacing / Re: Serial read from sensor on: October 29, 2009, 06:27:17 am
Hi Awol,

Thanks for the advice - I substituted your code into the script but I'm getting errors beyond my ability.  It will be my lack of syntax/knowledge !!  Would you be up for patching it into the original code?
210  Forum 2005-2010 (read only) / Interfacing / Re: Serial read from sensor on: October 28, 2009, 05:06:25 pm
Ideally I would get I2C working :-)  I am using 4K7 pullups to 5V and analog pins 4 & 5.  The code below (written by another helpful soul)returns a result but the bytes returned are all 8's with a PPM: 2056 !!!

IE:
CO PPM: 2056
Data received:8,8,8,8,8,8,8,


Code:
#include <Wire.h>
//wire library

#define address 0x31
//address of the sensor

#define delayC 5000
//delay count in ms

#define baudrate 9600
//baudrate for communication

void setup()
{
  Wire.begin();
  Serial.begin(baudrate);
}

void loop()
{
  //Serial.print("CO PPM: ");
  
  int co=0;

  Wire.beginTransmission(address);
  // start the transmission
  
  Wire.send('R');
    
  Wire.endTransmission();
  //end the transmission
  
 /// Wire.requestFrom(address, 7);
  
  // format
  // 1) Configuration 0x08
  // 2) CO (high byte?)
  // 3) CO (low byte?)
  // 4) reserved
  // 5) reserved
  // 6) reserved
  // 7) reserved
  
  Serial.print ("Data received:");
  for (int i=0;i<7;i++)
  {
    byte c;
    c = getI2Cchar();
    Serial.print(c,HEX);
    Serial.print(',');
    if (i==1) co = c;
    if (i==2) co = (co << 8) | c;
  }
  Serial.println("");
  Serial.print("CO PPM: ");
  Serial.println(co);

  delay(delayC);
}

byte getI2Cchar()
{
  delay(2); // delay 2mS
  Wire.requestFrom(address, 1);
  return (Wire.receive());
}
Pages: 1 ... 12 13 [14] 15 16 ... 19