help me combine two sketches...

I have a Dallas 1-wire simple sketch that reports the temperature from two different sensors... I am on a UNO R3 with a Ethernet shield plugged in. I just got a SD card for it and plugged it into the ethernet sheild.

I would like to learn how to record that data to the SD card, something simple so that I can graph it later..

column 1: some number value starting at 1
column 2: the internal temperature sensor
column 3: the external temperature sensor

Dallas code below:

#include <OneWire.h>
#include <DallasTemperature.h>

// Data wire is plugged into pin 2 on the Arduino
#define ONE_WIRE_BUS 2

// Setup a oneWire instance to communicate with any OneWire devices
// (not just Maxim/Dallas temperature ICs)
OneWire oneWire(ONE_WIRE_BUS);

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

void setup(void)
{
// start serial port
Serial.begin(9600);

// Start up the library
sensors.begin();
}

void loop(void)
{
delay(5000);

sensors.requestTemperatures();
Serial.print("External Temperature is: ");
Serial.print(sensors.getTempCByIndex(0) * 1.8 + 32.0); // Convert to F
Serial.print('\n');
Serial.print("Internal Temperature is: ");
Serial.print(sensors.getTempCByIndex(1) * 1.8 + 32.0); // Convert to F
Serial.print('\n');

}

SD card below (the temp sensors are on pin 2)... this code example I think was for like 3 different sensors... I just need to learn how to adopt it... thank you!

#include <SPI.h>
#include <SD.h>

const int chipSelect = 2;

void setup() {
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}

Serial.print("Initializing SD card...");

// see if the card is present and can be initialized:
if (!SD.begin(chipSelect)) {
Serial.println("Card failed, or not present");
// don't do anything more:
return;
}
Serial.println("card initialized.");
}

void loop() {
// make a string for assembling the data to log:
String dataString = "";

// read three sensors and append to the string:
for (int analogPin = 0; analogPin < 3; analogPin++) {
int sensor = analogRead(analogPin);
dataString += String(sensor);
if (analogPin < 2) {
dataString += ",";
}
}

// open the file. note that only one file can be open at a time,
// so you have to close this one before opening another.
File dataFile = SD.open("datalog.txt", FILE_WRITE);

// if the file is available, write to it:
if (dataFile) {
dataFile.println(dataString);
dataFile.close();
// print to the serial port too:
Serial.println(dataString);
}
// if the file isn't open, pop up an error:
else {
Serial.println("error opening datalog.txt");
}
}

You really need to go back and edit that post and put the code in code tags. Have you tested the two sketches individually?

Well aarg asks a valid question and while I just have my doubts about the first one, you are clearly doomed as soon as you try to work them together.

You have

// Data wire is plugged into pin 2 on the Arduino
#define ONE_WIRE_BUS 2

for one and

const int chipSelect = 2;

In the other - a conflict of pins. Putting the DS18B20s on 2 is fine but I suggest you standardise on using pin 4 for the select for SD, because that seems to be what everybody else does.

If the temp code actually works, fine, but it looks like junk. There seems to be no definition of the returned values, which are simply floats. If it is junk, you will get better tutoring here:

The second programme is definitely junk, and perhaps not even complete, or at best a useless choice of example, as it tries to record information you are not producing, and certainly don't need to. In short, analogue recording of digital data is a bad idea, and digital data is what you get from a DS18B20.

Combining two programmes involves little more than getting the preliminary stuff from one and copying it into the other. Same goes for the setup, and the loop. The rest is getting rid of redundant stuff, ensuring no conflict (!) and the order of the events in the loop. The last is the hard bit but only inasmuch that there is no order in the first bits.

I suggest the first thing to do is get the temperature stuff going, assuming you haven't already.

For the SD, there is the aforementioned clash, and apparently a missing line

pinMode(10, OUTPUT);

in the setup. Otherwise I think the first two sections are OK, and may simply be copied into the temp programme. In the loop you should be able to print to file in much the same way that you print to serial.

Serial.print(InTemp);
Serial.print("           ");
Serial.println(OutTemp);

           myFile = SD.open(filename, FILE_WRITE);//<<<<<<<<<<<<<<<<< OPEN
  myFile.print(InTemp);
  myFile.print(",");
  myFile.println(OutTemp);
       myFile.close();//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>CLOSE

