Go Down

Topic: how to save previous values in memory and print them to serial (Read 157 times) previous topic - next topic

peter12

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?

Code: [Select]
#include <SoftwareSerial.h>
#include <OneWire.h>
#include <DallasTemperature.h>

#define RX    3   // *** D3, Pin 2
#define TX    4   // *** D4, Pin 3
SoftwareSerial mySerial(RX, TX);

#define ONE_WIRE_BUS 1
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors (&oneWire);

static uint32_t counter = 0;

void setup() {               
  //init
  mySerial.begin(9600);
  mySerial.println("Dallas Temperature IC Control Library Initializing...");

  sensors.begin();

  mySerial.println("Initialization complete.");
}


void loop() {
  //sensor temperature reading
  sensors.requestTemperatures();
  double tempC = sensors.getTempCByIndex(0);
 
  //here I want to print all previous temperature values
  mySerial.print(counter);
  mySerial.print(": ");
  mySerial.print(tempC, 1);
  mySerial.println("C, ");
  counter++;

  delay(10000); //delay 10 seconds
}

dougp

What is your definition of a 'cycle'?

Be advised, the sketch will do nothing for a whole ten seconds while in delay(10000).  See IDE file/examples/digital/blink without delay.

An array is a common way to attack the problem of dealing with a series of things.
Everything we call real is made of things that cannot be regarded as real.  If quantum mechanics hasn't profoundly shocked you, you haven't understood it yet. - Niels Bohr

No private consultations undertaken!

JaBa

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.

pylon

Please keep in mind that the ATtiny85 has 512 Bytes of RAM, saving a lot of values is not possible with this type of MCU.

peter12

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.5*23.1*22.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?

Sorry for noob questions :)

pylon

Quote
But I woild like to store more values, it can be in one variable e.g. with delimiter * such as 21.5*23.1*22.1, but when I print it in serial, then just first 42 chars are shown.
You can declare

Code: [Select]
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.

Quote
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?

peter12

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.

peter12

I found out that, probably, I could save temperature values to EEPROM :)

pylon

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).

Go Up