Problems with a digital sensor and Maximum an Minimum

Good Morning,
I have a problem with the code.
What I want is for it to read me all the values and show me the maximum and minimum values in the Serial monitor.
But, I don't know why it doesn't renew or save the last value for me.
If you can help me, I would appreciate it
I´m using a Arduino UNO, and 3 digital sensor of DS18B20

//incluir librerias
#include <Statistic.h>
#include <OneWire.h>
#include <DallasTemperature.h>


const int oneWirePin = 5;
OneWire oneWireBus(oneWirePin);
DallasTemperature sensors(&oneWireBus);

#define Delay 2500
unsigned long lastMillis = 0;


//Dirección de los sensores en Hexadecimal
DeviceAddress sensor1 = { 0x28, 0x7D, 0x4E, 0x56, 0xB5, 0x01, 0x3C, 0x06 };
DeviceAddress sensor2 = { 0x28, 0xBB, 0x22, 0x79, 0x97, 0x08, 0x03, 0x67 };
DeviceAddress sensor3 = { 0x28, 0xE1, 0x82, 0x79, 0x97, 0x09, 0x03, 0x71 }; 

//Variables del programa y escritura y lectura de las variables
float SensTemp_1 [3] = {};
float SensTemp_2 [3] = {};
float SensTemp_3 [3] = {}; 
float Maximum, Minimum, average, mediaTemp, Add_data;
int N;

void setup(void)
{
  //declaramos variables fijas solamente lo ejecutamos una vez
  Serial.begin(9600);
  sensors.begin();
  sensors.setResolution(sensor1, 10);//sensor de termometro 1
  sensors.setResolution(sensor2, 10);//sensor de termometro 2
  sensors.setResolution(sensor3, 10);//sensor de termometro 3

}
//creamos una funcion de punto a punto para los sensores en hexadecimal
void printsensor(DeviceAddress deviceAddress)
{

//creamos una serie de variables para esta función
  float temp;
  float tempC = sensors.getTempC(deviceAddress);//tempC es Temperatura en Grados centigrados, y get.TempC es una función donde recoje los datos
  if (tempC == -127.00) {
    Serial.print("No hay señal de temperatura, revisa el cableado eléctrico");
    //en caso si no hubiera nada conectado en la placa de prototipo o mal direccionado en el programa
  } else {
    Serial.print(tempC);
   Serial.println(" ºC");//muestra la temperatura en grados centigrados en monitor serie
  }  
} 
void loop(void)
{  
  
 unsigned long currentMillis = millis();
 if(currentMillis -lastMillis >= Delay){ 
  lastMillis = currentMillis;
  
  Serial.println("Leyendo sensores de temperaturas");
  sensors.requestTemperatures();//Esperando respuesta de los sensores
  Serial.print("Temperatura sensor 1: ");
  printsensor(sensor1);   
  Serial.print("Temperatura Maxima: ");
  Serial.println(0);
  Serial.print("Temperatura minima: ");    
  Serial.println(0);
  Serial.print("Temperatura media: ");
  Serial.println(0);
  
  Serial.println("---------");

  Serial.print("Temperatura sensor 2: ");
  printsensor(sensor2);
  Serial.print("Temperatura Maxima: ");
  Serial.println(0);
  Serial.print("Temperatura minima: ");    
  Serial.println(0);
  Serial.print("Temperatura media: ");
  Serial.println(0);
  
  Serial.println("---------");
  
  Serial.print("Temperatura sensor 3: ");
  printsensor(sensor3);
  Serial.print("Temperatura Maxima: ");
  Serial.println(0);
  Serial.print("Temperatura minima: ");    
  Serial.println(0);
  Serial.print("Temperatura media: ");
  Serial.println(0);
  
  Serial.println("-------"); 
  
  }
}


What is this line of code supposed to do ? Does it even compile ?

  Mib Dallas_1;
//Variables del programa y escritura y lectura de las variables
float Dallas_1 = sensors.getTempC(sensor1);
float Dallas_2 = sensors.getTempC(sensor2);
float Dallas_3 = sensors.getTempC(sensor3);

The above will not work. Global variables are assigned their initial values before setup() is executed. So at that time, sensors.begin() will not have been executed, and attempting to use sensors.getTempC() will not succeed.

EDIT: hmm.. I wonder if you imagined that each time your code reads the value of "Dallas_1" it will be reading the latest temperature from the sensor? Unfortunately that is not how things work in C code or in most other languages. Even if the above worked at all, the value in the variable Dallas_1 would never change because your code never updates it by reading the sensor and assigning the new value to the variable.

So...how can I fixed?
At this moment...I trying to make an arrays
This is the complete code:

//incluir librerias
#include <Statistic.h>
#include <OneWire.h>
#include <DallasTemperature.h>


const int oneWirePin = 5;
OneWire oneWireBus(oneWirePin);
DallasTemperature sensors(&oneWireBus);

#define Delay 5000
unsigned long lastMillis = 0;


//Dirección de los sensores en Hexadecimal
DeviceAddress sensor1 = { 0x28, 0x7D, 0x4E, 0x56, 0xB5, 0x01, 0x3C, 0x06 };
DeviceAddress sensor2 = { 0x28, 0xBB, 0x22, 0x79, 0x97, 0x08, 0x03, 0x67 };
DeviceAddress sensor3 = { 0x28, 0xE1, 0x82, 0x79, 0x97, 0x09, 0x03, 0x71 }; 

//Variables del programa y escritura y lectura de las variables
float SensTemp_1 [3] = {};
float SensTemp_2 [3] = {};
float SensTemp_3 [3] = {}; 
float Maximum, Minimum, average, mediaTemp, Add_data;
int N;

void setup(void)
{
  //declaramos variables fijas solamente lo ejecutamos una vez
  Serial.begin(9600);
  sensors.begin();
  sensors.setResolution(sensor1, 10);//sensor de termometro 1
  sensors.setResolution(sensor2, 10);//sensor de termometro 2
  sensors.setResolution(sensor3, 10);//sensor de termometro 3

}
//creamos una funcion de punto a punto para los sensores en hexadecimal
void printsensor(DeviceAddress deviceAddress)
{

//creamos una serie de variables para esta función
  float temp;
  float tempC = sensors.getTempC(deviceAddress);//tempC es Temperatura en Grados centigrados, y get.TempC es una función donde recoje los datos
  if (tempC == -127.00) {
    Serial.print("No hay señal de temperatura, revisa el cableado eléctrico");
    //en caso si no hubiera nada conectado en la placa de prototipo o mal direccionado en el programa
  } else {
    Serial.print(tempC);
   Serial.println(" ºC");//muestra la temperatura en grados centigrados en monitor serie
  }  
} 

  
void loop(void)
{  


  
 unsigned long currentMillis = millis();
 if(currentMillis -lastMillis >= Delay){ 
  lastMillis = currentMillis;
  
  Serial.println("Leyendo sensores de temperaturas");
  sensors.requestTemperatures();//Esperando respuesta de los sensores
  Serial.print("Temperatura sensor 1: ");
  printsensor(sensor1);    
  Serial.print("Temperatura Maxima: ");
  Serial.println(0);
  Serial.print("Temperatura minima: ");    
  Serial.println(0);
  Serial.print("Temperatura media: ");
  Serial.println(0);
  
  Serial.println("---------");

  Serial.print("Temperatura sensor 2: ");
  printsensor(sensor2);
  Serial.print("Temperatura Maxima: ");
  Serial.println(0);
  Serial.print("Temperatura minima: ");    
  Serial.println(0);
  Serial.print("Temperatura media: ");
  Serial.println(0);
  
  Serial.println("---------");
  
  Serial.print("Temperatura sensor 3: ");
  printsensor(sensor3);
  Serial.print("Temperatura Maxima: ");
  Serial.println(0);
  Serial.print("Temperatura minima: ");    
  Serial.println(0);
  Serial.print("Temperatura media: ");
  Serial.println(0);
  
  Serial.println("-------"); 
  
  }
}

What happened to your code tags? You used them correctly in your first post. Please fix that.

EDIT: @mrdemon deleted post #4

Hi,
How about to start from basics and get ONE sensor working and your Max Min code for that, before cluttering your code with the extra sensors?
That way you can use serial monitor to check you variables as you progress through your code.

Tom.... :grinning: :+1: :coffee: :australia:

Actually, I using one sensor to see what happens
:sweat_smile:

I change the code, but it still similar and more clear to see

Hi,
Can you please post your simplified code?

Thanks.. Tom... :grinning: :+1: :coffee: :australia:

Please do not do that. Now, the comments made by forum members no longer make sense because you changed your original post.

It's ok to edit your posts to correct spelling errors, fix broken links, add code tags when they were missing and so on. But it's not considered ok to change your post in a way that makes the topic impossible to understand when read from the first post to the last post. Other forum members wishing to help you, or beginners with a similar problem hoping to learn something will only get confused!

okay, here there are.
this code I was trying to see what happens with one sensor and one variable

//incluir librerias
#include <Statistic.h>
#include <OneWire.h>
#include <DallasTemperature.h>


//declarar variables en el pin de arduino
#define Delay 5000
unsigned long lastMillis = 0;
const int oneWirePin = 5;
OneWire oneWireBus(oneWirePin);
DallasTemperature sensors(&oneWireBus);

