Combining final sketch

I need assistance combining these two sketches for my weather station project (both of them are working perfectly but when I try to combine, the serial monitor doesnt show anything. I have already tried different combinations but it seems I cant get it).

This first sketch belongs to the a rain gauge attached to my adafruit datalogging RTC, SDCard shield:

#include <SPI.h>
#include <SD.h>          //Ensure that you downlaod these libraries and store them in your Arduino libraries folder 
#include <Wire.h>        //Remember no semi-colon after these commands
#include "RTClib.h"


const int REED = 9;     //The reed switch outputs to digital pin 9
int val = 0;            //Current value of reed switch
int old_val = 0;        //Old value of reed switch
int REEDCOUNT = 0;      //The intial count is zero

const int chipSelect = 10;
File logfile;
#define SYNC_INTERVAL 60000
uint32_t syncTime = 0; // time of last sync()


RTC_Millis rtc; /////RTC_DS1307 RTC; // define the Real Time Clock object


/////////////Program Set-up/////////////////////////////////////////////////////////////////////

void setup(void){
  
  Serial.begin(9600);
  pinMode (REED, INPUT_PULLUP);  //Activate the internal pull-up resistor
 

  
/////////////Setting up the file on the SD Card///////////////////////////////

  Serial.println("Initializing SD card...");
  pinMode(10, OUTPUT);  // make sure that the default chip select pin is set to output
  
  
  if (!SD.begin(chipSelect)) {  // see if the card is present and can be initialized:
    Serial.println("Card failed, or not present");
  }
  Serial.println("card initialized.");
  
  // create a new file
  char filename[] = "LOGGER00.CSV";      //The file names must be in the 8.3 format
  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!
    }
  }
  
  if (! logfile) {
    Serial.println("could not create file");
  }
  
  Serial.print("Logging to: ");
  Serial.println(filename);
  
  rtc.begin(DateTime(F(__DATE__), F(__TIME__)));
   /////rtc.adjust(DateTime(2015, 7, 7, 12, 40, 0));
  
Serial.println("Rainfall, Reading/count, date, time");
logfile.println("Rainfall, Reading/count, date, time");  
}
///////////////////The Program Loop//////////////////////////////////////////////////////////////////////
void loop(){
  

 DateTime now = rtc.now(); /////DateTime now;    // and record date/time of change
  
  val = digitalRead(REED);
 

  
  if ((val == LOW) && (old_val == HIGH))//Low means that the Reed switch is open (which happens when the magnet passess).
  
 {
 delay(10);    // Delay put in to deal with any "bouncing" in the switch.
 

  
 char buf1[50];
 
 Serial.print("Precipitation");
 Serial.print("= ");
 REEDCOUNT = REEDCOUNT + 10;
 old_val = val;

 Serial.print(REEDCOUNT);
 Serial.print(" mm of rain at ");
 
 sprintf(buf1, "%02d:%02d:%02d of %02d/%02d/%02d",  now.hour(), now.minute(), now.second(), now.day(), now.month(), now.year());
 Serial.println(buf1);
 
 logfile.print("Precipitation");
 logfile.print("= ");
 logfile.print("Precipitation");
 logfile.print(". ");
 logfile.println(buf1);
 

}
  else {
    old_val = val; //If the status hasn't changed then do nothing
  }

  if ((millis() - syncTime) < SYNC_INTERVAL) return;

 
  logfile.flush();

  syncTime = millis();
 
 }

And this one belongs to the 4 waterproof DS18B20 temperature sensors:

#include <OneWire.h>

//Get DallasTemperature Library here:  http://milesburton.com/Main_Page?title=Dallas_Temperature_Control_Library
#include <DallasTemperature.h>

/*-----( Declare Constants and Pin Numbers )-----*/
#define ONE_WIRE_BUS_PIN 2

/*-----( Declare objects )-----*/
// Setup a oneWire instance to communicate with any OneWire devices
OneWire oneWire(ONE_WIRE_BUS_PIN);

// Pass our oneWire reference to Dallas Temperature.
DallasTemperature sensors(&oneWire);

