Go Down

Topic: Multiple sensors (DS18S20), RTC & SD Card (Read 39 times) previous topic - next topic

GrantNZ

Nov 02, 2014, 02:22 am Last Edit: Nov 02, 2014, 09:53 am by robtillaart
Hi There,

I am a novice at Arduino, and get by at looking at others code and 'hacking' it to suit.  Recently I have attempted to get multiple senors to write to a SD card.  I managed to get the sensors being detected, and the RTC working, but when I add the code to about the SD setup in the VOID setup, I get a loop in the VOID loop.

The code below works fine without the SD card code in the void setup, but once added, it gets stuck at the //set time delay loop.  Keeps returning Test point 1 & test point 2.

Any help appreciated

Code: [Select]

#include <OneWire.h>
#include "RTClib.h"
#include <Wire.h>
#include "SD.h"

File logfile;
RTC_DS1307 RTC;


char *names[] = {"Upper","Lower"};
// DS18S20 Temperature chip i/o
OneWire ds(12);  // on pin 12

void setup(void) {
 // initialize inputs/outputs
 // start serial port
 Serial.begin(9600);
 Wire.begin();
 RTC.begin();
 
 if (! RTC.isrunning()) {
   Serial.print("RTC is NOT running!");
 }
{  

Serial.print("Initializing SD card...");
 // On the Ethernet Shield, CS is pin 4. It's set as an output by default.
 // Note that even if it's not used as the CS pin, the hardware SS pin
 // (10 on most Arduino boards, 53 on the Mega) must be left as an output
 // or the SD library functions will not work.
  pinMode(10, OUTPUT);
 
 if (!SD.begin(10)) {
   Serial.println("initialization failed!");
   return;
 }
 Serial.println("initialization done.");

 // create a new file
char filename[] = "LOGGER00.txt";
 for (uint8_t i = 0; i < 100; i++) {
 filename[6] = i/10 + '0';
 filename[7] = i%10 + '0';
if (! SD.exists(filename)) {
 
 // only open a new file if it doesn't exist
   logfile = SD.open(filename, FILE_WRITE);
   break; // leave the loop!
   }
 }
 
logfile = SD.open("test.txt", FILE_WRITE);

 // if the file opened okay, write to it:
 if (logfile) {
   Serial.print("Writing to test.csv...");
   logfile.println("SD card is working");

   // close the file:
   logfile.close();
   Serial.println("done.");
 
} else {
 
   // if the file didn't open, print an error:
   Serial.println("error opening test.txt");
   }
 }
}

int d_temp(byte data[12])
{
     word TReading, Tc_100,SignBit;
     TReading = (data[1] << 8) + data[0];
     SignBit = TReading & 0x8000;  // test most sig bit
     if (SignBit) // negative
     {
       TReading = (TReading ^ 0xffff) + 1; // 2's comp
     }
     Tc_100 = (6 * TReading) + TReading / 4;    // multiply by (100 * 0.0625) or 6.25
     return Tc_100;
}

void loop() {


   for (int z=0; z <= 2; z++){
 
   
 byte i;
 byte present = 0;
 byte data[12];
 byte addr[8];
 word secs;
 
 static int sensor=0;
 if ( !ds.search(addr)) {
     Serial.print("\n"); //No more
     ds.reset_search();
     
   Serial.println("Test point 1");    
     
//    Set time delay      
     for(secs=0; secs < 5; secs++)
        delay(1000);   // 1000 = 1sec
     
     sensor = 0;
     Serial.println("Test point 2");  
     return;
     
 }

{
 if ( OneWire::crc8( addr, 7) != addr[7]) {
     Serial.print("CRC is not valid!\n");
     return;
 }
}

 if ( addr[0] == 0x10) {
     Serial.print("Device is a DS18S20 family device.\n");
 }
 else if ( addr[0] == 0x28) {
   int Whole, Fract;
   int Tc_100;
    Tc_100 = d_temp(data);
   
   
    Whole = Tc_100 / 100;  // separate off the whole and fractional portions
    Fract = Tc_100 % 100;

     if(sensor != 0)
       Serial.print(", ");
       
     if(sensor > sizeof(names)/sizeof(char *))
        Serial.print("Sensor with no name");
     else        
        Serial.print(names[sensor]);
     Serial.print(" = ");
     Serial.print(Whole);
     Serial.print(".");
     if (Fract < 10)
       Serial.print("0");
     Serial.print(Fract);
     sensor++; // Move on to the next sensor
 }
 else {
     Serial.print("Device family is not recognized: 0x");

     Serial.print("\n");
     return;
 }
 

 ds.reset();
 ds.select(addr);
 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);    
 ds.write(0xBE);         // Read Scratchpad
 for ( i = 0; i < 9; i++) {           // we need 9 bytes
   data[i] = ds.read();
 }
 
 OneWire::crc8( data, 8);
 
    {
if (z == 1){

   DateTime now = RTC.now();
   
   Serial.print(", ");
   Serial.print(now.year(), DEC);
   Serial.print('/');
   Serial.print(now.month(), DEC);
   Serial.print('/');
   Serial.print(now.day(), DEC);
   Serial.print(' ');
   Serial.print(now.hour(), DEC);
   Serial.print(':');
   Serial.print(now.minute(), DEC);
   Serial.print(':');
   Serial.print(now.second(), DEC);
   //Serial.println();
     }
   }  
 }
}

moderater: added code tags...please use them second icon from right

filk


its a bit messy, from what i see the for loop encompasing all your code within your main loop is to print out the start time.  but will if you get that far end up printing the time data every other pass through the main loop..

also there looks like an extra {} pair embedded in your startup function after the rtc check?

and after test point2 you have return; that will send you back to the top of the main loop back down to test point 2 only to then return back to the top of the loop.................

Go Up