Simplifying code

Hello there,
I'm using Arduino since a few weeks, I am familiar with the C# environment and have coded a lot of programms for microcontrollers from Velleman to automize my greenhouse, but now these days my new computer doesn't work with these older cards, so I have chosen for an Arduino based solution, I'm surching already a few days to shorten the code but I'm stuck here, I always get errors, so can anyone help me out here?
I made a sketch where I want to make the changes, you can load it simply in an ESP32 WROOM DEV kit without tft or so, the code I use will be uploaded and changed by the interaction on the tft, but there I didn't had a problem to write and use the coding. Only fill in the wifi connection and your password. So here's the modified code, because I already have much more code.

// Using ESP32 Dev Module ESP32 WROOM 32 from AZ delivery https://www.az-delivery.de/en
/* Include libraries */
#include <FS.h>    // File system library
#include <SPI.h>   // ESP32 Arduino core library
#include <SD.h>    // SD card library
#include "time.h"  // C:\Users\User\AppData\Local\Arduino..\packages\esp32\hardware\esp32\2.0.9\tools\sdk\esp32\include\newlib\platform_include\*/
#include <WiFi.h>  // This library allows an Arduino board to connect to the internet
#include <Wire.h>  // Wiring library
/*
 * Connect the µSD card module to the following pins:
 *
 * µSD Card module    | ESP32
 *
 * CS                   SD D0/GPIO2
 * MOSI                 MOSI/GPIO23
 * VSS/GND              GND
 * VDD/VCC              5V
 * CLK/SCK              SCK/GPIO18
 * MISO                 MISO/GPIO19
 *
 * TFT SPI             | Resistor      | ESP32
 *
 * VCC                   -               5V
 * GND                   -               GND
 * CS                    1KΩ             SS/GPIO5
 * RESET/RST             1KΩ             GPIO22 OR 3.3V
 * D/C                   1KΩ             GPIO4
 * SDI/MOSI              1KΩ             MOSI/GPIO23
 * SCK                   1KΩ             SCK/GPIO18
 * LED                   -               3.3V or GPIO15 (if used to save screen)
 * SDO/MISO              1KΩ             MISO/GPIO19
 *
 * SD CARD (if used)   | ESP32
 * 9 D2                  -
 * 1 D3                  GPIO2
 * 2 CMD                 MOSI/GPIO23
 * 3 VSS                 GND
 * 4 VDD                 3.3V
 * 5 CLK                 SCK/GPIO18
 * 6 VSS                 GND
 * 7 DO                  MISO/GPIO19
 * 8 D1                  -
 *
 */
/*
Connect following pins from the DHT to the ESP32
*   DHT      Resistor           ESP32
*    1          -               3.3V
*    2         10KΩ             3.3V
*    2          -               12
*    3          -               -
*    4          -               GND
*/

// Disconnect GPIO22 and wire the tft pin RESET to 3.3V
// BMP 280 connection           ESP32
/* VCC                          3.3V
 *  GND                          GND
 *  SCL                          21
 *  SDA                          22
 */

// Identification and password needed to connect to your router
const char *ssid = "your ssid";
const char *password = "password";


const char *ntpServerName = "pool.ntp.org";
const long gmtOffset_sec = 3600;
const int daylightOffset_sec = 3600;