/*-----( Declare Variables )-----*/
// Assign the addresses of your 1-Wire temp sensors.
// See the tutorial on how to obtain these addresses:
// http://www.hacktronics.com/Tutorials/arduino-1-wire-address-finder.html

DeviceAddress Probe01 = { 0x28, 0x5A, 0xB7, 0x00, 0x00, 0x00, 0x80, 0x6D }; 
DeviceAddress Probe02 = { 0x28, 0x5C, 0xB7, 0x00, 0x00, 0x00, 0x80, 0xDF };
DeviceAddress Probe03 = { 0x28, 0x12, 0x6D, 0x73, 0x06, 0x00, 0x00, 0x27 };
DeviceAddress Probe04 = { 0x28, 0xB2, 0xCB, 0x5E, 0x06, 0x00, 0x00, 0x39 };



void setup()   /****** SETUP: RUNS ONCE ******/
{
  // start serial port to show results
  Serial.begin(9600);
  Serial.print("Initializing temp. sensors");
  Serial.println("Reading 4 probes");
  
  
  sensors.begin();// Initialize the Temperature measurement library
  
  
  sensors.setResolution(Probe01, 10);// set the resolution to 10 bit (Can be 9 to 12 bits .. lower is faster)
  sensors.setResolution(Probe02, 10);
  sensors.setResolution(Probe03, 10);
  sensors.setResolution(Probe04, 10);


}//--(end setup )---

void loop()   /****** LOOP: RUNS CONSTANTLY ******/
{
  delay(1000);
  Serial.println();
  Serial.print("Number of Devices found on bus = ");  
  Serial.println(sensors.getDeviceCount());   
  Serial.print("Getting temperatures... ");  
  Serial.println();   
  
    
  sensors.requestTemperatures();  // Command all devices on bus to read temperature 
  
  Serial.print("Probe 01 temperature is:   ");
  printTemperature(Probe01);
  Serial.println();

  Serial.print("Probe 02 temperature is:   ");
  printTemperature(Probe02);
  Serial.println();
 
  Serial.print("Probe 03 temperature is:   ");
  printTemperature(Probe03);
  Serial.println();
   
  Serial.print("Probe 04 temperature is:   ");
  printTemperature(Probe04);
  Serial.println();
  

  
}//--(end main loop )---

/*-----( Declare User-written Functions )-----*/
void printTemperature(DeviceAddress deviceAddress)
{

float tempC = sensors.getTempC(deviceAddress);

   if (tempC == -127.00) 
   {
   Serial.print("Error getting temperature  ");
   } 
   else
   {
   Serial.print("C: ");
   Serial.print(tempC);
   Serial.print(" F: ");
   Serial.print(DallasTemperature::toFahrenheit(tempC));
   }
}// End printTemperature
//*********( THE END )***********

Thanks

This Simple Merge Demo may help.

If you can't get it to work please post your best attempt at the merged code and tell us exactly what it does and does not do.

...R

Hi Robin,

This is my best attempt so far. I have previously read your post about combinations so I think that I would need to modify something related with millis-delay in the rain gauge sketch.

Both sketches work perfectly independently but with this combination the serial monitor does not show anything apart from …failed.

/*-----( Declare Libraries )-----*/

#include <SPI.h>
#include <SD.h>          //Ensure that you downlaod these libraries and store them in your Arduino libraries folder 
#include <Wire.h>        //Remember no semi-colon after these commands
#include "RTClib.h"
#include <OneWire.h>
#include <DallasTemperature.h> //Get DallasTemperature Library here:  http://milesburton.com/Main_Page?title=Dallas_Temperature_Control_Library

/*-----( Declare Constants and Pin Numbers )-----*/
const int REED = 9;     //The reed switch outputs to digital pin 9
int val = 0;            //Current value of reed switch
int old_val = 0;        //Old value of reed switch
int REEDCOUNT = 0;      //The intial count is zero

const int chipSelect = 10;
File logfile;
#define SYNC_INTERVAL 60000
#define ONE_WIRE_BUS_PIN 2
uint32_t syncTime = 0; // time of last sync()

