How to decrement a variable in every line of a loop..?

May be worth building the path dynamically when needed and save 399x

test/default_time/morning/

(Type should also be const char *)

I have experienced it hundreds of times:

Whenever a description of the complete project is given
new and much better possabilities to achieve a functionality can be suggested.

So @nemo4all

what is the final purposes of all these 200 up to 400 numbers?

The variable names have patterns.
hour/minute on/off
day-month-year

It will be worth a lot to check f your real numbers that are stored into your variables starting with
day1_t1_active,
day1_t2_active,
...
day1_t5_active,
going on with
day2_t
day3_t
....
going on with
day1_t1_hour_on/off
day1_t1_min_on/off

day1_t2_hour_on/off
day1_t2_min_on/off
etc. etc. etc.
if ll these numbers have a pattern.

I am asking for the real numbers that you receive from the database
what will be the most complex example how all these numbers look like?

Most complex in the sense of how many numbers will have a different value?

Is there any chance that these numbers have a pattern?
or
can it happen that each and every number will have a different value ?

What will be "ON" or "OFF" ?

Ideas to keep all the information but to store this information in a smarter more effective way:

hours and minutes can be transformed into minuteOfDay

minuteOfDay = hours * 60 + minutes
example
minuteOfDay = 5 * 60 + 57 // value stored into variable minuteOfDay = 357

If the final purpose is known maybe a smart way for storing / using date month year can be found too

If you are the person that has total control about the firebase-database it might be even possible to modifiy the database-structure to be more effective.

1 Like

Here is a demo-code that shall explain how arrays work

// relates to this Aarduino-Forum-thread https://forum.arduino.cc/t/how-to-decrement-a-variable-in-every-line-of-a-loop/1265739/32
// this is the WOKWI-Link: https://wokwi.com/projects/399298310078465025

// "byte" is the datatype of the array
// "myPhoneDigits" is the name of the array 
// the "4" defines how many elements the array shall have
byte myPhoneDigits[4]; // declaring an array of byte with 4 elements named "myPhoneDigits"

void setup() {
  // make sure to adjust baudrate of the 
  //Arduino-IDE serial monitor to 115200 baud
  Serial.begin(115200); 
  Serial.println( F("Setup-Start") );

  // store values in each array-element
  myPhoneDigits[0] = 4; // element  1 which has index nr 0
  myPhoneDigits[1] = 9; // element  2 which has index nr 1
  myPhoneDigits[2] = 5; // element  3 which has index nr 2
  myPhoneDigits[3] = 1; // element  4 which has index nr 3

  Serial.println();
  Serial.println();
  Serial.println("all phone-digits printed as fixed characters");
  Serial.println("Index-Nr   0123");
  Serial.println("the digits 4951");
  Serial.println();
  Serial.println("now printed in a for-loop");

  Serial.print("the digits ");

  for (int i = 0; i < 4; i++) { // count up 0 to 3
    Serial.print(myPhoneDigits[i]); // print content of each array-element
  }
  
  Serial.println();
  Serial.println();
  Serial.println("now printing with explanation");
  Serial.println("each array-element and its content");

  for (int i = 0; i < 4; i++) { // count up 0 to 3
    Serial.print("array-element myPhoneDigits[");
    Serial.print(i); // print index-number
    Serial.print("] holds value ");
    Serial.println(myPhoneDigits[i]); // print content of each array-element
  }
  
  Serial.println("Scroll back serial monitor to read all printed lines");
}


void loop() {
}

This is the WOKWI-Simulation of this democode

The serial monitor in the WOKWI-Simulation shows these lines

Setup-Start


all phone-digits printed as fixed characters
Index-Nr   0123
the digits 4951

now printed in a for-loop
the digits 4951

now printing with explanation
each array-element and its content
array-element myPhoneDigits[0] holds value 4
array-element myPhoneDigits[1] holds value 9
array-element myPhoneDigits[2] holds value 5
array-element myPhoneDigits[3] holds value 1
Scroll back serial monitor to read all printed lines

Those lines are full of patterns.

You should look into arrays and structures, the magic combination of arrays of struct, and the https://docs.arduino.cc/learn/built-in-libraries/eeprom/ EEPROM.put() and EEPROM.get() commands that can read/write (and update) a whole table worth of information in one line like these:

EEPROM.get(333,dayDates);
dayDates[7].date = 22; 
EEPROM.put(333,dayDates); 

EEPROM.get(93,onOffSchedule);
onOffSchedule[7][1] = 6;
EEPROM.put(93,onOffSchedule);

EEPROM.get(33,activeBits)
Serial.print(activeBits[7][4];

EEPROM.get(1,regularSchedule);
Serial.print(regularSchedule[0].morning_hour_off);
...

As for arrays of structs, the Flashing multiple LEDs at the same time tutorial has several examples of using arrays of structs to extend the BlinkWithoutDelay example to multiple blinking LEDs.

I'm sad that https://docs.arduino.cc/programming/ doesn't mention 'struct', and array - Arduino Reference doesn't even hint at, and almost discourages arrays of structs.

This:

Giving information in dribs and drabs means the answer comes in dribs and drabs. People get frustrated trying to pull out the needed material and having previously unmentioned requirements added along the way.

5 Likes

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