struct tm timeInfo;
// We need a number to switch between pages
int Page = 0;
char prog0[13] = { "111000009001" };  // For example set the output on monday, tuesday, wednesday high at 09:00, the last digit 1 = pinMode high
char prog1[13] = { "111111109010" };  // Every day we set the output off at 09:01, the last digit = 0, so pinMode low
char prog2[13] = { "100000018101" };  // Monday set output on at 18:10
char prog3[13] = { "100000018150" };  // Monday set output off at 18:15
int selProg = 0;
int selIO = 1;
File myFile;
char fileName[] = "/programm.txt";
const int chipSelect = 2;
char charRead;
char inputString[13];  //string to hold read string
int c50 = 0;
void setup() {
  Serial.begin(115200);
  // connect to WiFi
  Serial.printf("Connecting to %s ", ssid);
  WiFi.begin(ssid, password);
  int i = 0;
  while (i < 15 && WiFi.status() != WL_CONNECTED) {
    i++;
    Serial.print(".");
    if (i == 15) {
      // disconnect WiFi as it's no longer needed
      WiFi.disconnect(true);
      WiFi.mode(WIFI_OFF);
      Serial.println("WIFI disconnected");
      break;
    }
    delay(500);
  }
  if (WiFi.status() == WL_CONNECTED) {
    Serial.println(" CONNECTED");
  }
  // init and get the time
  configTime(gmtOffset_sec, daylightOffset_sec, ntpServerName);

  Serial.println("Initializing System")
    /* Setup the sd-card and test the type, send the information to the serial monitor */
    if (!SD.begin(2)) {  // Select chip, use SD_CS GPIO 2
    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");
  }

  uint64_t cardSize = SD.cardSize() / (1024 * 1024);
  Serial.printf("SD Card Size: %lluMB\n", cardSize);
  Serial.println("initialisation done.");
  writeToFile();
  delay(2000);
  readFromFile();
  printFile();
}

void loop() {
  loadProgramm();
  delay(20000);
}

