Create variables during runtime

Hallo! Newbie here…

I’m building a remote temperature sensor (like hundreds of people starting with Arduino) combining 3 Dallas temp senstors and the ThingSpeak library.

To print the temperature values of the 3 sensors, the example sketch creates a loop to to go through every sensor on the bus. After a temperature is measured, it’s stored as “tempC” and overwritten on the next loop. No problem as it’s already printed anyway.

Now I want to upload these values to TingSpeak. In order to do that I would need all 3 values still alible after the loop as t0, t1, t2 but I’ve found that it can’t be done as per below because “variables are defend by the compiler so can’t be created dynamically during the runtime”?

My 2 questions are:

  1. Is that indeed the case or do I miss a simple ; or {} or so?
  2. If indeed true, how can I makes all 3 values available for writing to ThingSpeak after the loop has run n-times?
sensors.requestTemperatures(); // Send the command to get temperatures

  // Loop through each device, print out temperature data
  for (int i = 0; i < numberOfDevices; i++) {
    // Search the wire for address
    if (sensors.getAddress(tempDeviceAddress, i)) {
      // Output the device ID
      Serial.print("Temperature for device: ");
      Serial.print(i, DEC);
      // Print the data
      float tempC = sensors.getTempC(tempDeviceAddress);
      float t(i, DEC) = sensors.getTempC(tempDeviceAddress);   // <= This is giving me headache!!
      Serial.print(" = ");
      Serial.print(tempC);
      Serial.println(" °C");
    }
  }
  Serial.println();
    // set the fields with the values
    ThingSpeak.setField(1, t0);
    ThingSpeak.setField(2, t1);
    ThingSpeak.setField(3, t2);

I’m coming from this tutorial btw, combining it with the example sketches provided by ThingSpeak.

You are getting confused.

You define (all) the necessary variables at the start of your code.

When the measurements are made, they are stored in the variables and processed/ printed on each pass through the loop.

If you want to log them for a later process, you need to put them in some sort of larger storage. There is a small amount of EEPROM on an Arduino, if this is insufficient you will need a dedicated EEPROM chip or SD card.

Exactly how many readings do you propose to store?

Hi Paul, thanks for the reply.

I only need to store 3 values. Once they are uploaded to TingSpeak they can be overwritten again in the next loop (some 5 minutes later). However now they are overwritten when the next line is printed during the loop. The loop runs 3 times as I have 3 sensors connected.

But how do I get the 1st loop to store the values that are measured in 3 variables to I can pick them up for the 2nd part and write them to ThingSpeak?

Can you explain more about how often you read the temperatures, and how often you want to send the reading to thingspeak. It’s not clear to me if you want to send the last available reading, or if you want some average of all the readings between the last data sent to thingspeak?

You can certainly declare a global array of variables to hold the temperature readings.

float valuesForThingspeak[3];

Fill them here

float tempC = sensors.getTempC(tempDeviceAddress);
//float t(i, DEC) = sensors.getTempC(tempDeviceAddress);   // <= This is giving me headache!!
valuesForThingspeak[i] = tempC;  //or += tempC for taking an average

Then

    // set the fields with the values
    ThingSpeak.setField(1, valuesForThingspeak[0]);
    ThingSpeak.setField(2, valuesForThingspeak[1] );
    ThingSpeak.setField(3,valuesForThingspeak[2]);

As you do not appear to have actually posted your code, I do not think we can help you! :roll_eyes:

cattledog:
Can you explain more about how often you read the temperatures, and how often you want to send the reading to thingspeak. It’s not clear to me if you want to send the last available reading, or if you want some average of all the readings between the last data sent to thingspeak?

You can certainly declare a global array of variables to hold the temperature readings.

float valuesForThingspeak[3];

Fill them here

float tempC = sensors.getTempC(tempDeviceAddress);

//float t(i, DEC) = sensors.getTempC(tempDeviceAddress);   // <= This is giving me headache!!
valuesForThingspeak[i] = tempC;  /or += tempC for taking an average




Then


// set the fields with the values
   ThingSpeak.setField(1, valuesForThingspeak[0]);
   ThingSpeak.setField(2, valuesForThingspeak[1] );
   ThingSpeak.setField(3,valuesForThingspeak[2]);

It’s indeed about sending the last reading (of all 3 sensors) so your suggestion works great. Thanks!
So simple… but as mentioned I’m quite new to this.

Now every measurement is send to ThingSpeak (runs via a timer every 5 min).

TingSpeak sends back a http status code, so my next plan is to store the values until it returns a 200 “OK” and then " flushes" the measurements. This means I’d also need to store the timestamp of the measurement and expand the code quite a bit. For now that’s way beyond my knowledge but will learn over time.

Thanks to you both for your help!

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