Go Down

Topic: [SOLVED]Logging multiple temp sensors questions(step by step) (Read 6 times) previous topic - next topic

vespapierre

Nov 29, 2012, 03:35 pm Last Edit: Dec 13, 2012, 08:05 pm by vespapierre Reason: 1
Modifying this multiple sensors onewire sketch :
http://www.arduino.cc/playground/Learning/OneWire
I would like to log 4 sensors (later write to SD card and send to the web).
The best would be for the logfile later the lines look like :
"temp1,temp2,temp3,temp4,date,time"  - date and time will be here later if this code will be fine.

Now int the code sprintf() looks like:
Code: [Select]
sprintf(buf, "%d:%c%d.%d\337C     ",sensor,SignBit ? '-' : '+', Whole, Fract < 10 ? 0 : Fract);
Serial.print(buf);


Please help how to modify the sketch from the current value.

full sketch at this point is:
Code: [Select]
#include <OneWire.h>

OneWire  ds(3);  // on pin 3
#define MAX_DS1820_SENSORS 4     //sensors max number
byte addr[MAX_DS1820_SENSORS][8];
void setup(void)
{
  Serial.begin(9600);
   Serial.print(F("DS1820 Test"));
 if (!ds.search(addr[0]))
 {
   Serial.print(F("No more addresses."));
   ds.reset_search();
   delay(250);
   return;
 }
 if ( !ds.search(addr[1]))
 {
   Serial.print(F("No more addresses."));
   ds.reset_search();
   delay(250);
   return;
 }
}
int HighByte, LowByte, TReading, SignBit, Tc_100, Whole, Fract;
char buf[20];

void loop(void)
{
 byte i, sensor;
 byte present = 0;
 byte data[12];

 for (sensor=0;sensor<MAX_DS1820_SENSORS;sensor++)
 {
   if ( OneWire::crc8( addr[sensor], 7) != addr[sensor][7])
   {
     Serial.print(F("CRC is not valid"));
     return;
   }

   if ( addr[sensor][0] != 0x10)
   {
     Serial.print(F("Device is not a DS18S20 family device."));
     return;
   }

   ds.reset();
   ds.select(addr[sensor]);
   ds.write(0x44,1);         // start conversion, with parasite power on at the end

   delay(1000);     // maybe 750ms is enough, maybe not
   // we might do a ds.depower() here, but the reset will take care of it.

   present = ds.reset();
   ds.select(addr[sensor]);    
   ds.write(0xBE);         // Read Scratchpad

   for ( i = 0; i < 9; i++)
   {           // we need 9 bytes
     data[i] = ds.read();
   }

   LowByte = data[0];
   HighByte = data[1];
   TReading = (HighByte << 8) + LowByte;
   SignBit = TReading & 0x8000;  // test most sig bit
   if (SignBit) // negative
   {
     TReading = (TReading ^ 0xffff) + 1; // 2's comp
   }
   Tc_100 = (TReading*100/2);    

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

   sprintf(buf, "%d:%c%d.%d\337C     ",sensor,SignBit ? '-' : '+', Whole, Fract < 10 ? 0 : Fract);

// a little sprintf help:  
//%% prints a single % (percent sign).
//%c prints a single character with the given ASCII number (66 is printed as B, for example).
//%s prints a given string
//%d a signed integer in decimal
//%u an unsigned (can't be negative) integer in decimal
//%o an unsigned integer in octal
//%x an unsigned integer in hexadecimal
//%e a floating point number in scientific notation
//%f a floating point number in fixed decimal notation
//%g same as %e or %f, whichever printf thinks is best.
//%X is the same as %x, except it uses upper-case letters
//%E like %e, but with an upper-case 'E'
//%G same as %E when scientific notation is used for the value.
//%p a pointer; it outputs Perl's value address in hexadecimal.
//%n an odd little bugger that *stores* (in the variable given for the position) the number of characters output so far.

   Serial.print(buf);
 }
}


Thanks!

guix

Hello, you can do something like this for example:

Code: [Select]

char buf[128] = "\0"; // IMPORTANT

for (uint8_t i = 0; i < 4; i++)
{
 sprintf(buf, "%s%c%d.%d,", buf,'-', 32, 9);
}

sprintf(buf, "%s%s", buf, "11/12/13-12:34:56");
Serial.println( buf );

The output:
Code: [Select]

-32.9,-32.9,-32.9,-32.9,11/12/13-12:34:56


Adapt to your code :)

vespapierre

great! thanks! I'll try this. Is it not a problem if there is an i using already here:
Code: [Select]
for ( i = 0; i < 9; i++)
    {           // we need 9 bytes
      data[i] = ds.read();
    }


( please note I'm on beginner level :) )

guix

But you must NOT use my loop :) you must just follow the "shape", your code must look like this:
Code: [Select]

char buf[128] = "\0"; // IMPORTANT

for (sensor=0;sensor<MAX_DS1820_SENSORS;sensor++)
{
    // rest of your code here
    //...

    Fract = Tc_100 % 100;

    sprintf(buf, "%s%c%d.%d,", buf, SignBit ? '-' : '+', Whole, Fract < 10 ? 0 : Fract); //modified sprintf
}

sprintf(buf, "%s%s", buf, "11/12/13-12:34:56");
Serial.print(buf); // this moved outside of the loop

vespapierre


Go Up