I don't know how to stop a loop with a input from a IrReceiver

So I am pretty new to Arduino and this is my first post on this forum.
I am using a IrReceiver and its remote to display numbers on a 7 segments display.

if (decodedValue == "FORWARD") {
        contatore = contatore++;
       if (contatore > 9) {
        contatore = 0;
      }
      if(contatore == 0) {
        clearDisplay();
        display0();
        delay(200);
      } else if(contatore == 1) {
        clearDisplay();
        display1();
        delay(200);
      } else if(contatore == 2) {
        clearDisplay();
        display2();
        delay(200);
      } else if(contatore == 3) {
        clearDisplay();
        display3();
        delay(200);
      } else if(contatore == 4) {
        clearDisplay();
        display4();
        delay(200);
      } else if(contatore == 5) {
        clearDisplay();
        display5();
        delay(200);
      } else if(contatore == 6) {
        clearDisplay();
        display6();
        delay(200);
      } else if(contatore == 7) {
        clearDisplay();
        display7();
        delay(200);
      } else if(contatore == 8) {
        clearDisplay();
        display8();
        delay(200);
      } else if(contatore == 9) {
        clearDisplay();
        display9();
        delay(200);
      }
    }
    if (decodedValue == "BACKWARD") {
      contatore = contatore - 1;
       if (contatore < 0) {
        contatore = 9;
      }
      if(contatore == 0) {
        clearDisplay();
        display0();
        delay(200);
      } else if(contatore == 1) {
        clearDisplay();
        display1();
        delay(200);
      } else if(contatore == 2) {
        clearDisplay();
        display2();
        delay(200);
      } else if(contatore == 3) {
        clearDisplay();
        display3();
        delay(200);
      } else if(contatore == 4) {
        clearDisplay();
        display4();
        delay(200);
      } else if(contatore == 5) {
        clearDisplay();
        display5();
        delay(200);
      } else if(contatore == 6) {
        clearDisplay();
        display6();
        delay(200);
      } else if(contatore == 7) {
        clearDisplay();
        display7();
        delay(200);
      } else if(contatore == 8) {
        clearDisplay();
        display8();
        delay(200);
      } else if(contatore == 9) {
        clearDisplay();
        display9();
        delay(200);
      }
    }

This code just make me press FORWARD to add 1 to the display, and BACKWARD make me do -1
I have 2 main problem:

  1. I want to numbers just go on when I press "FORWARD" but I don't really know how to do It, I tried using a for loop with for(int i = 0; i == 0) and at the end of the loop put a IrRemote,resume function, to get another signal to stop the counting, but it just stop my loop and after that I can't do anything.

  2. I also want the same code that I wrote before but instead of "FORWARD" and "BACKWARD" I want to put "+" and "-" but when I do that it just doesn't work anymore.

I would like even any help to make my code simplier and more pratical.

Thank you.

NOTE: Of course this isn't the entire code but the rest work pretty well, but just in case someone want to check it I will put it right down below.

#include <IRremote.h>

int a = 2;
int b = 3;
int c = 4;
int d = 5;
int e = 6;
int f = 7;
int g = 8;
int IR = 9;
int contatore = 0;
void display0() {
  digitalWrite(a, HIGH);
  digitalWrite(b, HIGH);
  digitalWrite(c, HIGH);
  digitalWrite(d, HIGH);
  digitalWrite(e, HIGH);
  digitalWrite(f, HIGH);
}

void display1() {
  digitalWrite(b, HIGH);
  digitalWrite(c, HIGH);
}

void display2() {
  digitalWrite(a, HIGH);
  digitalWrite(b, HIGH);
  digitalWrite(d, HIGH);
  digitalWrite(e, HIGH);
  digitalWrite(g, HIGH);
}

void display3() {
  digitalWrite(a, HIGH);
  digitalWrite(b, HIGH);
  digitalWrite(c, HIGH);
  digitalWrite(d, HIGH);
  digitalWrite(g, HIGH);
}

void display4() {
  digitalWrite(b, HIGH);
  digitalWrite(c, HIGH);
  digitalWrite(f, HIGH);
  digitalWrite(g, HIGH);
}

void display5() {
  digitalWrite(a, HIGH);
  digitalWrite(c, HIGH);
  digitalWrite(d, HIGH);
  digitalWrite(f, HIGH);
  digitalWrite(g, HIGH);
}