/*-----( Declare objects )-----*/
RTC_Millis rtc; /////RTC_DS1307 RTC; // define the Real Time Clock object
OneWire oneWire(ONE_WIRE_BUS_PIN);// Setup a oneWire instance to communicate with any OneWire devices
DallasTemperature sensors(&oneWire); // Pass our oneWire reference to Dallas Temperature.

/*-----( Declare Variables )-----*/
DeviceAddress Probe01 = { 0x28, 0x5A, 0xB7, 0x00, 0x00, 0x00, 0x80, 0x6D };// Assign the addresses of your 1-Wire temp sensors. 
DeviceAddress Probe02 = { 0x28, 0x5C, 0xB7, 0x00, 0x00, 0x00, 0x80, 0xDF };// See the tutorial on how to obtain these addresses:
DeviceAddress Probe03 = { 0x28, 0x12, 0x6D, 0x73, 0x06, 0x00, 0x00, 0x27 };// http://www.hacktronics.com/Tutorials/arduino-1-wire-address-finder.html
DeviceAddress Probe04 = { 0x28, 0xB2, 0xCB, 0x5E, 0x06, 0x00, 0x00, 0x39 };

/////////////Program Set-up/////////////////////////////////////////////////////////////////////

void setup(void){ /****** SETUP: RUNS ONCE ******/
  
  Serial.begin(9600);// start serial port to show results
  pinMode (REED, INPUT_PULLUP);  //Activate the internal pull-up resistor
 

  
/////////////Setting up the file on the SD Card///////////////////////////////

  Serial.println("Initializing SD card...");
  pinMode(10, OUTPUT);  // make sure that the default chip select pin is set to output
  
  
  if (!SD.begin(chipSelect)) {  // see if the card is present and can be initialized:
    Serial.println("Card failed, or not present");
  }
  Serial.println("card initialized.");  // create a new file
  
  char filename[] = "LOGGER00.CSV";      //The file names must be in the 8.3 format
  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!
    }
  }
  
  if (! logfile) {
    Serial.println("could not create file");
  }
  
  Serial.print("Logging to: ");
  Serial.println(filename);
  
  rtc.begin(DateTime(F(__DATE__), F(__TIME__)));
   /////rtc.adjust(DateTime(2015, 7, 7, 12, 40, 0));
  
Serial.println("Rainfall, Reading/count, date, time");
logfile.println("Rainfall, Reading/count, date, time"); 

 Serial.begin(9600);
 Serial.print("Initializing temp. sensors");
 Serial.println("Reading 4 probes");
  
  
 sensors.begin();// Initialize the Temperature measurement library
  
  
 sensors.setResolution(Probe01, 10);// set the resolution to 10 bit (Can be 9 to 12 bits .. lower is faster)
 sensors.setResolution(Probe02, 10);
 sensors.setResolution(Probe03, 10);
 sensors.setResolution(Probe04, 10);


}//--(end setup )---


///////////////////The Program Loop//////////////////////////////////////////////////////////////////////
void loop(){ //****** LOOP: RUNS CONSTANTLY ******/
  

 DateTime now = rtc.now(); /////DateTime now;    // and record date/time of change
  
  val = digitalRead(REED);
 

  
  if ((val == LOW) && (old_val == HIGH))//Low means that the Reed switch is open (which happens when the magnet passess).
  
 {
 delay(10);    // Delay put in to deal with any "bouncing" in the switch.
 

  
 char buf1[50];
 
 Serial.print("Precipitation");
 Serial.print("= ");
 REEDCOUNT = REEDCOUNT + 10;
 old_val = val;

 Serial.print(REEDCOUNT);
 Serial.print(" mm of rain at ");
 
 sprintf(buf1, "%02d:%02d:%02d of %02d/%02d/%02d",  now.hour(), now.minute(), now.second(), now.day(), now.month(), now.year());
 Serial.println(buf1);
 
 logfile.print("Precipitation");
 logfile.print("= ");
 logfile.print("Precipitation");
 logfile.print(". ");
 logfile.println(buf1);
 

}
  else {
    old_val = val; //If the status hasn't changed then do nothing
  }

  if ((millis() - syncTime) < SYNC_INTERVAL) return;
  logfile.flush();
  syncTime = millis();

  {
  delay(1000);
  Serial.println();
  Serial.print("Number of Devices found on bus = ");  
  Serial.println(sensors.getDeviceCount());   
  Serial.print("Getting temperatures... ");  
  Serial.println();   
  
    
  sensors.requestTemperatures();  // Command all devices on bus to read temperature 
  
  Serial.print("Probe 01 temperature is:   ");
  printTemperature(Probe01);
  Serial.println();

  Serial.print("Probe 02 temperature is:   ");
  printTemperature(Probe02);
  Serial.println();
 
  Serial.print("Probe 03 temperature is:   ");
  printTemperature(Probe03);
  Serial.println();
   
  Serial.print("Probe 04 temperature is:   ");
  printTemperature(Probe04);
  Serial.println();
  
  }
  
}//--(end main loop )---

 /*-----( Declare User-written Functions )-----*/
