|
|

楼主 |
发表于 2014-9-12 11:05:53
|
显示全部楼层
读数有冲突。但是有冲突解决办法的。Dalla那个函数库里有。请见我的源代码。
#include <OneWire.h>
#include <DallasTemperature.h>
#include <SPI.h>
#include <LCD5110_CN.h>
LCD5110 myGLCD(3,4,5,11,6);
extern uint8_t SmallFont[];
// Data wire is plugged into port 2 on the Arduino
#define ONE_WIRE_BUS 2
#define TEMPERATURE_PRECISION 12
#define RELAY_pump 7
#define RELAY_spray1 8
#define RELAY_spray2 9
#define RELAY_spray3 10
#define OPEN_TEMPERATURE 45
#define CLOSE_TEMPERATURE 40
/** Pins:
* Hardware SPI:
* MISO -> 12
* MOSI -> 11
* SCK -> 13
*
* Configurable:
* CE -> 8
* CSN -> 7 */
// 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);
int numberOfDevices; // Number of temperature devices found
DeviceAddress tempDeviceAddress; // We'll use this variable to store a found device address
// function to print a device address
void printAddress(DeviceAddress deviceAddress)
{
for (uint8_t i = 0; i < 8; i++)
{
if (deviceAddress[i] < 16) Serial.print("0");
Serial.print(deviceAddress[i], HEX);
}
}
// function to print the temperature for a device
void printTemperature(DeviceAddress deviceAddress)
{
// method 1 - slower
//Serial.print("Temp C: ");
//Serial.print(sensors.getTempC(deviceAddress));
//Serial.print(" Temp F: ");
//Serial.print(sensors.getTempF(deviceAddress)); // Makes a second call to getTempC and then converts to Fahrenheit
// method 2 - faster
float tempC = sensors.getTempC(deviceAddress);
Serial.print("Temp C: ");
Serial.print(tempC);
//Serial.print(" Temp F: ");
//Serial.println(DallasTemperature::toFahrenheit(tempC)); // Converts tempC to Fahrenheit
}
float tempC;
char tempstr[5];
char ifspray=0;
float averagetemp;
int startnum=0;
void setup(void)
{
// start serial port
Serial.begin(9600);
/* To change CE / CSN Pins:
*
* Mirf.csnPin = 9;
* Mirf.cePin = 7;
*/
myGLCD. InitLCD();
myGLCD.setFont(SmallFont);
pinMode(RELAY_pump, OUTPUT);
pinMode(RELAY_spray1, OUTPUT);
pinMode(RELAY_spray2, OUTPUT);
pinMode(RELAY_spray3, OUTPUT);
//Serial.println("Dallas Temperature IC Control Library Demo");
//Mirf.spi = &MirfHardwareSpi;
//Mirf.init();
// Start up the library
//Mirf.setRADDR((byte *)"Tempstation");
// Grab a count of devices on the wire
//Mirf.payload = sizeof(unsigned long);
// locate devices on the bus
Serial.print("Locating devices...");
/*
* To change channel:
*
* Mirf.channel = 10;
*
* NB: Make sure channel is legal in your area.
*/
//Mirf.config();
sensors.begin();
numberOfDevices = sensors.getDeviceCount();
Serial.print("Found ");
Serial.print(numberOfDevices, DEC);
Serial.println(" devices.");
// report parasite power requirements
Serial.print("Parasite power is: ");
if (sensors.isParasitePowerMode()) Serial.println("ON");
else Serial.println("OFF");
// Loop through each device, print out address
for(int i=0;i<numberOfDevices; i++)
{
// Search the wire for address
if(sensors.getAddress(tempDeviceAddress, i))
{
Serial.print("Found device ");
Serial.print(i, DEC);
Serial.print(" with address: ");
printAddress(tempDeviceAddress);
Serial.println();
//Serial.print("Setting resolution to ");
//Serial.println(TEMPERATURE_PRECISION,DEC);
// set the resolution to 9 bit (Each Dallas/Maxim device is capable of several different resolutions)
sensors.setResolution(tempDeviceAddress, TEMPERATURE_PRECISION);
Serial.print("Resolution actually set to: ");
Serial.print(sensors.getResolution(tempDeviceAddress), DEC);
Serial.println();
}else{
Serial.print("Found ghost device at ");
Serial.print(i, DEC);
Serial.print(" but could not detect address. Check power and cabling");
}
}
}
void loop(void)
{
ifspray=0;
averagetemp=0;
//unsigned long time = millis();
// call sensors.requestTemperatures() to issue a global temperature
// request to all devices on the bus
Serial.print("Requesting temperatures...");
sensors.requestTemperatures(); // Send the command to get temperatures
Serial.println("DONE");
myGLCD.print("sensor1:",LEFT,0);
myGLCD.print("sensor2:",LEFT,8);
myGLCD.print("sensor3:",LEFT,16);
myGLCD.print("sensor4:",LEFT,24);
myGLCD.print("Start count:",LEFT,32);
myGLCD.print("Average:",LEFT,40);
// Loop through each device, print out temperature data
for(int i=0;i<numberOfDevices; i++)
{
// Search the wire for address
if(sensors.getAddress(tempDeviceAddress, i))
{
// Output the device ID
//Serial.print("Temperature for device: ");
//Serial.println(i,DEC);
// It responds almost immediately. Let's print out the data
printTemperature(tempDeviceAddress); // Use a simple function to print out the data
tempC = sensors.getTempC(tempDeviceAddress);
myGLCD.printNumF(tempC,2,RIGHT,i*8);
//Mirf.setTADDR((byte *)"Transfer");
//Mirf.send((byte *)"Temp C: \n");
Serial.print("Temp C: ");
//Mirf.send((byte *) dtostrf(tempC,5,2,tempstr));
Serial.println(tempC);
//while(Mirf.isSending()){
//}
//delay(10);
/*while(!Mirf.dataReady()){
//Serial.println("Waiting");
if ( ( millis() - time ) > 50000 ) {
Serial.println("Timeout on response from server!");
return;
}
}*/
//else ghost device! Check your power requirements and cabling
}
averagetemp=averagetemp+tempC;
}
Serial.println(numberOfDevices);
averagetemp=averagetemp/numberOfDevices;
myGLCD.printNumF(averagetemp,2,RIGHT,40);
myGLCD.printNumI(startnum,RIGHT,32);
Serial.println(averagetemp);
if (averagetemp>OPEN_TEMPERATURE)
{
ifspray=1;
}
else if (averagetemp<CLOSE_TEMPERATURE)
{
ifspray=0;
}
//Serial.println(":");
//Serial.println(":");
//Serial.println(":");
if (ifspray==1)
{
digitalWrite(RELAY_pump, HIGH);
digitalWrite(RELAY_spray1, HIGH);
delay(30000);
digitalWrite(RELAY_spray1, LOW);
digitalWrite(RELAY_spray2, HIGH);
delay(30000);
digitalWrite(RELAY_spray2, LOW);
digitalWrite(RELAY_spray3, HIGH);
delay(30000);
digitalWrite(RELAY_spray3, LOW);
startnum=startnum+1;
}
else
{
digitalWrite(RELAY_pump, LOW);
delay(5000);
}
}
|
|