void display6() {
  digitalWrite(a, HIGH);
  digitalWrite(c, HIGH);
  digitalWrite(d, HIGH);
  digitalWrite(e, HIGH);
  digitalWrite(f, HIGH);
  digitalWrite(g, HIGH);
}

void display7() {
  digitalWrite(a, HIGH);
  digitalWrite(b, HIGH);
  digitalWrite(c, HIGH);
}

void display8() {
  digitalWrite(a, HIGH);
  digitalWrite(b, HIGH);
  digitalWrite(c, HIGH);
  digitalWrite(d, HIGH);
  digitalWrite(e, HIGH);
  digitalWrite(f, HIGH);
  digitalWrite(g, HIGH);
}

void display9() {
  digitalWrite(a, HIGH);
  digitalWrite(b, HIGH);
  digitalWrite(c, HIGH);
  digitalWrite(d, HIGH);
  digitalWrite(f, HIGH);
  digitalWrite(g, HIGH);
}

void clearDisplay() {
  digitalWrite(a, LOW);
  digitalWrite(b, LOW);
  digitalWrite(c, LOW);
  digitalWrite(d, LOW);
  digitalWrite(e, LOW);
  digitalWrite(f, LOW);
  digitalWrite(g, LOW);
}

String decodeKeyValue(long result) {
  switch(result){
    case 0x16:
      return "0";
    case 0xC:
      return "1"; 
    case 0x18:
      return "2"; 
    case 0x5E:
      return "3"; 
    case 0x8:
      return "4"; 
    case 0x1C:
      return "5"; 
    case 0x5A:
      return "6"; 
    case 0x42:
      return "7"; 
    case 0x52:
      return "8"; 
    case 0x4A:
      return "9"; 
    case 0x9:
      return "+"; 
    case 0x15:
      return "-"; 
    case 0x7:
      return "EQ"; 
    case 0xD:
      return "U/SD";
    case 0x19:
      return "CYCLE";         
    case 0x44:
      return "PLAY/PAUSE";   
    case 0x43:
      return "FORWARD";  
    case 0x40:
      return "BACKWARD";   
    case 0x45:
      return "POWER";   
    case 0x47:
      return "MUTE";   
    case 0x46:
      return "MODE";       
    case 0x0:
      return "ERROR";   
    default :
      return "ERROR";
    }
}

void setup() {
  for (int i = 2; i <=8; i++) {
    pinMode(i, OUTPUT);
  }
  Serial.begin(9600);                                     
  IrReceiver.begin(IR, ENABLE_LED_FEEDBACK);  
}

