#include <OneWire.h>
#define PIN 12 // DS18B20 PIN
#define SENSORS_AMOUNT 3
OneWire DS18B20(PIN);
byte addr[8]; // Arrary 64-bit addrress
byte array_sensors[SENSORS_AMOUNT][sizeof(addr)]; // Array of sensors
byte data[12]; // Array data from sensor
byte present = 0;
void setup() {
Serial.begin(9600);
search_sensors(DS18B20, array_sensors);
/*
for(uint8_t i = 0; i < SENSORS_AMOUNT; i++){
for(uint8_t j = 0; j < 8; j++){
Serial.print(array_sensors[i][j], HEX);
}
Serial.println();
}
*/
}
// Search sensors and add to array
void search_sensors(OneWire sensor_pin, byte arr_sensors[][sizeof(byte) * 8]){
uint8_t count = 0;
byte addr[8];
//Serial.println("Sensors addresses:");
while(sensor_pin.search(addr)){
for(uint8_t i = 0; i < 8; i++){
arr_sensors[count][i] = addr[i];
}
count++;
}
DS18B20.reset_search();
delay(250);
return;
}
void request_temp(OneWire sensor_pin, byte sensor[]){
sensor_pin.reset();
sensor_pin.select(sensor);
sensor_pin.write(0x44, 1);
}
void read_temp(OneWire sensor_pin, byte sensor[], byte data[]){
sensor_pin.reset();
sensor_pin.select(sensor);
sensor_pin.write(0xBE);
for(uint8_t i = 0; i < 9; i++){
data[i] = sensor_pin.read();
// Serial.print(data[i], HEX);
}
//Serial.println();
int16_t raw = (data[1] << 8) | data[0];
raw = raw << 3;
if(data[7] == 0x10){
raw = (raw & 0XFFF0) + 12 - data[6];
}
Serial.print(raw / 16.0);
Serial.print(" ");
}
void loop() {
// request data from all sensors
for (uint8_t j = 0; j < SENSORS_AMOUNT; j++){
request_temp(DS18B20, array_sensors[j]);
}
// delay from request to get temp about 750 ms
delay(1000);
// read temperature from all sensors
for(int8_t i = SENSORS_AMOUNT - 1; i >= 0 ; i--){
read_temp(DS18B20, array_sensors[i], data);
}
Serial.println();
delay(10);
}