void printTemperature(DeviceAddress deviceAddress)
{

float tempC = sensors.getTempC(deviceAddress);

   if (tempC == -127.00) 
   {
   Serial.print("Error getting temperature  ");
   } 
   else
   {
   Serial.print("C: ");
   Serial.print(tempC);
   Serial.print(" F: ");
   Serial.print(DallasTemperature::toFahrenheit(tempC));
   }
}// End printTemperature

Thanks

I will try to look at that later today. I have bookmarked it.

...R

Thank you so much Robin! I really appreciate it!

Try this

/*-----( Declare Libraries )-----*/

#include <SPI.h>
#include <SD.h>          //Ensure that you downlaod these libraries and store them in your Arduino libraries folder 
#include <Wire.h>        //Remember no semi-colon after these commands
#include "RTClib.h"
#include <OneWire.h>
#include <DallasTemperature.h> //Get DallasTemperature Library here:  http://milesburton.com/Main_Page?title=Dallas_Temperature_Control_Library

/*-----( Declare Constants and Pin Numbers )-----*/
const int REED = 9;     //The reed switch outputs to digital pin 9
int val = 0;            //Current value of reed switch
int old_val = 0;        //Old value of reed switch
int REEDCOUNT = 0;      //The intial count is zero

const int chipSelect = 10;
File logfile;
#define SYNC_INTERVAL 60000
#define ONE_WIRE_BUS_PIN 2
uint32_t syncTime = 0; // time of last sync()

/*-----( Declare objects )-----*/
RTC_Millis rtc; /////RTC_DS1307 RTC; // define the Real Time Clock object
OneWire oneWire(ONE_WIRE_BUS_PIN);// Setup a oneWire instance to communicate with any OneWire devices
DallasTemperature sensors(&oneWire); // Pass our oneWire reference to Dallas Temperature.

/*-----( Declare Variables )-----*/
DeviceAddress Probe01 = { 
  0x28, 0x5A, 0xB7, 0x00, 0x00, 0x00, 0x80, 0x6D };// Assign the addresses of your 1-Wire temp sensors. 
DeviceAddress Probe02 = { 
  0x28, 0x5C, 0xB7, 0x00, 0x00, 0x00, 0x80, 0xDF };// See the tutorial on how to obtain these addresses:
DeviceAddress Probe03 = { 
  0x28, 0x12, 0x6D, 0x73, 0x06, 0x00, 0x00, 0x27 };// http://www.hacktronics.com/Tutorials/arduino-1-wire-address-finder.html
DeviceAddress Probe04 = { 
  0x28, 0xB2, 0xCB, 0x5E, 0x06, 0x00, 0x00, 0x39 };

/////////////Program Set-up/////////////////////////////////////////////////////////////////////