void loop() {
  if (IrReceiver.decode()) {
    String decodedValue = decodeKeyValue(IrReceiver.decodedIRData.command);
    if (decodedValue == "0") {
      clearDisplay();
      display0();
      contatore = 0;
    }
    if (decodedValue == "1") {
      clearDisplay();
      display1();
      contatore = 1;
    }
    if (decodedValue == "2") {
      clearDisplay();
      display2();
      contatore = 2;
    }
    if (decodedValue == "3") {
      clearDisplay();
      display3();
      contatore = 3;
    }
    if (decodedValue == "4") {
      clearDisplay();
      display4();
      contatore = 4;
    }
    if (decodedValue == "5") {
      clearDisplay();
      display5();
      contatore = 5;
    }
    if (decodedValue == "6") {
      clearDisplay();
      display6();
      contatore = 6;
    }
    if (decodedValue == "7") {
      clearDisplay();
      display7();
      contatore = 7;
    }
    if (decodedValue == "8") {
      clearDisplay();
      display8();
      contatore = 8;
    }
    if (decodedValue == "9") {
      clearDisplay();
      display9();
      contatore = 9;
    }
    if (decodedValue == "CYCLE") {
      clearDisplay();
      display0();
      
      delay(1000);

      clearDisplay();
      display1();

      delay(1000);
      
      clearDisplay();
      display2();

      delay(1000);
      
      clearDisplay();
      display3();

      delay(1000);
      
      clearDisplay();
      display4();

      delay(1000);
      
      clearDisplay();
      display5();

      delay(1000);
      
      clearDisplay();
      display6();

      delay(1000);
      
      clearDisplay();
      display7();

      delay(1000);
      
      clearDisplay();
      display8();

      delay(1000);
      
      clearDisplay();
      display9();

      delay(1000);
      
      clearDisplay();
      display0();
      contatore = 0;
    }
    if (decodedValue == "FORWARD") {
        contatore = contatore++;
       if (contatore > 9) {
        contatore = 0;
      }
      if(contatore == 0) {
        clearDisplay();
        display0();
        delay(200);
      } else if(contatore == 1) {
        clearDisplay();
        display1();
        delay(200);
      } else if(contatore == 2) {
        clearDisplay();
        display2();
        delay(200);
      } else if(contatore == 3) {
        clearDisplay();
        display3();
        delay(200);
      } else if(contatore == 4) {
        clearDisplay();
        display4();
        delay(200);
      } else if(contatore == 5) {
        clearDisplay();
        display5();
        delay(200);
      } else if(contatore == 6) {
        clearDisplay();
        display6();
        delay(200);
      } else if(contatore == 7) {
        clearDisplay();
        display7();
        delay(200);
      } else if(contatore == 8) {
        clearDisplay();
        display8();
        delay(200);
      } else if(contatore == 9) {
        clearDisplay();
        display9();
        delay(200);
      }
    }
    if (decodedValue == "BACKWARD") {
      contatore = contatore - 1;
       if (contatore < 0) {
        contatore = 9;
      }
      if(contatore == 0) {
        clearDisplay();
        display0();
        delay(200);
      } else if(contatore == 1) {
        clearDisplay();
        display1();
        delay(200);
      } else if(contatore == 2) {
        clearDisplay();
        display2();
        delay(200);
      } else if(contatore == 3) {
        clearDisplay();
        display3();
        delay(200);
      } else if(contatore == 4) {
        clearDisplay();
        display4();
        delay(200);
      } else if(contatore == 5) {
        clearDisplay();
        display5();
        delay(200);
      } else if(contatore == 6) {
        clearDisplay();
        display6();
        delay(200);
      } else if(contatore == 7) {
        clearDisplay();
        display7();
        delay(200);
      } else if(contatore == 8) {
        clearDisplay();
        display8();
        delay(200);
      } else if(contatore == 9) {
        clearDisplay();
        display9();
        delay(200);
      }
    }
    if(decodedValue == "POWER") {
      if(digitalRead(a) == HIGH || digitalRead(b) == HIGH) {
        clearDisplay();
        delay(200);
      } else if(digitalRead(a) == LOW) {
        display0();
        delay(200);
        contatore = 0;
      }
    }


    IrReceiver.resume();
  }
}

where do you check if a decode value is available?

1 Like

It is ALWAYS best to post your entire sketch so people can copy/paste and compile.

You can simplify your code quite a bit by getting rid of the String class and using integers

#include <IRremote.h>

const int a = 2;
const int b = 3;
const int c = 4;
const int d = 5;
const int e = 6;
const int f = 7;
const int g = 8;
const int IR = 9;

int contatore = 0;

void displayDigit(int digit) {
  switch (digit) {
    case 0:
      digitalWrite(a, HIGH);
      digitalWrite(b, HIGH);
      digitalWrite(c, HIGH);
      digitalWrite(d, HIGH);
      digitalWrite(e, HIGH);
      digitalWrite(f, HIGH);
      digitalWrite(g, LOW);
      break;

    case 1:
      digitalWrite(a, LOW);
      digitalWrite(b, HIGH);
      digitalWrite(c, HIGH);
      digitalWrite(d, LOW);
      digitalWrite(e, LOW);
      digitalWrite(f, LOW);
      digitalWrite(g, LOW);
      break;

    case 2:
      digitalWrite(a, HIGH);
      digitalWrite(b, HIGH);
      digitalWrite(c, LOW);
      digitalWrite(d, HIGH);
      digitalWrite(e, HIGH);
      digitalWrite(f, LOW);
      digitalWrite(g, HIGH);
      break;

    case 3:
      digitalWrite(a, HIGH);
      digitalWrite(b, HIGH);
      digitalWrite(c, HIGH);
      digitalWrite(d, HIGH);
      digitalWrite(e, LOW);
      digitalWrite(f, LOW);
      digitalWrite(g, HIGH);
      break;

    case 4:
      digitalWrite(a, LOW);
      digitalWrite(b, HIGH);
      digitalWrite(c, HIGH);
      digitalWrite(d, LOW);
      digitalWrite(e, LOW);
      digitalWrite(f, HIGH);
      digitalWrite(g, HIGH);
      break;

    case 5:
      digitalWrite(a, HIGH);
      digitalWrite(b, LOW);
      digitalWrite(c, HIGH);
      digitalWrite(d, HIGH);
      digitalWrite(e, LOW);
      digitalWrite(f, HIGH);
      digitalWrite(g, HIGH);
      break;

    case 6:
      digitalWrite(a, HIGH);
      digitalWrite(b, LOW);
      digitalWrite(c, HIGH);
      digitalWrite(d, HIGH);
      digitalWrite(e, HIGH);
      digitalWrite(f, HIGH);
      digitalWrite(g, HIGH);
      break;

    case 7:
      digitalWrite(a, HIGH);
      digitalWrite(b, HIGH);
      digitalWrite(c, HIGH);
      digitalWrite(d, LOW);
      digitalWrite(e, LOW);
      digitalWrite(f, LOW);
      digitalWrite(g, LOW);
      break;

    case 8:
      digitalWrite(a, HIGH);
      digitalWrite(b, HIGH);
      digitalWrite(c, HIGH);
      digitalWrite(d, HIGH);
      digitalWrite(e, HIGH);
      digitalWrite(f, HIGH);
      digitalWrite(g, HIGH);
      break;

    case 9:
      digitalWrite(a, HIGH);
      digitalWrite(b, HIGH);
      digitalWrite(c, HIGH);
      digitalWrite(d, HIGH);
      digitalWrite(e, LOW);
      digitalWrite(f, HIGH);
      digitalWrite(g, HIGH);
      break;
  }
}

