Functions returning variables

Thanks for every bodies help. I’ve got it working now by declaring a static variable.
Hopefully, in another 50 years or so, I may be competent. (but I wouldn’t bet on it.)

We all started nowhere, we all have a long way to go. If not so much in this hobby, with something, unless we already dead sitting at home waiting to die for realz.

How big did you make the static array? Just curious.

Perhaps you could post your entire sketch that you will be working with now, that works for want it is meant for at the moment.

a7

I've done it as below which appears to be working.

These are parts of the actual sketch that I want it for rather than the test sketch that I originally posted. It outputs everything as a comma delineated .csv file so that I can make pretty graphs.
in excell.

      sprintf(readingTimestamp, "%02d/%02d/%04d %02d:%02d:%02d,", timeinfo.tm_mday, timeinfo.tm_mon + 1, timeinfo.tm_year + 1900, timeinfo.tm_hour, timeinfo.tm_min, timeinfo.tm_sec);
      appendFile(SD, fileName, readingTimestamp);
      appendFile(SD, fileName, NumToChar(humid));
      appendFile(SD, fileName, NumToChar(temperature2));
      appendFile(SD, fileName, NumToChar(pressure));
      appendFile(SD, fileName, "\n");  //new line
char* NumToChar(float number) {
  String numberAsString = String(number, 3) + ",";
  static char numberAsChar[10];
  numberAsString.toCharArray(numberAsChar, 10);
  //Serial.print(numberAsChar);

  return numberAsChar;
}

This is the whoe sketch

#include <Arduino.h>
#include <SD.h>
#include <DHT22.h>
#include <Wire.h>
#include <WiFi.h>
#include "time.h"
#include "SPI.h"
#include "FS.h"
#include <Bme280.h>
//#include <BME280I2C.h>

#define LED_Flash 25
#define I2C_SDA 33
#define I2C_SCL 32
Bme280TwoWire sensor;
//BME280I2C bme;    // Default : forced mode, standby time = 1000 ms
// Oversampling = pressure ×1, temperature ×1, humidity ×1, filter off,

DHT22 dht22(17);

unsigned long intervalCheck;
const char* ssid = "Utopia";
const char* password = "password";
const char* ntpServer = "pool.ntp.org";
const long gmtOffset_sec = 3600 * 0;
const int daylightOffset_sec = 3600 * 0;

float temperature;
float humidity;
float humid;
char fileName[18];
char readingTimestamp[30];
char readings[30];

const float Av = 5.0;  //number of readings for average
const int Av1 = int(Av);

float humidAv[Av1];
byte humCount = 0;
bool start = 1;
int refTime = 0;
File myFile;


/////////////////////////////////////////////////////////////////////
void setup() {
  delay(5000);
  pinMode(LED_Flash, OUTPUT);
  Serial.begin(115200);
  for (int x = 0; x < 10; x++) {
    Serial.println();
  }
  Serial.print("Initializing SD card...");

  if (!SD.begin()) {
    Serial.println("Card Mount Failed");
    return;
  }
  uint8_t cardType = SD.cardType();

  if (cardType == CARD_NONE) {
    Serial.println("No SD card attached");
    return;
  }

  Serial.print("SD Card Type: ");
  if (cardType == CARD_MMC) {
    Serial.println("MMC");
  } else if (cardType == CARD_SD) {
    Serial.println("SDSC");
  } else if (cardType == CARD_SDHC) {
    Serial.println("SDHC");
  } else {
    Serial.println("UNKNOWN");
  }
  WiFi.mode(WIFI_STA);  // Optional
  WiFi.begin(ssid, password);
  Serial.println("\nConnecting");

  while (WiFi.status() != WL_CONNECTED) {
    digitalWrite(LED_Flash, HIGH);
    Serial.print(".");
    delay(100);
    digitalWrite(LED_Flash, LOW);
    delay(100);
  }
  Serial.println(" Connected");

  listDir(SD, "/", 0);
  Serial.println();
  Serial.printf("Total space: %lluMB\n", SD.totalBytes() / (1024 * 1024));
  Serial.printf("Used space: %lluMB\n", SD.usedBytes() / (1024 * 1024));


  configTime(gmtOffset_sec, daylightOffset_sec, ntpServer);

  struct tm timeinfo;
  if (!getLocalTime(&timeinfo)) {
    Serial.println("Failed to obtain time");
  }





  delay(3000);


  intervalCheck = (timeinfo.tm_min * 60 + timeinfo.tm_sec);

  Wire.begin(I2C_SDA, I2C_SCL);

  /*while(!bme.begin())
  {
    Serial.println("Could not find BME280 sensor!");
    delay(1000);
  }

  switch(bme.chipModel())
  {
     case BME280::ChipModel_BME280:
       Serial.println("Found BME280 sensor! Success.");
       break;
     case BME280::ChipModel_BMP280:
       Serial.println("Found BMP280 sensor! No Humidity available.");
       break;
     default:
       Serial.println("Found UNKNOWN sensor! Error!");
  }*/

  sensor.begin(Bme280TwoWireAddress::Primary);
  sensor.setSettings(Bme280Settings::indoor());



  digitalWrite(LED_Flash, HIGH);
}