//Dirección de los sensores en Hexadecimal
DeviceAddress sensor1 = { 0x28, 0x7D, 0x4E, 0x56, 0xB5, 0x01, 0x3C, 0x06 };

//Variables del programa y escritura y lectura de las variables
float SensTemp_1 = sensors.getTempC(sensor1);
float Max;

void setup(void)
{
  //declaramos variables fijas solamente lo ejecutamos una vez
  Serial.begin(9600);
  sensors.begin();
  sensors.setResolution(sensor1, 10);//sensor de termometro 1
}
//creamos una funcion de punto a punto para los sensores en hexadecimal
void printsensor(DeviceAddress deviceAddress)
{

//creamos una serie de variables para esta función
  float temp;
  float tempC = sensors.getTempC(deviceAddress);//tempC es Temperatura en Grados centigrados, y get.TempC es una función donde recoje los datos
  if (tempC == -127.00) {
    Serial.print("No hay señal de temperatura, revisa el cableado eléctrico");
    //en caso si no hubiera nada conectado en la placa de prototipo o mal direccionado en el programa
  } else {
    Serial.print(tempC);
   Serial.println(" ºC");//muestra la temperatura en grados centigrados en monitor serie
  }  
} 

void loop(void)
{  unsigned long currentMillis = millis();
 if(currentMillis -lastMillis >= Delay){ 
  lastMillis = currentMillis;
  Max = SensTemp_1;
  if(SensTemp_1>Max)Max = SensTemp_1;
  
  Serial.println("Leyendo temperaturas");
  sensors.requestTemperatures();//Esperando respuesta de los sensores
  Serial.print("Temperatura sensor 1: ");
  printsensor(sensor1);    
  Serial.println(Max);    
  }
}


Sorry, Its my first Post on Forum

That won't work if you do not have this line immediately before it.

float SensTemp_1 = sensors.getTempC(sensor1);

SensTemp_1 is not a function.
You need to use this line to read the sensor when you need to, it isn't automatic.

Tom... :grinning: :+1: :coffee: :australia:

So...how can I make it automatic or refresh the variable?
At this moment...I´m feeling so stupid :sweat_smile:

Hi,
I have edited your code.
See where I have added;

  float SensTemp_1 = sensors.getTempC(sensor1); // this function reads the dallas sensor.
//incluir librerias
#include <Statistic.h>
#include <OneWire.h>
#include <DallasTemperature.h>


//declarar variables en el pin de arduino
#define Delay 5000
unsigned long lastMillis = 0;
const int oneWirePin = 5;
OneWire oneWireBus(oneWirePin);
DallasTemperature sensors(&oneWireBus);

//Dirección de los sensores en Hexadecimal
DeviceAddress sensor1 = { 0x28, 0x7D, 0x4E, 0x56, 0xB5, 0x01, 0x3C, 0x06 };

//Variables del programa y escritura y lectura de las variables
float SensTemp_1 = sensors.getTempC(sensor1);
float Max;

void setup(void)
{
  //declaramos variables fijas solamente lo ejecutamos una vez
  Serial.begin(9600);
  sensors.begin();
  sensors.setResolution(sensor1, 10);//sensor de termometro 1
}
//creamos una funcion de punto a punto para los sensores en hexadecimal
void printsensor(DeviceAddress deviceAddress)
{
  //creamos una serie de variables para esta función
  float temp;
  float tempC = sensors.getTempC(deviceAddress);//tempC es Temperatura en Grados centigrados, y get.TempC es una función donde recoje los datos
  if (tempC == -127.00)
  {
    Serial.print("No hay señal de temperatura, revisa el cableado eléctrico");
    //en caso si no hubiera nada conectado en la placa de prototipo o mal direccionado en el programa
  }
  else
  {
    Serial.print(tempC);
    Serial.println(" ºC");//muestra la temperatura en grados centigrados en monitor serie
  }
}

void loop(void)
{
  unsigned long currentMillis = millis();
  if (currentMillis - lastMillis >= Delay)
  {
    lastMillis = currentMillis;
    float SensTemp_1 = sensors.getTempC(sensor1); // this function reads the dallas sensor.
    Max = SensTemp_1;
    if (SensTemp_1 > Max)Max = SensTemp_1;
    Serial.println("Leyendo temperaturas");
    sensors.requestTemperatures();//Esperando respuesta de los sensores
    Serial.print("Temperatura sensor 1: ");
    printsensor(sensor1);
    Serial.println(Max);
  }
}

Untried but it compiles.

Tom... :grinning: :+1: :coffee: :australia:

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.