bobbycar/controller_teensy/include/temperature.h

106 lines
3.2 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>
2024-07-13 18:58:27 +00:00
//#define SEARCH_DEVICES
2024-06-26 19:42:16 +00:00
2024-07-14 08:54:49 +00:00
DeviceAddress thermometerFront={0x28,0xFF,0x64,0x0E,0x77,0xB0,0xAB,0x4B}; //IC with one marking 28FF640E77B0AB4B
float temp_Front;
DeviceAddress thermometerRear={0x28,0xFF,0x64,0x0E,0x76,0x5D,0x86,0xC2}; //IC with two markings
float temp_Rear;
2023-06-13 21:45:17 +00:00
DeviceAddress thermometerAir={0x28,0xFF,0x64,0x0E,0x74,0x7E,0xFE,0x23}; //IC with three markings 28FF640E747EFE23
float temp_Air;
#define ONE_WIRE_BUS 23 //GPIO pin
2023-06-13 21:45:17 +00:00
#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();
2024-06-26 19:42:16 +00:00
bool temperatureLoop();
2023-06-13 21:45:17 +00:00
void printAddress(DeviceAddress deviceAddress);
void initTemperature() {
sensors.begin();
2024-07-13 18:58:27 +00:00
//Just search for devices. Only needed when connecting a new sensor to find the address
#ifdef SEARCH_DEVICES
2023-06-13 21:45:17 +00:00
Serial.print("Locating 1Wire devices...");
2023-06-13 21:45:17 +00:00
Serial.print("Found ");
Serial.print(sensors.getDeviceCount(), DEC);
Serial.println(" devices.");
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);
2024-07-13 18:58:27 +00:00
#endif
2023-06-13 21:45:17 +00:00
2024-07-13 18:58:27 +00:00
//sensors.setResolution(thermometerESCFront, TEMPERATURE_PRECISION);
2024-06-26 19:42:16 +00:00
2023-06-13 21:45:17 +00:00
}
2024-06-26 19:42:16 +00:00
bool temperatureLoop(unsigned long loopmillis) {
2023-06-13 21:45:17 +00:00
static unsigned long last_read_ds18b20;
static bool flag_requestTemperatures=false;
if (loopmillis>last_read_ds18b20+READINTERVAL_DS18B20) {
2024-06-26 19:42:16 +00:00
if (loopmillis>last_read_ds18b20+READINTERVAL_DS18B20*4) { //timeout
2023-06-13 21:45:17 +00:00
Serial.println("Warn: Request Temperatures Timeout!");
flag_requestTemperatures=false;
2024-06-26 19:42:16 +00:00
last_read_ds18b20=loopmillis; //set time to lastread to wait for another timeout
return 0;
2023-06-13 21:45:17 +00:00
}
if (!flag_requestTemperatures) {
2024-06-26 19:42:16 +00:00
//in synchronous mode requestTemperatures takes ~34ms if sensors are available. if sensors not available it takes about 729ms
sensors.setWaitForConversion(false); //make async
//in asynchronous mode it takes ~2ms if sensors are available. if not it takes ~1ms
sensors.requestTemperatures();
sensors.setWaitForConversion(true);
2023-06-13 21:45:17 +00:00
flag_requestTemperatures=true;
}
if (sensors.isConversionComplete()) {
flag_requestTemperatures=false;
last_read_ds18b20=loopmillis;
2024-07-14 08:54:49 +00:00
temp_Front= sensors.getTempC(thermometerFront); //This takes ~12.5ms
temp_Rear= sensors.getTempC(thermometerRear);
2023-06-13 21:45:17 +00:00
temp_Air= sensors.getTempC(thermometerAir);
}
}
2024-06-26 19:42:16 +00:00
return 1;
2023-06-13 21:45:17 +00:00
}
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