I am working on a project in which I print the status of four moisture sensors and four DS18B20 temperature sensors to the serial monitor (to send to Max/MSP). For this, I wanted a list of 8 values printed.
It was simple enough to edit a DallasTemperature example patch to get this to work:
#include <OneWire.h>
#include <DallasTemperature.h>
OneWire ds18x20[] = {3, 4, 5, 6 };
const int oneWireCount = sizeof(ds18x20)/sizeof(OneWire);
DallasTemperature sensor[oneWireCount];
int fsrPin = 0;
int scnpin = 1;
int thrpin = 2;
int adc_id4 = 3;
void setup(void) {
// start serial port ORANGE GREEN YELLOW BLUE
Serial.begin(9600);
Serial.println("Dallas Temperature Multiple Bus Control Library Simple Demo");
Serial.print("============Ready with ");
Serial.print(oneWireCount);
Serial.println(" Sensors================");
// Start up the library on all defined bus-wires
DeviceAddress deviceAddress;
for (int i = 0; i < oneWireCount; i++) {;
sensor[i].setOneWire(&ds18x20[i]);
sensor[i].begin();
if (sensor[i].getAddress(deviceAddress, 0)) sensor[i].setResolution(deviceAddress, 12);
}
}
void loop(void) {
// call sensors.requestTemperatures() to issue a global temperature
// request to all devices on the bus
for (int i = 0; i < oneWireCount; i++) {
sensor[i].requestTemperatures();
}
Serial.print( analogRead( fsrPin ) );
Serial.print(" ");
Serial.print( analogRead( scnpin ) );
Serial.print(" ");
Serial.print( analogRead( thrpin ) );
Serial.print(" ");
Serial.print( analogRead( adc_id4 ) );
Serial.print(" ");
for (int i = 0; i < oneWireCount; i++) {
float temperature = sensor[i].getTempCByIndex(0);
Serial.print(temperature);
Serial.print(" ");
}
Serial.println();
delay(100);
}
The one major issue is the ~3000ms delay from using four temperature sensors. Is there a way in which each of the moisture sensors can keep updating every 100ms while the temperature value is calculated? Or would it be possible to speed up the conversion time for the temperature sensors - accuracy isn't paramount so if there was a way change from 12-bit operation to something lower, that might work.
Ideally, the calls would be in the loop as I would like the temperature to be continually updated (at a rate of once every 3000ms). Is that what you mean?
The speed of change for the temperature isn't the issue, it is the lack of constant updates from the moisture sensors - the values are being used to control audio parameters which need updating more than once every three seconds.
One very rough solution I tried was copying this part of the loop 29 times before having the full request:
You can use the DallasTemperature in a non-blocking mode that will allow your code to do useful work while it's waiting for the temperature sensors to do their conversion. See the library's source code in DallasTemperature.h / DallasTemperature.cpp. Also, look at the WaitForConversion and WaitForConversion2 examples.
Other things in your code that could be improved:
You only need to call requestTemperatures() once and it will start the conversion process on all sensors.
The getTempCByIndex() is slow and you're using it incorrectly. It's better to get and save the OneWire address for all the connected sensors once in your setup() function. Then call getTempC() for each sensor and pass it the address.
Use a millis() timer to control how often you want to update the temperatures. Every 10 even 30 seconds would be fine. If your application needs fast temperature readings, then you're using the wrong sensor.
Referring to reply #6, perhaps this in your setup would make measurements non-blocking where you'll need to check if the required 750ms time period for each sensor has elapsed ...
for (int i = 0; i < oneWireCount; i++) {
sensor[i].setOneWire(&ds18x20[i]);
sensor[i].begin();
sensor[i].setWaitForConversion(false);
if (sensor[i].getAddress(deviceAddress, 0)) sensor[i].setResolution(deviceAddress, 12);
}