It doesn't need to be any more fancy than that. A rather long-winded example is here

and has parts you are already familiar with.

Please check the stickies and learn how to insert code.

This Simple Merge Demo may give you some ideas.

...R

Nick_Pyner:
For the SD, there is the aforementioned clash, and apparently a missing line

pinMode(10, OUTPUT);

I'm not sure what the story is with this one. I've read it too, yet the SD examples shipped with the IDE, (V1.6.5), don't do it, and they work fine.

I'd never heard of it when I wrote my first programs using SD cards either, so didn't include that line, and they work without problems.

Is it perhaps only a problem with earlier versions of the SD library?

Edit: Just doing a bit of further reading, I think the clue is:-

Note that even if you don't use the hardware SS pin, it must be left as an output or the SD library won't work.

(From the Arduino SD reference here:- SD Library )
So it doesn't need to specifically be made an output in the user's code, it just needs to be left as an output, implying that the library already takes care of this.

OldSteve:
I'm not sure what the story is with this one. I've read it too, yet the SD examples shipped with the IDE, (V1.6.5), don't do it, and they work fine.

Is it perhaps only a problem with earlier versions of the SD library?

I'm not aware of that. Every SD example I have ever seen calls for pin10 as output, or 53 for Mega.
If it is OK when omitted with v1.6.5, then fine. If there is strife, the OP knows where to look. I use v1.5 something.

the Dallas-1 wire works // I have it running now... the SD is what I copied from the examples... that I don't know, thanks for all the replies, I will look over them carefully to learn how to record into the SD card.

The only problem with the SD example is the loop. It is dealing with analogue sensors and confusing things by recording the wrong sort of data.

Nick_Pyner:
I'm not aware of that. Every SD example I have ever seen calls for pin10 as output, or 53 for Mega.
If it is OK when omitted with v1.6.5, then fine. If there is strife, the OP knows where to look. I use v1.5 something.

Have you ever tried compiling/running a sketch without doing it?

Even the examples included with my IDE that were last modified 4 years ago in early 2012 don't include a line to make pin 10 an output. I don't know which version of the IDE was released in 2012, but it must have been unnecessary to add that line since at least then.

I've seen nothing whatsoever in any SD documentation that specifically says that pin 10 must be made an output in the user code, only that it must be left as an output - big difference.

The examples are all linked here, at the bottom of the page, and none have that line:- SD Library

OldSteve:
Have you ever tried compiling/running a sketch without doing it?

No, but I guess I will try eventually.

What I do see is that the file dump example included with the library was modified by Igoe on April 9 2012 and says

  // make sure that the default chip select pin is set to
  // output, even if you don't use it:
  pinMode(10, OUTPUT);

By "the" library, I mean "my" library, which I guess is just the plain vanilla SD library.

Nick_Pyner:
No, but I guess I will try eventually.

What I do see is that the file dump example included with the library was modified by Igoe on April 9 2012 and says

  // make sure that the default chip select pin is set to

// output, even if you don't use it:
 pinMode(10, OUTPUT);




By "the" library, I mean "my" library, which I guess is just the plain vanilla SD library.

Hmmm. Another case of bad version control, by the look. (My library is the 'plain' one that ships with the IDE too.)

From the "DumpFile.ino" example that came with my IDE/SD library:-

created  22 December 2010
 by Limor Fried
 modified 9 Apr 2012
 by Tom Igoe

and

void setup()
{
  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }


  Serial.print("Initializing SD card...");

  // see if the card is present and can be initialized:
  if (!SD.begin(chipSelect)) {
    Serial.println("Card failed, or not present");
    // don't do anything more:
    return;
  }
  Serial.println("card initialized.");

  // open the file. note that only one file can be open at a time,
  // so you have to close this one before opening another.
  File dataFile = SD.open("datalog.txt");

  // if the file is available, write to it:
  if (dataFile) {
    while (dataFile.available()) {
      Serial.write(dataFile.read());
    }
    dataFile.close();
  }
  // if the file isn't open, pop up an error:
  else {
    Serial.println("error opening datalog.txt");
  }
}

Same example, same modified date, different code. :slight_smile:

I'd say that probably the library itself has been updated over the years, and that if that line was needed in the past, it isn't now. It's a pity that they don't keep their files up to date with the "modified" date though.
I'd love to see some form of version control introduced.

Anyway, with the current library, I can definitely verify that everything works as expected without explicitly needing to make pin 10 an output. (I have a temperature logger running beside me right now that doesn't include that line, and it works fine.)

Edit: Sorry, I didn't include the whole of 'setup()' originally, but just fixed it.

Thanks. I guess that is clear, at least surely for V1.6.5, which I guess I should upgrade to anyway.

Nick_Pyner:
Thanks. I guess that is clear, at least surely for V1.6.5, which I guess I should upgrade to anyway.

1.6.5 works well, and is nice and stable, unlike the later two versions.
(After reading about all of the problems with 1.6.6 and 1.6.7, I won't be upgrading to a later version any time soon.)

I am still looking for help, I am not a seasoned C++ programmer, but I am watching videos and learning..

First I wanted to say that some of you are kind of rude and down right mean, not sure why you are even here in this Arduino community, what if I was a kid, say 9 asking for help, this would not be cool, I am kind of concerned a little bit that this is even happening... since many projects little kids are involved in.

Here is my best attempt at combining the code with the Dallas-1 (which works fine with 2 sensors) and the SD card which kind of does not work, I added some code to tell me about the SD card and it does seem to work, I was able to create some basic file on the SD card with other SD example code, but now I am trying to actually write the data to it..

// include the SD library:
#include <SPI.h>
#include <SD.h>

File dataFile;

// reference the Dallas-1 wire
#include <OneWire.h>
#include <DallasTemperature.h>

// set up variables using the SD utility library functions:

Sd2Card card;
SdVolume volume;
SdFile root;

// change this to match your SD shield or module;
// Arduino Ethernet shield: pin 4
// Adafruit SD shields and modules: pin 10
// Sparkfun SD shield: pin 8

const int chipSelect = 4;

// Data wire is plugged into pin 2 on the Arduino

#define ONE_WIRE_BUS 2

// Setup a oneWire instance to communicate with any OneWire devices
// (not just Maxim/Dallas temperature ICs)
OneWire oneWire(ONE_WIRE_BUS);

// Pass our oneWire reference to Dallas Temperature.

DallasTemperature sensors(&oneWire);
void setup() {

  // Open serial communications and wait for port to open:
  
Serial.begin(9600);

  // Start up the library
  sensors.begin();
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }

  Serial.print("\nInitializing SD card...");

  // we'll use the initialization code from the utility libraries
  // since we're just testing if the card is working!
  if (!card.init(SPI_HALF_SPEED, chipSelect)) {
    Serial.println("initialization failed. Things to check:");
    Serial.println("* is a card inserted?");
    Serial.println("* is your wiring correct?");
    Serial.println("* did you change the chipSelect pin to match your shield or module?");
    return;
  } else {
    Serial.println("Wiring is correct and a card is present.");
  }

  // Now we will try to open the 'volume'/'partition' - it should be FAT16 or FAT32

  if (!volume.init(card)) {
    Serial.println("Could not find FAT16/FAT32 partition.\nMake sure you've formatted the card");
    return;
  }

  Serial.println("\nFiles found on the card (name, date and size in bytes): ");
  root.openRoot(volume);

  // list all files in the card with date and size
  root.ls(LS_R | LS_DATE | LS_SIZE);

}

void loop(void) {

  delay(5000);
  sensors.requestTemperatures(); 
  Serial.print("External Temperature is: ");
  Serial.print(sensors.getTempCByIndex(0) * 1.8 + 32.0); // Convert to F
  Serial.print('\n');
  Serial.print("Internal Temperature is: ");
  Serial.print(sensors.getTempCByIndex(1) * 1.8 + 32.0);
  Serial.print('\n'); 

  int Intemp = Serial.print(sensors.getTempCByIndex(0) * 1.8 + 32.0)
  File dataFile = SD.open("test1.txt", FILE_WRITE);

  // if the file is available, write to it:
  if (dataFile) {
    dataFile.println(Intemp);
    dataFile.close();
    // print to the serial port too:
    Serial.println(Intemp);
  }

  // if the file isn't open, pop up an error:
  else {
    Serial.println("error opening file");

  }
}

here is the output from that get from the serial monitor...
Initializing SD card...Wiring is correct and a card is present.

Files found on the card (name, date and size in bytes):
TEST.TXT 2000-01-01 01:00:00 90
External Temperature is: 72.50
Internal Temperature is: 68.90
72.50error opening file
External Temperature is: 72.50
Internal Temperature is: 68.90
72.50error opening file
External Temperature is: 72.50
Internal Temperature is: 68.90
72.50error opening file

Have you got a program that works with the SD Card and without the temperature stuff?

If so please post it.
And please use the code button </> so your code looks like this and is easy to copy to a text editor

...R

yes, I gotten example SD program to work, anyways, I played around with it and it worked this time...

I am recording temperatures separated by a comma, I confirmed this by playing another script to read the contents of the data, also added code to delete the contents of the file, as I was adjusting the formatting.

What is the easiest way to get the date/time from the eithernet cable (the shield) so that I can add that into the SD log part too ? this way I can graph it, I have all of this work done in Python, but I am trying to learn here in C..

I guess I will read some examples for Ethernet next...

#include <OneWire.h>
#include <DallasTemperature.h>

#include <SPI.h>
#include <SD.h>
File myFile;

const int chipSelect = 4;
 
// Data wire is plugged into pin 2 on the Arduino
#define ONE_WIRE_BUS 2
 
// Setup a oneWire instance to communicate with any OneWire devices 
// (not just Maxim/Dallas temperature ICs)
OneWire oneWire(ONE_WIRE_BUS);
 
// Pass our oneWire reference to Dallas Temperature.
DallasTemperature sensors(&oneWire);

 
void setup(void)
{
  // start serial port
  Serial.begin(9600);

  // Start up the library
  sensors.begin();

  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }

  Serial.print("Initializing SD card...");

  // see if the card is present and can be initialized:
  if (!SD.begin(chipSelect)) {
    Serial.println("Card failed, or not present");
    // don't do anything more:
    return;
  }
 
  Serial.println("card initialized.");

  Serial.println("Removing test5.txt...");
  SD.remove("test5.txt");

  if (SD.exists("test5.txt")) {
    Serial.println("test5.txt exists.");
  } else {
    Serial.println("test5.txt doesn't exist.");
  }

}
 