/////////////////////////////////////////////////////////////////
void loop() {
  //humid_temp();

  if (touchRead(4)<40)
  {
    digitalWrite(LED_Flash, HIGH);
    delay(10000);
    digitalWrite(LED_Flash, LOW);
  }

  float temperature = dht22.getTemperature() * 1.0;
  delay(50);
  float humidity = dht22.getHumidity() * 1.0;
  delay(50);

  struct tm timeinfo;
  if (!getLocalTime(&timeinfo)) {
    Serial.println("Failed to obtain time");
  }


  if ((timeinfo.tm_min * 60 + timeinfo.tm_sec - intervalCheck) >= (Av * 5)) {  //check for interval
    intervalCheck = (timeinfo.tm_min * 60 + timeinfo.tm_sec);
    if (humidity != humid) {  //check if humidity has changed since last reading

      if (start) {
        sprintf(fileName, "/%02d%02d%02d00.csv", timeinfo.tm_mon + 1, timeinfo.tm_mday, timeinfo.tm_hour);  // make file name from date and time
        Serial.println();
        Serial.print("File Name =  ");
        Serial.println(fileName);

        for (int i = 0; i <= Av - 1; i++) {



          /*set start humidity, all values in matrix from 1 reading
          to stop initial readings being unusually HIGH
          */


          humidAv[i] = humidity;
        }
        refTime = timeinfo.tm_mday*24+timeinfo.tm_hour;
        start = 0;
      }

      float hum = 0.0;
      humidAv[humCount] = humidity;
      for (int i = 0; i <= Av - 1; i++) {  //find mean humidity over 5 readings

        hum = hum + humidAv[i];
      }
      humid = float(hum / Av);
      hum = 0;
      humCount++;
      if (humCount > Av - 1) {
        humCount = 0;
      }

      float temperature2 = sensor.getTemperature();
      float pressure = sensor.getPressure() * 1.005;
      

      Serial.print(&timeinfo, "%A, %B %d %Y %H:%M:%S ");

      digitalWrite(LED_Flash, HIGH);

      Serial.print("   ");
      Serial.print("BME289 T = ");
      Serial.print(temperature2, 2);
      Serial.print(" ");
      Serial.print(" deg. C,   T = ");

      Serial.print("DHT22 T = ");
      Serial.print(temperature, 2);
      Serial.print(" ");
      Serial.print(" deg. C,   H = Av ");

      Serial.print(humid, 2);
      Serial.print(" actual ");
      Serial.print(humidity, 2);
      Serial.print("%    ");
      Serial.print("P = ");
      Serial.print(pressure);
      Serial.println(" Pa ");




      sprintf(readingTimestamp, "%02d/%02d/%04d %02d:%02d:%02d,", timeinfo.tm_mday, timeinfo.tm_mon + 1, timeinfo.tm_year + 1900, timeinfo.tm_hour, timeinfo.tm_min, timeinfo.tm_sec);
      appendFile(SD, fileName, readingTimestamp);
      appendFile(SD, fileName, NumToChar(humid));
      appendFile(SD, fileName, NumToChar(temperature2));
      appendFile(SD, fileName, NumToChar(pressure));
      appendFile(SD, fileName, "\n");  //new line

      appendFile(SD, "/Ongoing.csv", readingTimestamp);
      appendFile(SD, "/Ongoing.csv", NumToChar(humid));
       appendFile(SD, "/Ongoing.csv", NumToChar(temperature2));
       appendFile(SD, "/Ongoing.csv", NumToChar(pressure));
      appendFile(SD, "/Ongoing.csv", "\n");  //new line


      humid = humidity;
    }


    if (timeinfo.tm_mday*24+timeinfo.tm_hour > refTime) {
      start = 1;

    }
  }


  digitalWrite(LED_Flash, LOW);
}

//////////////////////////////////////////////////////////////////////

char* NumToChar(float number) {
  String numberAsString = String(number, 3) + ",";
  static char numberAsChar[10];
  numberAsString.toCharArray(numberAsChar, 10);
  //Serial.print(numberAsChar);

  return numberAsChar;
}

//////////////////////////////////////////////////////////////////////


void appendFile(fs::FS& fs, const char* path, const char* message) {


  File file = fs.open(path, FILE_APPEND);
  if (!file) {
    Serial.println("Failed to open file for appending");
    return;
  }
  if (file.print(message)) {

  } else {
    Serial.println("Append failed");
  }
  file.close();
}
///////////////////////////////////////////////////////////////////
void listDir(fs::FS& fs, const char* dirname, uint8_t levels) {
  Serial.printf("Listing directory: %s\n", dirname);

  File root = fs.open(dirname);
  if (!root) {
    Serial.println("Failed to open directory");
    return;
  }
  if (!root.isDirectory()) {
    Serial.println("Not a directory");
    return;
  }

  File file = root.openNextFile();
  while (file) {
    if (file.isDirectory()) {
      Serial.print("  DIR : ");
      Serial.println(file.name());
      if (levels) {
        listDir(fs, file.path(), levels - 1);
      }
    } else {
      Serial.print("  FILE: ");
      Serial.print(file.name());
      Serial.print("  SIZE: ");
      Serial.println(file.size());
    }
    file = root.openNextFile();
  }
}
///////////////////////////////////////////////////////////
1 Like

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