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

**URL:** <https://forum.arduino.cc/t/how-to-decrement-a-variable-in-every-line-of-a-loop/1265739>\
**Category:** Programming\
**Created:** [May 29, 2024, 6:55pm UTC](https://forum.arduino.cc/t/how-to-decrement-a-variable-in-every-line-of-a-loop/1265739 "2024-05-29T18:55:14Z")\
**Posts on this page:** 6\
**Page:** 2

<div class="post-metadata">

**Author:** ![J-M-L](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/j-m-l/32/766245_2.png) [@J-M-L](https://forum.arduino.cc/u/J-M-L)\
**Post date:** [May 30, 2024, 6:32am UTC](https://forum.arduino.cc/t/how-to-decrement-a-variable-in-every-line-of-a-loop/1265739/31 "2024-05-30T06:32:59Z")

</div>

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

`test/default_time/morning/`

(Type should also be const char \*)

---

<div class="post-metadata">

**Author:** ![StefanL38](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/stefanl38/32/713548_2.png) [@StefanL38](https://forum.arduino.cc/u/StefanL38)\
**Post date:** [May 30, 2024, 8:28am UTC](https://forum.arduino.cc/t/how-to-decrement-a-variable-in-every-line-of-a-loop/1265739/32 "2024-05-30T08:28:28Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![StefanL38](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/stefanl38/32/713548_2.png) [@StefanL38](https://forum.arduino.cc/u/StefanL38)\
**Post date:** [May 30, 2024, 8:59am UTC](https://forum.arduino.cc/t/how-to-decrement-a-variable-in-every-line-of-a-loop/1265739/33 "2024-05-30T08:59:43Z")

</div>

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

```arduino
// 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

> **[Explain-Array-Demo-002 - Wokwi ESP32, STM32, Arduino Simulator](https://wokwi.com/projects/399298310078465025)**
>
> Run IoT and embedded projects in your browser: ESP32, STM32, Arduino, Pi Pico, and more. No installation required!

The serial monitor in the WOKWI-Simulation shows these lines

```arduino
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

```

---

<div class="post-metadata">

**Author:** ![DaveX](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/davex/32/751443_2.png) [@DaveX](https://forum.arduino.cc/u/DaveX)\
**Post date:** [May 30, 2024, 12:50pm UTC](https://forum.arduino.cc/t/how-to-decrement-a-variable-in-every-line-of-a-loop/1265739/34 "2024-05-30T12:50:07Z")

</div>

Those lines are full of patterns.

You should look into [arrays](https://www.arduino.cc/reference/en/language/variables/data-types/array/) and [structures](https://www.tutorialspoint.com/cprogramming/c_structures.htm), the magic combination of arrays of struct, and the [https://docs.arduino.cc/learn/built-in-libraries/eeprom/](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](https://forum.arduino.cc/t/flashing-multiple-leds-at-the-same-time/1065564) 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/](https://docs.arduino.cc/programming/) doesn't mention ['`struct'`](https://en.cppreference.com/w/c/language/struct), and [array - Arduino Reference](https://www.arduino.cc/reference/en/language/variables/data-types/array/) doesn't even hint at, and almost discourages arrays of structs.

---

<div class="post-metadata">

**Author:** ![dougp](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/dougp/32/238908_2.png) [@dougp](https://forum.arduino.cc/u/dougp)\
**Post date:** [May 30, 2024, 1:18pm UTC](https://forum.arduino.cc/t/how-to-decrement-a-variable-in-every-line-of-a-loop/1265739/35 "2024-05-30T13:18:06Z")

</div>

This:

> [@StefanL38](#):
>
> 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?

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.

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [November 26, 2024, 1:19pm UTC](https://forum.arduino.cc/t/how-to-decrement-a-variable-in-every-line-of-a-loop/1265739/36 "2024-11-26T13:19:01Z")

</div>

Closed on November 26, 2024, 1:19pm UTC

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

[Previous page](https://forum.arduino.cc/t/how-to-decrement-a-variable-in-every-line-of-a-loop/1265739.md?page=1)