void setup(void){ /****** SETUP: RUNS ONCE ******/

  Serial.begin(9600);// start serial port to show results
  pinMode (REED, INPUT_PULLUP);  //Activate the internal pull-up resistor

  /////////////Setting up the file on the SD Card///////////////////////////////

  Serial.println("Initializing SD card...");
  pinMode(10, OUTPUT);  // make sure that the default chip select pin is set to output


  if (!SD.begin(chipSelect)) {  // see if the card is present and can be initialized:
    Serial.println("Card failed, or not present");
  }
  Serial.println("card initialized.");  // create a new file

  char filename[] = "LOGGER00.CSV";      //The file names must be in the 8.3 format
  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!
    }
  }

  if (! logfile) {
    Serial.println("could not create file");
  }

  Serial.print("Logging to: ");
  Serial.println(filename);

  rtc.begin(DateTime(F(__DATE__), F(__TIME__)));
  /////rtc.adjust(DateTime(2015, 7, 7, 12, 40, 0));

  Serial.println("Rainfall, Reading/count, date, time");
  logfile.println("Rainfall, Reading/count, date, time"); 

  //Serial.begin(9600);
  Serial.print("Initializing temp. sensors");
  Serial.println("Reading 4 probes");


  sensors.begin();// Initialize the Temperature measurement library


  sensors.setResolution(Probe01, 10);// set the resolution to 10 bit (Can be 9 to 12 bits .. lower is faster)
  sensors.setResolution(Probe02, 10);
  sensors.setResolution(Probe03, 10);
  sensors.setResolution(Probe04, 10);


}//--(end setup )---


///////////////////The Program Loop//////////////////////////////////////////////////////////////////////
void loop(){ //****** LOOP: RUNS CONSTANTLY ******/


  DateTime now = rtc.now(); /////DateTime now;    // and record date/time of change

  val = digitalRead(REED);


  if ((val == LOW) && (old_val == HIGH))//Low means that the Reed switch is open (which happens when the magnet passess).

  {
    delay(10);    // Delay put in to deal with any "bouncing" in the switch.

    char buf1[50];

    Serial.print("Precipitation");
    Serial.print("= ");
    REEDCOUNT = REEDCOUNT + 10;
    old_val = val;

    Serial.print(REEDCOUNT);
    Serial.print(" mm of rain at ");

    sprintf(buf1, "%02d:%02d:%02d of %02d/%02d/%02d",  now.hour(), now.minute(), now.second(), now.day(), now.month(), now.year());
    Serial.println(buf1);

    logfile.print("Precipitation");
    logfile.print("= ");
    logfile.print("Precipitation");
    logfile.print(". ");
    logfile.println(buf1);


  }
  else {
    old_val = val; //If the status hasn't changed then do nothing
  }

  if ((millis() - syncTime) < SYNC_INTERVAL) return;
  logfile.flush();
  syncTime = millis();

  {
    //delay(1000);
    Serial.println();
    Serial.print("Number of Devices found on bus = ");  
    Serial.println(sensors.getDeviceCount());   
    Serial.print("Getting temperatures... ");  
    Serial.println();   


    sensors.requestTemperatures();  // Command all devices on bus to read temperature 

    Serial.print("Probe 01 temperature is:   ");
    printTemperature(Probe01);
    Serial.println();

    Serial.print("Probe 02 temperature is:   ");
    printTemperature(Probe02);
    Serial.println();

    Serial.print("Probe 03 temperature is:   ");
    printTemperature(Probe03);
    Serial.println();

    Serial.print("Probe 04 temperature is:   ");
    printTemperature(Probe04);
    Serial.println();

  }

}//--(end main loop )---

/*-----( Declare User-written Functions )-----*/
void printTemperature(DeviceAddress deviceAddress)
{

  float tempC = sensors.getTempC(deviceAddress);

  if (tempC == -127.00) 
  {
    Serial.print("Error getting temperature  ");
  } 
  else
  {
    Serial.print("C: ");
    Serial.print(tempC);
    Serial.print(" F: ");
    Serial.print(DallasTemperature::toFahrenheit(tempC));
  }
}// End printTemperature

I will wait to see if @BillHo's suggestion works.

...R

Hi BillHo/Robin,

Thanks for your info. As you can see on the pdf attached I can read the values at the serial monitor. I was just needing to remove the second serial.begin! thanks again BillHo.

The problem I have now is that the SDCard is not retaining any data.

I am gonna try to get a different SDCard.

Thanks again guys, I will let you know how I am getting on!

Reading at serial monitor.pdf.pdf (232 KB)

Hi guys,

The SDcard cannot create a file. Should be the red LED on at the Adafruit datalogging shield or just the green LED from the power?