void writeToFile() {
  myFile = SD.open("/programm.txt", FILE_WRITE);
  if (myFile)  // it opened OK
  {
    Serial.println("Writing to programm.txt");
    myFile.println(prog0);
    myFile.println(prog1);
    myFile.println(prog2);
    myFile.println(prog3);
    // close the file:
    myFile.close();
    Serial.println("Done");
  } else
    Serial.println("Error opening programm.txt");  // programm.txt does not exist
  // re-open the file for reading:
  myFile = SD.open("/programm.txt");
  if (myFile) {
    Serial.println("Writing to programm.txt");
    myFile.println(prog0);
    myFile.println(prog1);
    myFile.println(prog2);
    myFile.println(prog3);
    Serial.println("programm.txt created");
    // read from the file until there's nothing else in it:
    while (myFile.available()) {
      Serial.println("Programm text: ");
      Serial.write(myFile.read());
    }
    // close the file:
    myFile.close();
  } else {
    // if the file didn't open, print an error:
    Serial.println("error opening the text file");
  }
}
void readFromFile() {
  int d = 0;
  byte i = 0;  //counter
  //now read it back and show on Serial monitor
  // Check to see if the file exists:
  if (!SD.exists("/programm.txt"))
    Serial.println("programm.txt doesn't exist.");
  Serial.println("Reading from programm.txt:");
  myFile = SD.open("/programm.txt");

  while (myFile.available()) {
    char inputChar = myFile.read();  // Gets one byte from serial buffer
    if (inputChar == '\n')           //end of line (or 10)
    {
      inputString[i] = 0;  //terminate the string correctly
      Serial.println(inputString);
      if (d == 0) {
        substituteChars(inputString, prog0, 0, 11);
      }
      if (d == 1) {
        substituteChars(inputString, prog1, 0, 11);
      }
      if (d == 2) {
        substituteChars(inputString, prog2, 0, 11);
      }
      if (d == 3) {
        substituteChars(inputString, prog3, 0, 11);
      }
      d++;
      i = 0;
    } else {
      inputString[i] = inputChar;  // Store it
      i++;
    }
    if (i > sizeof(inputString)) {
      Serial.println("Incoming string longer than array allows");
      Serial.println(sizeof(inputString));
      while (1)
        ;
    }
  }
  // close the file:
  myFile.close();
  Serial.println("Done reading file");
  printFile();
}
void printFile() {
  Serial.print("programma 0 = ");
  Serial.println(prog0);
  Serial.print("programma 1 = ");
  Serial.println(prog1);
  Serial.print("programma 2 = ");
  Serial.println(prog2);
  Serial.print("programma 3 = ");
  Serial.println(prog3);
}
void substituteChars(char source[], char destination[], byte startAt, byte length) {
  for (int pos = 0; pos < length; pos++) {
    destination[startAt + pos] = source[pos];
  }
}
// So far everything goes fine
void loadProgramm() {
  int dayNow;
  int daySet;
  /*
  hour = timeinfo.tm_hour;
  min = timeinfo.tm_min;
  sec = timeinfo.tm_sec;
  day = timeinfo.tm_mday;
  month = timeinfo.tm_mon + 1;
  year = timeinfo.tm_year +1900;
  wday = int from Sunday 0-6
  tm_isdst = int	Daylight Saving Time
  see also: https://cplusplus.com/reference/ctime/tm/
  */
  int hourNow = timeInfo.tm_hour;
  String setMin = String(prog0[9]);
  if (setMin != "0") {
    setMin += String(prog0[10]);
  } else {
    setMin = String(prog0[10]);
  }
  String setHour = String(prog0[7]);
  if (setHour != "0") {
    setHour += String(prog0[8]);
  } else {
    setHour = String(prog0[8]);
  }
  int minNow = timeInfo.tm_min;
  char onOff = prog0[11];
  int i = timeInfo.tm_wday;
  String weekday = String(i);
  if (i == 0) {  // Sunday
    i = 7;
  }
  // if (prog0 != "000000000000" || prog0 != "100000000000") {
  // Programm at I/O1 != empty
  if (String(prog0[i - 1]) == "1" && String(hourNow) == String(setHour) && String(minNow) == String(setMin)) {
    if (String(onOff) == "1") {
      // output high
      Serial.println("Output on");
    } else {
      // output low
      Serial.println("Output off");
    }
  }
  /*} else {
    Serial.println("Programm empty");
  }*/
  setMin = String(prog1[9]);
  if (setMin != "0") {
    setMin += String(prog1[10]);
  } else {
    setMin = String(prog1[10]);
  }
  setHour = String(prog1[7]);
  if (setHour != "0") {
    setHour += String(prog1[8]);
  } else {
    setHour = String(prog1[8]);
  }
  minNow = timeInfo.tm_min;
  onOff = prog1[11];
  if (String(prog1[i - 1]) == "1" && String(hourNow) == String(setHour) && String(minNow) == String(setMin)) {
    if (String(onOff) == "1") {
      // output high
      Serial.println("Output on");
    } else {
      // output low
      Serial.println("Output off");
    }
  }
  setMin = String(prog2[9]);
  if (setMin != "0") {
    setMin += String(prog2[10]);
  } else {
    setMin = String(prog2[10]);
  }
  setHour = String(prog2[7]);
  if (setHour != "0") {
    setHour += String(prog2[8]);
  } else {
    setHour = String(prog2[8]);
  }
  onOff = prog2[11];
  if (String(prog2[i - 1]) == "1" && String(hourNow) == String(setHour) && String(minNow) == String(setMin)) {
    if (String(onOff) == "1") {
      // output high
      Serial.println("Output on");
    } else {
      // output low
      Serial.println("Output off");
    }
  }
  setMin = String(prog3[9]);
  if (setMin != "0") {
    setMin += String(prog3[10]);
  } else {
    setMin = String(prog3[10]);
  }
  setHour = String(prog3[7]);
  if (setHour != "0") {
    setHour += String(prog3[8]);
  } else {
    setHour = String(prog3[8]);
  }
  onOff = prog3[11];
  if (String(prog3[i - 1]) == "1" && String(hourNow) == String(setHour) && String(minNow) == String(setMin)) {
    if (String(onOff) == "1") {
      // output high
      Serial.println("Output on");
    } else {
      // output low
      Serial.println("Output off");
    }
  }
}
// How can I make the loadProgramm, readFromFile and writeToFile more complex but with less code??
// Probably with a class or a function, but I don't know how, I only wrote classes a long time ago into JavaScript and Flash ActionScript
// I also wrote in C#

Please post the entire error listing verbatim. To be clear, you are compiling for an ESP32?

I made a sketch where I want to make the changes, you can load it simply in an ESP32 WROOM DEV kit without tft or so, the code I use will be uploaded and changed

Confused. This seems to reference code that hasn't been written or modified in some way yet. Can you explain further?