void clearDisplay() {
  digitalWrite(a, LOW);
  digitalWrite(b, LOW);
  digitalWrite(c, LOW);
  digitalWrite(d, LOW);
  digitalWrite(e, LOW);
  digitalWrite(f, LOW);
  digitalWrite(g, LOW);
}

// start above 9 since we have digits 0..9
enum { PLUS = 10, MINUS, EQ, U_SD, CYCLE, PLAY, FORWARD, BACKWARD, POWER, MUTE, MODE, ERROR };


int decodeKeyValue(long result) {
  switch (result) {
    case 0x16:  return 0;
    case 0xC:   return 1;
    case 0x18:  return 2;
    case 0x5E:  return 3;
    case 0x8:   return 4;
    case 0x1C:  return 5;
    case 0x5A:  return 6;
    case 0x42:  return 7;
    case 0x52:  return 8;
    case 0x4A:  return 9;
    case 0x9:   return PLUS;
    case 0x15:  return MINUS;
    case 0x7:   return EQ;
    case 0xD:   return U_SD;
    case 0x19:  return CYCLE;
    case 0x44:  return PLAY;
    case 0x43:  return FORWARD;
    case 0x40:  return BACKWARD;
    case 0x45:  return POWER;
    case 0x47:  return MUTE;
    case 0x46:  return MODE;
    default :
      Serial.print("Unknown code received: 0x");
      Serial.println(result, HEX );
      return ERROR;
  }
}

void setup() {
  for (int i = 2; i <= 8; i++) {
    pinMode(i, OUTPUT);
  }
  Serial.begin(9600);
  IrReceiver.begin(IR, ENABLE_LED_FEEDBACK);
}

void loop() {
  if (IrReceiver.decode()) {
    int decodedValue = decodeKeyValue(IrReceiver.decodedIRData.command);
    if (decodedValue >= 0 && decodedValue <= 9) {
      contatore = decodedValue;
      displayDigit(contatore);
    }
    else if (decodedValue == CYCLE) {
      for ( contatore = 0; contatore < 10; contatore++ ) {
        displayDigit(contatore);
        delay(1000);
      }
      contatore = 0;
      displayDigit(contatore);
    }
    else if (decodedValue == FORWARD) {
      contatore++;
      if (contatore > 9) {
        contatore = 0;
      }
      displayDigit(contatore);
      delay(200);
    }
    else if (decodedValue == BACKWARD) {
      contatore--;
      if (contatore < 0) {
        contatore = 9;
      }
      displayDigit(contatore);
      delay(200);
    }
    else if (decodedValue == POWER) {
      if (digitalRead(a) == HIGH || digitalRead(b) == HIGH) {
        clearDisplay();
        delay(200);
      } else if (digitalRead(a) == LOW) {
        contatore = 0;
        displayDigit(contatore);
        delay(200);
      }
    }
    IrReceiver.resume();
  }
}

I don't understand what you mean by this, it is confusing. It sounds similar to your "CYCLE" function. Is that what you want it to do?

I am not 100% sure what this line of code will do, but I am 99% sure it will do nothing:

        contatore = contatore++;

