Hi, I'm setting up an Arduino temperature logger using a Mega, the Ethernet/SD shield, an RTC, a 20x4 LCD screen, and some DS18B20 onewire temperature sensors. I'm aiming for 12 sensors, but I'm starting with 4 for testing.
I want to keep each DS18B20 to its own bus - the main benefit being that I can have a specific socket for each sensor so I don't have to start working out which address relates to which sensor, and there doesn't seem to be a downside to this approach in hardware, but the software is more complicated.
This code works if I replace [NUM_BUSES] with "2" in the for loop, but if I change it to 3 or more then the code starts to fail, and by that I mean it stops logging to the SD card and stops reading from the SD card too. I presume it's something to do with the long delays in the Dallas library?
So my questions are:
Am I doing something fundamentally wrong?
Is there a better way to refactor this code?
Is there a way to loop through the temperature requests without causing issues. I could drop the sensor readings down to 11-bit to speed that up, but I'm not sure how to do that given the array setup.
Snippet from Global:
// Libraries for DS18B20
#include <OneWire.h>
#include <DallasTemperature.h>
// Define the number of OneWire buses and the data pin for each bus.
#define NUM_BUSES 4
int busPins[NUM_BUSES] = { 2, 3, 4, 5 }; // Define bus pins
// Create an array of OneWire objects for each bus.
OneWire oneWire[NUM_BUSES] = { OneWire(busPins[0]), OneWire(busPins[1]), OneWire(busPins[2]), OneWire(busPins[3]) };
// Create an array of DallasTemperature objects for each bus.
DallasTemperature sensors[NUM_BUSES] = { DallasTemperature(&oneWire[0]), DallasTemperature(&oneWire[1]), DallasTemperature(&oneWire[2]), DallasTemperature(&oneWire[3]) };
// Create an array to store the CRC16 values for each sensor.
unsigned int crcValues[NUM_BUSES];
unsigned int prevCRC = 0;
Snippet from Loop:
for (int i = 0; i < [NUM_BUSES]; i++) {
sensors[i].requestTemperatures();
tempC = sensors[i].getTempCByIndex(0);
Serial.print("Sensor ");
Serial.print(i);
Serial.print(": ");
Serial.print(tempC);
Serial.println("°C");
// Add data to dataLine string
dataLine += tempC;
dataLine += ",";
}
myFile.println(dataLine);
Serial.println(dataLine);
I can post all of the code if that's helpful but it's over 1k lines.
thanks,
Danny