1 Like

What is that for?

@v205 The FS library is the filesystem to read/write to an sd card.

Hello @aarg @UKHeliBob , I have shorten the program because I use 5 outputs, every output has 6 programsteps, so I have prog0 to prog29 to write and read to an sd-card only once or when I select the buttons on the tft (load, write, read...). Below a picture how it looks like


The errors I don't have anymore because I worked out the code, but these code now is so long that I like to shorten in a more complex way by changing the prog(number) in a way like this I thought. The code is stored in a char Array named prog0, prog1 etc.
The data is coded in as: the first 7 characters represents the days 1 means set 0=unset, Monday = 1st byte (while by calling the time I will get Su = 0 so sunday will be 7 and all the other days are i -1), the 8th byte and the 9th byte represents the hours set on the tft and stored in the program byte8: 0-2 & byte 9: 0-9, byte 10 and 11 represents the minutes 10: 0-5 & 11: 0-9, and the last byte is to check if the output has to be set on or off.
This is the code I have now:

  if (prog0 != "000000000000" || prog0 != "100000000000") {
    // Programm at I/O1 != empty
    if (String(prog0[i - 1]) == "1" && String(hourNow) == String(setHour) && String(minNow) == String(setMin)) {
      if (String(onOff) == "1") {
        // output high
        Serial.println("Output on");
      } else {
        // output low
        Serial.println("Output off");
      }
    }
  }
if (prog1 != "000000000000" || prog1 != "1000000000") {
    // Program at I/O1 != empty
    if (String(prog1[i - 1]) == "1" && String(hourNow) == String(setHour) && String(minNow) == String(setMin)) {
      if (String(onOff) == "1") {
        // output high
        Serial.println("Output prog1 on");
      } else {
        // output low
        Serial.println("Output prog1 off");
      }
    }
  }// and so on

So I use prog0 to prog29 = 30 times the same code, only changed the prog into another number: prog0, prog1,prog2,prog3 etc.
the programm steps are loaded in an array: char prog[13] = {"value"};
Next problem by loading the program from the file on the sd card.
So I thought to do the following:

int d = 0;
char inputString[13];
char destin[13];
char charRead;
void readFromFile() {
  byte i = 0;

  if (!SD.exists("/programm.txt"))
    Serial.println("programm.txt doesn't exist.");
  Serial.println("Reading from programm.txt:");
  myFile = SD.open("/programm.txt");
  while (myFile.available()) {
    char inputChar = myFile.read();
    if (inputChar == '\n')  //end of line (or 10)
    {
      if (d < 30) {
        switch (d) {
          case 0:
            destin = prog0;
            break;
          case 1:
            destin = prog1;
            break;
            // and so on
        }
        changeDest(inputString, destin, 0, 11);
        d++;
        i = 0;
      } else {
        inputString[i] = inputChar;
        i++;
      }
      if (i > sizeOf(inputString)) {
        while (1)
          ;
      }
    }
  }
}
void changeDest(char source[], char destination[], byte startAt, byte length) {
  for (int pos = 0; pos < length; pos++) {
    destination[startAt + pos] = source[pos];
  }
}

But this doesn't work.
I have to load the code from the sd card to the 30 programmsteps and into the memory, reusing it to set an output on or off like any digital time clock.
Any help welcome and thanks in advance.

@aarg Oh yes it is an ESP32 I am using like this:
https://www.az-delivery.de/products/esp32-developmentboard

1 Like

@anon92864395 @aarg I have made some changes and I think it's working like it has to be now, so I put the whole sketch below, can you take a look at it again? Thanks in advance.