If you want me to explain why it does nothing, I can try to explain, but it might be confusing for you. So I recommend you change that line to

        contatore++;

or

        contatore += 1;

or

        contatore = contatore + 1;

it is undefined behavior so the compiler is free to do whatever it wants.

It is probably too complex for @cloddo to understand, and so slightly off-topic, but can you explain why?

In my mind it should work like this:

  1. read the current value of contatore
  2. increment the value of contatore
  3. assign the value read in step 1 to contatore (over-writing the new value set in step 2)

And so contatore is not changed.

So are you going to say that the 1, 2, 3 sequence isn't guaranteed?

First of all I am sorry if I didn't explained myself properly, I am Italian so it's quite hard to me speaking in english.
What I wanted is basically do what the "FORWARD" button does but continuisly, as you said similar to my "CYCLE" function, but I wanted it to go on even after 0, so it goes 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 and so on until I press any other button on the remote to stop it.

I don't really understood why contatore = contatore++ shouldn't work but apperently it does, but I appreciate the help I will try to figure it out and for now I'll just change it because I want my code to be the more accurate possible

ā€œ++iā€ is known as the pre-increment operator, which increments the value of 'i' immediately and returns the incremented value. On the other hand, ā€œi++ā€ is known as the post-increment operator, which increments the value of 'i' but returns the original value that 'i' held before being incremented.

It is two writes to the same variable within 1 sequence point (I think I am using the proper term). One write is the assignment and one write is the result of the post increment operator and since that is undefined behavior, the compiler is free to do whatever it wants and change what it actually does in the future without it being a bug.

Exactly. The compiler could optimize that sequence differently and possibly in a way that breaks. Apparently, it is currently working as you describe but the next version of the compiler may not.

1 Like

I have attempted to do this for you, but I have not tested it. Also I did not understand what the POWER button is supposed to do.

#include <IRremote.h>

const byte segments[] = {2, 3, 4, 5, 6, 7, 8};
const byte IR = 9;
const byte sevenSeg[] = {
  //gfedcba
  0b0111111, //0
  0b0000110, //1
  0b1001011, //2
  0b1001111, //3
  0b1100110, //4
  0b1101101, //5
  0b1101101, //6
  0b0000111, //7
  0b1111111, //8
  0b1101111  //9
};

int contatore = 0;
unsigned long lastUpdateTime;
bool cycleMode;

const byte KEY_0 = 0x16;
const byte KEY_1 = 0xC;
const byte KEY_2 = 0x18;
const byte KEY_3 = 0x5E;
const byte KEY_4 = 0x8;
const byte KEY_5 = 0x1C;
const byte KEY_6 = 0x5A;
const byte KEY_7 = 0x42;
const byte KEY_8 = 0x52;
const byte KEY_9 = 0x4A;
const byte KEY_PLUS = 0x9;
const byte KEY_MINUS = 0x15;
const byte KEY_EQ = 0x7;
const byte KEY_U_SD = 0xD;
const byte KEY_CYCLE = 0x19;
const byte KEY_PLAY_PAUSE = 0x44;
const byte KEY_FORWARD = 0x43;
const byte KEY_BACKWARD = 0x40;
const byte KEY_POWER = 0x45;
const byte KEY_MUTE = 0x47;
const byte KEY_MODE = 0x46;
const byte KEY_ERROR = 0x0;

void display(byte n) {
  for (byte s = 0; s < 7; s++) {
    digitalWrite(segments[s], bitRead(sevenSeg[n], s));
  }
}

void setup() {
  for (byte s = 0; s < 7; s++) {
    pinMode(segments[s], OUTPUT);
  }
  Serial.begin(9600);
  IrReceiver.begin(IR, ENABLE_LED_FEEDBACK);
}

void loop() {
  if (IrReceiver.decode()) {
    cycleMode = false;
    switch (IrReceiver.decodedIRData.command) {
      case KEY_0: contatore = 0; break;
      case KEY_1: contatore = 1; break;
      case KEY_2: contatore = 2; break;
      case KEY_3: contatore = 3; break;
      case KEY_4: contatore = 4; break;
      case KEY_5: contatore = 5; break;
      case KEY_6: contatore = 6; break;
      case KEY_7: contatore = 7; break;
      case KEY_8: contatore = 8; break;
      case KEY_9: contatore = 9; break;
      case KEY_CYCLE:
        cycleMode = true;
        lastUpdateTime = millis();
        break;
      case KEY_FORWARD:
        if (++contatore > 9) contatore = 0;
        break;
      case KEY_BACKWARD:
        if (--contatore < 0) contatore = 9;
        break;
      case KEY_POWER: break; //Not sure what this should do?
    }
    display(contatore);
    IrReceiver.resume();
  }

  if (cycleMode and millis() - lastUpdateTime > 1000) {
    if (++contatore > 9) contatore = 0;
    display(contatore);
    lastUpdateTime += 1000;
  }
}

