Hi, I am trying to use attiny85 to get temperature values each time in cycle and store them to variable and at the end of each cycle print them to serial. I made this code to print current temperature value each cycle in loop. But how should I edit my code to save previous temperature values in variable and print them all together every time in the loop?
loop() will be restarted each time it Ends. This is done automatically by the Arduino. A normal variable which was defined within loop() will therefore not retain ist value for use in subsequent calls.
There are two ways to calculate a value within loop() which will retain its value for subsequent calls.
If the variable were to be made available anywhere in any function of the current program, you would have to use what is called a global variable. (Global because it will be available anywhere and everywhere.) To define a global variable, simple do so outside of any function, typically at the beginning of the program before the Setup function.
If the variable is only needed within the loop() function, there is a second way to do it. Define the variable within loop but using the modifier static.
In your code, you have already used both methods. You defined Counter as a global variable (because it is declared outside of any function). You also used the modifier static (which I can't imagine has any effect on a global variable).
So, simply define a variable such as double last_tempC and then at the end of loop set last_tempC = tempC.
Thank you guys! Yes I can declare last_tempC but it stores just one value. But I woild like to store more values, it can be in one variable e.g. with delimiter * such as 21.523.122.1, but when I print it in serial, then just first 42 chars are shown. What am I doing wrong please?
And how can I please calculate how many temperatures can I store in memory?
But I woild like to store more values, it can be in one variable e.g. with delimiter * such as 21.523.122.1, but when I print it in serial, then just first 42 chars are shown.
You can declare
float temps[10];
This allows you to hold up to 10 temperatures in memory. A for loop over the array may print it to the serial interface.
And how can I please calculate how many temperatures can I store in memory?
The Arduino IDE tells you at the end of the compile run how many bytes are used by global variables. In addition to that value you need two bytes for every subroutine call, memory for the local variables (both on the stack) and some memory for the internal IDE functions (interrupts for time keeping and the like).
You see, the calculation is quite complicated so you should keep it to the absolute minimum.
What's the reason to store the values in RAM and not printing them immediately?
pylon:
What's the reason to store the values in RAM and not printing them immediately?
I am trying to make small device, which will be able to log e.g.100 last temperature values (it is logging once per hour). From time to time device is connected to serial port, so I would like to print all stored values via serial. This is enough for me. But I think, attiny85 has not enough memory for this, so I should use some kind of external memory. I hope for very simple project, but it looks it is getting bigger and bigger (for my current knowledge, but I am glad I will learn something new). Any link or idea is highly appreciated.
You can do that but don't use floats, you can request the temperature value as the raw 12bit value the sensor returns which fits into a 16bit integer. That needs half the memory (in RAM and EEPROM). You can do the conversion to a float later on your PC (or whatever device you use to read out the values).