// Code for Arduino environment
// Using ESP32 Dev Module ESP32 WROOM 32 from AZ delivery https://www.az-delivery.de/en
/* Include libraries */
#include <iostream>
#include <string>
#include <FS.h>   // File system library
#include <SPI.h>  // Esp8266/Esp32 core library for Arduino environment
#include <SD.h>   // SD card library
#include "time.h"  /*C:\Users\User\AppData\Local\Arduino..\packages\ esp32\hardware\esp32\2.0.9\tools\ sdk\esp32\include\newlib\platform_include\*/
#include <WiFi.h>  // This library allows an Arduino board to connect to the internet
#include <Wire.h>  // Wiring library
/*
 * Connect the µSD card module to the following pins:
 *
 * µSD Card module    | ESP32
 *
 * CS                   SD D0/GPIO2
 * MOSI                 MOSI/GPIO23
 * VSS/GND              GND
 * VDD/VCC              5V
 * CLK/SCK              SCK/GPIO18
 * MISO                 MISO/GPIO19
 *
 * TFT SPI             | Resistor      | ESP32
 *
 * VCC                   -               5V
 * GND                   -               GND
 * CS                    1KΩ             SS/GPIO5
 * RESET/RST             1KΩ             GPIO22 OR 3.3V
 * D/C                   1KΩ             GPIO4
 * SDI/MOSI              1KΩ             MOSI/GPIO23
 * SCK                   1KΩ             SCK/GPIO18
 * LED                   -               3.3V or GPIO15 (if used to save screen)
 * SDO/MISO              1KΩ             MISO/GPIO19
 *
 * SD CARD (if used)   | ESP32
 * 9 D2                  -
 * 1 D3                  GPIO2
 * 2 CMD                 MOSI/GPIO23
 * 3 VSS                 GND
 * 4 VDD                 3.3V
 * 5 CLK                 SCK/GPIO18
 * 6 VSS                 GND
 * 7 DO                  MISO/GPIO19
 * 8 D1                  -
 *
 */
/*
Connect following pins from the DHT to the ESP32
*   DHT      Resistor           ESP32
*    1          -               3.3V
*    2         10KΩ             3.3V
*    2          -               12
*    3          -               -
*    4          -               GND
*/

// Disconnect GPIO22 and wire the tft pin RESET to 3.3V
// BMP 280 connection           ESP32
/* VCC                          3.3V
 *  GND                          GND
 *  SCL                          21
 *  SDA                          22
 */

/*Change the data with your internet settings*/
// Identification and password needed to connect to your router
const char *ssid = "your ssid";
const char *password = "password";

const char *ntpServerName = "pool.ntp.org";
const long gmtOffset_sec = 3600;
const int daylightOffset_sec = 3600;
struct tm timeinfo;
// The data will be stored as follow: Mo,Tu,We,Th,Fr,Sa,Su,H,H,M,M,O;7 Days, Time & O stands for output
char prog0[13] = { "111000009001" };  // For example set the output on monday, tuesday, wednesday high at 09:00, the last digit 1 = pinMode high
char prog1[13] = { "111111109010" };  // Every day we set the output off at 09:01, the last digit = 0, so pinMode low
char prog2[13] = { "100000018101" };  // Monday set output on at 18:10
char prog3[13] = { "100000018150" };  // Monday set output off at 18:15
char *myString = "111000115001";
File myFile;
char fileName[] = "/programm.txt";
const int chipSelect = 2;
char charRead;
char inputString[13];  //string to hold read string
int c50 = 0;
void setup() {
  Serial.begin(115200);
  pinMode(0, OUTPUT);
  pinMode(13, OUTPUT);
  pinMode(14, OUTPUT);
  pinMode(24, OUTPUT);
  pinMode(25, OUTPUT);
  // connect to WiFi
  Serial.printf("Connecting to %s ", ssid);
  WiFi.begin(ssid, password);
  int i = 0;
  while (i < 15 && WiFi.status() != WL_CONNECTED) {
    i++;
    Serial.print(".");
    if (i == 15) {
      // disconnect WiFi as it's no longer needed
      WiFi.disconnect(true);
      WiFi.mode(WIFI_OFF);
      Serial.println("WIFI disconnected");
      break;
    }
    delay(500);
  }
  if (WiFi.status() == WL_CONNECTED) {
    Serial.println(" CONNECTED");
  }
  // init and get the time
  configTime(gmtOffset_sec, daylightOffset_sec, ntpServerName);

  Serial.println("Initializing System");
  /* Setup the sd-card and test the type, send the information to the serial monitor */
  if (!SD.begin(2)) {  // Select chip, use SD_CS GPIO 2
    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");
  }

  uint64_t cardSize = SD.cardSize() / (1024 * 1024);
  Serial.printf("SD Card Size: %lluMB\n", cardSize);
  Serial.println("initialisation done.");
  writeToFile();
  delay(2000);
  readFromFile();
  printFile();
}

