ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

电热毯配个温控

2022-01-01 15:04:39  阅读:163  来源: 互联网

标签:tempC sensors else int print 电热毯 配个 Serial 温控


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

// Data wire is plugged into port 2 on the Arduino
#define ONE_WIRE_BUS 11
#define jdq 12
#define TEMPERATURE_PRECISION 12

// 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

void setup(void)
{
// start serial port
pinMode(12, OUTPUT);
delay(200);
Serial.begin(9600);
Serial.println("Dallas Temperature IC Control Library Demo");
digitalWrite(12,LOW);
delay(100);
// Start up the library
sensors.begin();

// Grab a count of devices on the wire
numberOfDevices = sensors.getDeviceCount();

// locate devices on the bus
Serial.print("Locating devices...");

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");
}
}

}
int HH0 = 0;
int HH1 = 0;
int H1 = 0;
int H0 = 0;
int LL1 = 0;
int LL0 = 0;
int L1 = 0;
int L0 = 0;
// function to print the temperature for a device
void printTemperature(DeviceAddress deviceAddress,int i)
{
// 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

if (tempC > 25 && i == 0) {HH0 = 1;H0 = 0;LL0 = 0;L0 = 0; }
else if (tempC > 22 && i == 0) {HH0 = 0;H0 = 1;LL0 = 0;L0 = 0; }
else if (tempC > 20 && i == 0) {HH0 = 0;H0 = 0;LL0 = 0;L0 = 0; }
else if (tempC > 18 && i == 0) {HH0 = 0;H0 = 0;LL0 = 0;L0 = 1; }
else if ( i == 0) {HH0 = 0;H0 = 0;LL0 = 1;L0 = 0; }

if (tempC > 25 && i == 1) {HH1 = 1;H1 = 0;LL1 = 0;L1 = 0; }
else if (tempC > 22 && i == 1) {HH1 = 0;H1 = 1;LL1 = 0;L1 = 0; }
else if (tempC > 20 && i == 1) {HH1 = 0;H1 = 0;LL1 = 0;L1 = 0; }
else if (tempC > 18 && i == 1) {HH1 = 0;H1 = 0;LL1 = 0;L1 = 1; }
else if ( i == 1) {HH1 = 0;H1 = 0;LL1 = 1;L1 = 0; }

}

void loop(void)
{
if (HH1 == 1 || HH0 == 1 ) {digitalWrite(jdq,LOW); delay(200);}
else if (LL1 == 1 || LL0 == 1 ) {digitalWrite(jdq,HIGH); delay(200);}
else if (L1 == 1 && L0 == 1 ) {digitalWrite(jdq,HIGH); delay(200);}
else if (H1 == 1 && H0 == 1 ) {digitalWrite(jdq,LOW); delay(200);}

// 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");


// 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,i); // Use a simple function to print out the data
}
//else ghost device! Check your power requirements and cabling

}
delay(10000);

}

// 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);
}
}

 

///////////    2个  ds18b20 接在 11arduino D11    ,   继电器 接在  D1

 

 

标签:tempC,sensors,else,int,print,电热毯,配个,Serial,温控
来源: https://www.cnblogs.com/chunk998/p/15755393.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有