Reading at serial monitor.pdf.pdf (232 KB)

What is that large PDF file?

Have you tried a very simple program to write to the SD Card so you can learn how to do it?

…R

Hi Robin,

The Pdf file is the reading at the serial monitor showing the fail about creating the file on the SD card.

I have to proceed creating a text file named “test.txt” in the card root but I do not know how to do it (neither have not found any info on the net).

Please note that I am using an adafruit datalogging shield SDcard with RTC (see photo attached).Then, I would need to download the SD Fat library and extract to the libraries folder of my Arduino installation.

Could you indicate me that program to write on and learn how to get it done?

Another thing that unknown is that the red LED attached to the adafruit does not stay On for long. It does not say anything about at its website.

Thanks.

Antonio

Have you tried the examples on the SD Card library page

You really should be able to find that sort of thing by yourself. Google !

...R

Robin, I have previously tried SD card libraries but it just giving errors and not recording anything on the SDcard. Also, I have googled it and followed the process but still not getting it.

Ill keep trying.

Thanks

Acachinero:
Robin, I have previously tried SD card libraries but it just giving errors and not recording anything on the SDcard.

Then write a very short program (perhaps one of the examples from the Library) and if it does not work post the code here so we can help with the problem.

...R

Robin2:
Then write a very short program (perhaps one of the examples from the Library) and if it does not work post the code here so we can help with the problem.

...R

The problem is coming with the pin conexions because the red LED (from the SDcard reader) is not blinking, that's why the codes are not working.

I am afraid to start soldering and not getting the pins properly connected but I am gonna assume the risk because I have no another chance.

Thanks,

Antonio

Acachinero:
The problem is coming with the pin conexions because the red LED (from the SDcard reader) is not blinking, that’s why the codes are not working.

I am afraid to start soldering and not getting the pins properly connected but I am gonna assume the risk because I have no another chance.

Post a link to the datasheet for the SDCard reader you are trying to use. And remind me which Arduino board are you using.

…R

Seems I missed the image of the board in Reply #10 - here it is for convenience.

9df662a7754cac73467895a44229d19d40befbed.jpg

But a link to the datasheet is also necessary.

You can not use that shield without soldering the pins. If you tried using it just by pushing the pins in to the board it is possible you have damaged the SD card and / or the shield.

I have just soldered the Shield on my Arduino Uno. It seems to work properly because the red LED blinks when compiling the Sketch but it is not finishing to upload it!

See the pdf attached showing the errors.

I might have damaged the shield.

I guess I need to buy a new shield. :frowning:

errors uploading.pdf (175 KB)

Please post the errors as text so we don't have to download a file.

...R

Robin,

Arduino takes ages uploading and at the end just show this so I cannot read at the serial monitor. The port is correctly selected for my Arduino Uno.

avrdude: stk500_recv(): programmer is not responding
avrdude: stk500_getsync() attempt 1 of 10: not in sync: resp=0x60
avrdude: stk500_recv(): programmer is not responding
avrdude: stk500_getsync() attempt 2 of 10: not in sync: resp=0x60
avrdude: stk500_recv(): programmer is not responding
avrdude: stk500_getsync() attempt 3 of 10: not in sync: resp=0x60
avrdude: stk500_recv(): programmer is not responding
avrdude: stk500_getsync() attempt 4 of 10: not in sync: resp=0x60
avrdude: stk500_recv(): programmer is not responding
avrdude: stk500_getsync() attempt 5 of 10: not in sync: resp=0x60
avrdude: ser_send(): write error: sorry no info avail
Problem uploading to board. See http://www.arduino.cc/en/Guide/Troubleshooting#upload for suggestions.

Sketch uses 11,486 bytes (35%) of program storage space. Maximum is 32,256 bytes.
Global variables use 1,364 bytes (66%) of dynamic memory, leaving 684 bytes for local variables. Maximum is 2,048 bytes.
avrdude: ser_open(): can't open device "\.\COM3": The system cannot find the file specified.

Problem uploading to board. See http://www.arduino.cc/en/Guide/Troubleshooting#upload for suggestions.