void loop() {
  loadProgramm();
  delay(20000);
}

void writeToFile() {
  myFile = SD.open("/programm.txt", FILE_WRITE);
  if (myFile)  // it opened OK
  {
    myFile.println(prog0);
    myFile.println(prog1);
    myFile.println(prog2);
    myFile.println(prog3);
    // close the file:
    myFile.close();
    Serial.println("Done writing to programm.txt");
  } else
    Serial.println("Error opening programm.txt");  // programm.txt does not exist
  // re-open the file for reading:
  myFile = SD.open("/programm.txt");
  if (myFile) {
    Serial.println("Writing to programm.txt");
    myFile.println(prog0);
    myFile.println(prog1);
    myFile.println(prog2);
    myFile.println(prog3);
    Serial.println("programm.txt created");
    // close the file:
    myFile.close();
  } else {
    // if the file didn't open, print an error:
    Serial.println("error opening the text file");
  }
}
void readFromFile() {
  int d = 0;
  byte i = 0;  //counter
  //now read it back and show on Serial monitor
  // Check to see if the file exists:
  if (!SD.exists("/programm.txt"))
    Serial.println("programm.txt doesn't exist.");
  Serial.println("Reading from programm.txt:");
  myFile = SD.open("/programm.txt");

  while (myFile.available()) {
    char inputChar = myFile.read();  // Gets one byte from serial buffer
    if (inputChar == '\n')           //end of line
    {
      inputString[i] = 0;  //terminate the string correctly
      Serial.println(inputString);
      if (d == 0) {
        substituteChars(inputString, prog0, 0, 11);
      }
      if (d == 1) {
        substituteChars(inputString, prog1, 0, 11);
      }
      if (d == 2) {
        substituteChars(inputString, prog2, 0, 11);
      }
      if (d == 3) {
        substituteChars(inputString, prog3, 0, 11);
      }
      d++;
      i = 0;
    } else {
      inputString[i] = inputChar;  // Store it
      i++;
    }
    if (i > sizeof(inputString)) {
      Serial.println("Incoming string longer than array allows");
      Serial.println(sizeof(inputString));
      while (1)
        ;
    }
  }
  // close the file:
  myFile.close();
  Serial.println("Done reading file");
}
void printFile() {
  Serial.print("programma 0 = ");
  Serial.println(prog0);
  Serial.print("programma 1 = ");
  Serial.println(prog1);
  Serial.print("programma 2 = ");
  Serial.println(prog2);
  Serial.print("programma 3 = ");
  Serial.println(prog3);
}
void substituteChars(char source[], char destination[], byte startAt, byte length) {
  for (int pos = 0; pos < length; pos++) {
    destination[startAt + pos] = source[pos];
  }
}
// So far everything goes fine
void loadProgramm() {
  struct tm timeInfo;
  if (!getLocalTime(&timeInfo)) {
    Serial.println("Failed to obtain time");
    return;
  }
  bool onOff = false;
  bool daySet = false;
  String setHour;
  String setMin;
  char setOn = prog0[11];
  int hourNow = timeInfo.tm_hour;
  int minNow = timeInfo.tm_min;
  int b = timeInfo.tm_wday;
  int c = b - 1;
  if (c == -1) {
    c = 6;
    Serial.println("Sunday");
  }
  int IO = 0;
  //std::string prog5 = myString;
  int d = 0;
  do {
    switch (d) {
      case 0:
        myString = prog0;
        IO = 0;
        break;
      case 1:
        myString = prog1;
        IO = 0;
        break;
      case 2:
        myString = prog2;
        IO = 0;
        break;
      case 3:
        myString = prog3;
        IO = 0;
        break;
    }
    setOn = myString[11];
    if (String(setOn) == "1") {
      onOff = true;
    } else {
      onOff = false;
    }
    daySet = false;
    if (String(myString[c]) == "1") {
      daySet = true;
    }
    if (minNow < 10) {
      setMin = String(myString[10]);
    } else {
      setMin = String(myString[9]);
      setMin += String(myString[10]);
    }
    if (hourNow < 10) {
      setHour = String(myString[8]);
    } else {
      setHour = String(myString[7]);
      setHour += String(myString[8]);
    }
    checkProgram(myString, c, daySet, hourNow, minNow, setHour, setMin, onOff, IO);
    d++;
  } while (d < 4);
  Serial.println("Done checking file");
}
void checkProgram(const std::string &prog, int wDay, bool daySet, int hourNow, int minNow, String setHour, String setMin, bool onOff, int progNum) {
  /* Used outputs
  pinMode(0, OUTPUT);
  pinMode(13, OUTPUT);
  pinMode(14, OUTPUT);
  pinMode(24, OUTPUT);
  pinMode(25, OUTPUT);
  */
  
  if (prog != "000000000000" && prog != "1000000000") {
    //Program at I/O1 != empty
    if (prog[wDay] == '1' && daySet && String(hourNow) == setHour && String(minNow) == setMin) {
      if (onOff) {
        //The high output...
        std::cout << "Output " << prog << " on\n";
        std::cout << "Output " << progNum << " set high\n";
        if (progNum == 1) {
          setOutput1();
        }
      } else {
        //The low output...
        std::cout << "Output " << prog << " off\n";
        std::cout << "Output " << progNum << " set low\n";
        if (progNum == 0) {
          clearOutput1();
        }
      }
    } else {
      // output ! empty but data changing from time or day now
      std::cout << "Output " << prog << " skipped\n";
    }
  } else {
    std::cout << "Data not set yet\n";
  }
}
void setOutput1() {
  // eg: set output 1
  pinMode(0, HIGH);
}
void clearOutput1() {
  // eg: clear output 1
  pinMode(0, LOW);
}