void loop(void)
{
  delay(5000);
 
  sensors.requestTemperatures(); 
  Serial.print("External Temperature is: ");
  Serial.print(sensors.getTempCByIndex(0) * 1.8 + 32.0); // Convert to F
  Serial.print('\n');
  Serial.print("Internal Temperature is: ");
  Serial.print(sensors.getTempCByIndex(1) * 1.8 + 32.0);
  Serial.print('\n'); 

  // make a string for assembling the data to log:
  String external_temp = "";
  String internal_temp = "";

  // read the two sensors and append to the string:
    external_temp = String(sensors.getTempCByIndex(0) * 1.8 + 32.0);
    internal_temp = String(sensors.getTempCByIndex(1) * 1.8 + 32.0);

File dataFile = SD.open("test5.txt", FILE_WRITE);

  // if the file is available, write to it:
  if (dataFile) {
    dataFile.println(external_temp + "," + internal_temp);
    dataFile.close();
  }
  // if the file isn't open, pop up an error:
  else {
    Serial.println("error opening datalog.txt");
  }

 
}

kodie425:
First I wanted to say that some of you are kind of rude and down right mean, not sure why you are even here in this Arduino community, what if I was a kid, say 9 asking for help, this would not be cool, I am kind of concerned a little bit that this is even happening... since many projects little kids are involved in.

This surprised me. I had to go back and look through the thread to see who had been rude. The answer - no one. You must be very thin-skinned.

aarg suggested this:-

You really need to go back and edit that post and put the code in code tags

And you totally ignored it. That's rude.

Then Nick Pyner gave you some good advice, as well as commenting that the code examples you'd found were not very good. What's wrong with that? (I'm assuming you thought that this was rude.)

I see nothing else in this thread that could be construed as rude, or improper for kids. (Now, of course, you'll think I'm rude for voicing my opinion. :slight_smile: )

If you really want help, don't be so critical of those that are trying to help you.