bobbycar/controller_teensy/include/temperature.h

103 lines
3.0 KiB
C
Raw Normal View History

2023-06-13 21:45:17 +00:00
#ifndef _TEMPERATURE_H_
#define _TEMPERATURE_H_
#include <OneWire.h>
#include <DallasTemperature.h>
DeviceAddress thermometerESCFront={0x28,0xFF,0x64,0x0E,0x77,0xB0,0xAB,0x4B}; //IC with one marking 28FF640E77B0AB4B
float temp_ESCFront;
DeviceAddress thermometerESCRear={0x28,0xFF,0x64,0x0E,0x76,0x5D,0x86,0xC2}; //IC with two markings
float temp_ESCRear;
DeviceAddress thermometerAir={0x28,0xFF,0x64,0x0E,0x74,0x7E,0xFE,0x23}; //IC with three markings 28FF640E747EFE23
float temp_Air;
#define ONE_WIRE_BUS A2 //GPIO pin
#define TEMPERATURE_PRECISION 12 //max is 12
#define READINTERVAL_DS18B20 5000 //ms
// 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);
void initTemperature();
void temperatureLoop();
void printAddress(DeviceAddress deviceAddress);
void initTemperature() {
sensors.begin();
delay(1000);
Serial.print("Locating devices...");
Serial.print("Found ");
Serial.print(sensors.getDeviceCount(), DEC);
Serial.println(" devices.");
delay(1000);
delay(1000);
//Just search for devices. Only needed when connecting a new sensor to find the address
oneWire.reset_search();
for (uint8_t i=0;i<sensors.getDeviceCount();i++){
DeviceAddress _addr;
if (!oneWire.search(_addr)) {
Serial.print("Error: Device not found");
}else{
Serial.print("Found device. Address:");
printAddress(_addr);
}
Serial.println();
}
delay(1000);
//sensors.setResolution(thermometerReservoir, TEMPERATURE_PRECISION);
}
void temperatureLoop(unsigned long loopmillis) {
static unsigned long last_read_ds18b20;
static bool flag_requestTemperatures=false;
if (loopmillis>last_read_ds18b20+READINTERVAL_DS18B20) {
if (loopmillis>last_read_ds18b20+READINTERVAL_DS18B20*10) { //timeout
Serial.println("Warn: Request Temperatures Timeout!");
flag_requestTemperatures=false;
}
if (!flag_requestTemperatures) {
sensors.requestTemperatures(); //this takes ~34ms
flag_requestTemperatures=true;
}
if (sensors.isConversionComplete()) {
flag_requestTemperatures=false;
last_read_ds18b20=loopmillis;
temp_ESCFront= sensors.getTempC(thermometerESCFront); //This takes ~12.5ms
temp_ESCRear= sensors.getTempC(thermometerESCRear);
temp_Air= sensors.getTempC(thermometerAir);
/*
Serial.print("temp_ESCFront: "); Serial.println(temp_ESCFront);
Serial.print("temp_ESCRear: "); Serial.println(temp_ESCRear);
Serial.print("temp_Air: "); Serial.println(temp_Air);
*/
}
}
}
void printAddress(DeviceAddress deviceAddress)
{
for (uint8_t i = 0; i < 8; i++)
{
// zero pad the address if necessary
if (deviceAddress[i] < 16) Serial.print("0");
Serial.print(deviceAddress[i], HEX);
}
}
#endif