Hello,
I have as project making a temperature and Ph measurement, and at the same time present the values in a display.
I have 'more or less' succeed to do what I want to do, I based my project on this tutorial:
where they use the max6675 breakboard in combination with a k type thermocouple to measure the temperature.
my question is coming..., right now (as in the tutorial), the measurement is inside the loop () function, using thermocouple.readCelsius() from the thermocouple object I created before the setup(). this function will get a temperature value at each iteration, but the thing is that the speed at what the loop() function is run will change as I add more to it (for example the update of the display). My first thought (I am still learning...) would be to grave 'as many temperature values as I can' and once a complete second passed present an average value of these measured temperatures as the value for the second. but several questions arise from this,
is the correct approach to do?
I am new to arduino programming AND C++, how can I append the values to a list to calculate the average, if I don't know how many values I will get in a second (as the time for the loop() function variates slightly each time)
it is obvious that I am limited at one point by the speed of the max6675/thermocouple, no? how one should take into account this? is it possible that the loop() function goes 'faster' than what the sensor will respond? or not because when running the function thermocouple.readCelsius() it will wait till it gets the value?
a good example for achieving something similar? all tutorials I have found around (up to now) they go until 'hey you got a value, great' but not about how to correctly place them in time for example. should I use the millis() function and calculate a delta time in each iteration of the loop() function? or there is a more 'correct'/'elegant' way to do it?
regards
The first beginners problem you likely have is displaying information every time through your loop() function. Display update should only be done when the value changes. In addition, ONLY display the numeric value. Display all other stuff only in setup() and not every update.
To calculate the average you need to define some variables: (some snippet)
float sum = 0.0;
uint32_t count = 0;
uint32_t lastAverageDisplay = 0;
void loop()
{
if (millis() - lastAverageDisplay >= 1000)
{
// update last display moment, once per second = once per 1000 millis
lastAverageDisplay += 1000;
// calc and print average
float average = sum / count;
Serial.println(average, 3);
// reset counters
sum = 0;
count = 0;
}
// add as much measurements as loop allows
sum += thermocouple.readCelsius();
count += 1;
}
Hint: To make information useless display and change it faster then the user can read it. You could read the temperature every 30 seconds, it should not change that much. But you can watch and if it changes more then x then display it again.
Your questions are absolutely valid. In general first you will need to read some datasheets and do some maths, before start programing. Or in iterations.
For example, as you said, you can't sample a sensor faster than the time that it needs to respond with a correct value. So, check in the datasheet of the sensor what is the minimum safe time between samples. Or, do an experiment with a simple setup to check it. Or ask here But I don't know that concrete sensor.
The same for the rest of elements regarding what you want to do with them. Get the numbers and do the calculations.
When you know the values that you need per second and what the sensor can do, you can program a loop to do the samples at the exact rate that you need. There are several options to do it.
You should find the answers in the datasheet of the sensor, the library documentation. Or by experimenting. Or asking.
Probably the MCU can go much faster than the sensor, the display, the serial port... and the eyes of the user.
This is one option and there are others. Depends on your requirements. If high precision is not so important, using millis() to pool at intervals should be ok. You can search here in the forum, or ask for concrete options.