I've got a Vernier Interface Shield hooked up to an Arduino Mega. The Interface Shield lets me use Vernier science probes with the Arduino.
One of the sensors is a DS18B20 temperature probe. The sensor returns a correct value only when I comment out all of the other working code for the other sensors, but returns an erroneous value when the code isn't commented out.
Please see the comments in the code for the description of the problem. Not quite sure why the temp probe would be reporting good values only when the rest of the code in loop() is commented out.
#include <OneWire.h>
#include <DallasTemperature.h>
/********************************************************************/
// Data wire for the DS18B20 temp sensor is plugged into pin 37 on the Arduino
#define ONE_WIRE_BUS 37
/********************************************************************/
// 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);
/********************************************************************/
char outBuffer[32];
int temp1 = 37;
int floatSwitch1 = 31;
int floatSwitch2 = 33;
int floatSwitch3 = 35;
int analog0 = A0;
int analog1 = A1;
int analog2 = A2;
int analog3 = A3;
int analog4 = A4;
int analog5 = A5;
void setup() {
Serial.begin(115200);
pinMode(temp1, INPUT_PULLUP);
pinMode(floatSwitch1, INPUT_PULLUP);
pinMode(floatSwitch2, INPUT_PULLUP);
pinMode(floatSwitch3, INPUT_PULLUP);
pinMode(analog0, INPUT);
pinMode(analog1, INPUT);
pinMode(analog2, INPUT);
pinMode(analog3, INPUT);
pinMode(analog4, INPUT);
pinMode(analog5, INPUT);
sensors.begin();
}
void loop() {
// read the value from the temperature sensor:
sensors.requestTemperatures();
Serial.println(serialOutput("T1", sensors.getTempFByIndex(0)));
delay(2000);
// if I comment out all remaining code below in loop(), the temp sensor above returns the correct value ie. <T1:73.06>
// if I leave the code as it is, the other sensors return correct values, but the temp sensor does not anymore ie. <T1:-196.60>.
// even if I unplug the wire from pin 37, the reading still returns as <T1:-196.60>
Serial.println(serialOutput("A0", analogRead(analog0)));
delay(2000);
Serial.println(serialOutput("A1", analogRead(analog1)));
delay(2000);
Serial.println(serialOutput("A2", analogRead(analog2)));
delay(2000);
Serial.println(serialOutput("A3", analogRead(analog3)));
delay(2000);
Serial.println(serialOutput("A4", analogRead(analog4)));
delay(2000);
Serial.println(serialOutput("A5", analogRead(analog5)));
delay(2000);
Serial.println(serialOutput("FS1", digitalRead(floatSwitch1)));
delay(2000);
Serial.println(serialOutput("FS2", digitalRead(floatSwitch2)));
delay(2000);
Serial.println(serialOutput("FS3", digitalRead(floatSwitch3)));
delay(2000);
}
char *serialOutput(const char *sensor, const float reading) {
char result[5];
dtostrf(reading, 4, 2, result);
sprintf(outBuffer, "<%s:%s>", sensor, result);
return outBuffer;
}