Everyone can load it into an ESP32 Dev kit and change it to his own needs.
Have fun!

Thank you, for posting the corrected sketch. I was thinking of setting up a similar project for my students, and this might be very very helpful. I'm glad that not only that you were able to resolve your issue, but also just didn't leave your post open for others to attempt to solve what's now a non-issue. Kuddos

1 Like

@hornesnfront You´re welcome. I´m working on a project actualy to automate my greenhouse. I used to do this with some Velleman kits before http://www.orchidsinvivo.be/projects.htm , but these are working today only with a computer and my new computer was only 8 months old when he refused to work due to the humidity of course. So I bought an AZ delivery smart home solution https://www.az-delivery.de/products/az-touch-wandgehauseset-mit-touchscreen-fur-esp8266-und-esp32?_pos=2&_sid=981e51f05&_ss=r and I thought it would be sold with an example code, but no way , there is nothing written when you buy this. I´m used to write in C#, CSS, HTML, Javascript even programming pics from Microchip or before in Flash and ActionScript, but Arduino is different and more complex because it´s depending on C++. When I´m done with setting up the screen and pages like you see in the image when you buy this thing, I´ll post it, but I´ll need a few more days to finish the work. I have already more than 2000 lines of coding and now I´m dealing with the problem I´ll have to use a master ESP32 and a slave, because I´m running out of I/O´s. So wish me luck, but I think I can deal it :smiley:. With kind regards, Rudy.

@hornesnfront if you are thinking of something like this to work on with your students, I´m working with Arduino UNO, ESP8266MOD 12F and ESP8266MOD 12F mini D1 and of course the ESP32 WROOM Dev kit C to see what´s the best for me, I´m using 2.4" and 2.8" tft ili9341 screen. I´m using in my project DHT22 and bmp280 sensor already but later I like to add the BH1750 and a watersensor, and other stuff of course. You can also send me an email [mod edit: email address removed, please send this person a PM if you wish to contact them privately]

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