Let me know if it works or not.

Also please read the code and try to understand it, and ask any questions you like. I want to you understand each change that I made to your original code and why I made that change.

look this over
see function pointer pg 106 of The C Programming Language


const unsigned long ForMsecPeriod = 200;
      unsigned long forMsec;
      unsigned long msec;

void clearDisplay () { Serial.println (__func__); }

void disp_0 () { Serial.println (__func__); }
void disp_1 () { Serial.println (__func__); }
void disp_2 () { Serial.println (__func__); }
void disp_3 () { Serial.println (__func__); }
void disp_4 () { Serial.println (__func__); }
void disp_5 () { Serial.println (__func__); }
void disp_6 () { Serial.println (__func__); }
void disp_7 () { Serial.println (__func__); }
void disp_8 () { Serial.println (__func__); }
void disp_9 () { Serial.println (__func__); }

void (*func []) (void) = {
    disp_0,
    disp_1,
    disp_2,
    disp_3,

    disp_4,
    disp_5,
    disp_6,
    disp_7,

    disp_8,
    disp_9,
};
int cnt;

bool forward;

// -----------------------------------------------------------------------------
void forwardOp ()
{
    if (msec - forMsec < ForMsecPeriod)
        return;
    forMsec = msec;

    clearDisplay ();
    func [cnt] ();  // exec func-ptr at index cnt in func []

    if (10 <= ++cnt)
        cnt = 0;
}

// -----------------------------------------------------------------------------
void loop ()
{
    msec = millis ();

    if (forward)
        forwardOp ();

    if (Serial.available ()) {
        char c = Serial.read ();

        if ('f' == c)
            forward = true;
    }
}

void setup() {
    Serial.begin(115200);
}

Hi @cloddo ,

my suggestion is that you might start learning C and C++ basics first before starting to create something more complex...

Do you know the ballade of the Sorcerer's Apprentice? :wink:

https://en.m.wikipedia.org/wiki/The_Sorcerer%27s_Apprentice

or in Italian language

https://it.m.wikipedia.org/wiki/L%27apprendista_stregone_(ballata)

Without understanding the basics it will be hard for others to give you understandable advice. We can easily solve your problem (provided we understand correctly what you want to achieve) but a sound solution will quite likely look very different to your recent code. Reason is that your sketch is based on code just good enough to demo certain functions but not really usable for more than that.

Once you learned the basics of C and C++ (independent from Arduino!!) you'll develop a different view how to solve the task.

The most important question:

Are you just looking for a single solution or are you interested in learning to master microcontrollers?

1 Like

the issue is how to usethe language, how to do something periodically while still monitoring inputs.

It's not an issue of understanding the language but understanding concepts and structures common to many languages. where does a C/C++ book explain how to use a timer or multi-tasking?

well actually I wanted to lear C watching some code online and just learn from them, but honestly I can't understand anything of some of the answer under my post so I think I'll follow ur suggestion and just learn C and C++ and only after trying to figure out how to properly use arduino.

perhaps you lack of understand could easily be resolved if you indicated which parts of the code you don't understand.

your original post suggested to me that you are more familiar with coding

Improving your knowledge will certainly help to implement the required changes!

Good luck!

I agree that the concept can be explained without addressing a specific programming language. But
if you analyze the original sketch you'll see that the TO lacks basic knowledge that will make it difficult to implement the required changes.

That's why I suggested to improve the basic(!) knowledge first ...

well 2 years ago I started learning python, but then I focused on school and just forget anything I know.
So I'm quite familiar with coding but not with C and honestly I wasn't even that good at coding in python

Python is a nice language but follows a different concept compared to C++.

It's much easier to make use of astonishing functionality in Python with some imports and a few lines of code than in C or C++ where you have to carefully consider data types and variable declarations.

There is an Italian community in this forum where you surely get a lot of support if you feel more comfortable in your native language!

I'm looking forward to your future posts here!

Buona fortuna e in bocca al lupo!