How to run infinite loop while counter is on specific number

Hi,
I have a project with Arduino, an oled screen, and a button.
I have 3 voids with text/images/gigs that work on screen.

want to create a program that shows:
while counter is on 1: void1 in infinite loop
while counter is on 2: void2 in infinite loop
while counter is on 3: void3 in infine loop
counter is incrementing on button push
I know how to setup 99% of code but I don't know how to exit an infinite loop (how to increment state of button while in infinite loop and exit it)

break, usually.

(They're called "functions" not "voids")

Let’s have a look at your functions.

What are gigs in this context?

a7

void xxx1() {
  display.clearDisplay(); //for Clearing the display
  display.setTextSize(4);
  display.setTextColor(WHITE);
  display.setCursor(20,20);
  display.print("xxx1");
  display.display();
  delay(3000);
}
void xxx2() {
  display.clearDisplay(); //for Clearing the display
  display.setTextSize(4);
  display.setTextColor(WHITE);
  display.setCursor(20,20);
  display.print("xxx2");
  display.display();
  delay(3000);
}

void slika() {
//this code shows gif on oled screen
}

void state1(){
  xxx1();
  slika();
  xxx2();
  slika();
  }

void loop() {
  int a=digitalRead(3);
  if(a==1){
    count=1+count;
    Serial.println(count);
    delay(500);}

//this is pseudocode of what i whant:
    *do: state1 in infinite loop*
*        while:  state is **1***

}

Unfortunately, a break isn't going to get you out of a delay.

Hello dpustahija
Code a array of function pointers.
Have a nice day and enjoy coding in C++.

Read here. It seems like the trick in #4 would be within your abilities.

HTH

a7

I don't see how this answers my question of changing var value when the button is pressed inside of an infinite loop and exiting loop

You have not shown us your infinite loop, yet.

Sry.

Number 7. I always get 4 and 7 confused.

a4

I need a solution for the infinite loop. i don't have it.

this is a list of 15 images converted to 15 Bitmap HEX Codes. this has to be in loop to show gif from images and have small delays between them.

void slika() {
//this code shows gif on oled screen
}

i have 3 functions(3 gifs) that i what when i press button once hows gif1, press again shows gif2 ...

Post that code. The solution I pointed you at correctly last time I hope, would be a quick and dirty solution in this case I have no doubt.

a7

Your loop() function is already inside an infinite loop so maybe this sort of thing is what you are looking for:

void loop()
{
  if (digitalRead(3) == HIGH)
  {
    count += 1;
    Serial.println(count);
  }

  switch (count)
  {
  case 1: state1(); break;
  case 2: state2(); break;
  case 3: state3(); break;

  default: count = 0; break;
  }
}

Note: "count += 1;" is a shortcut for "count = count + 1;"

Note: The switch/case statement is a shortcut for:

  if (count == 1)
    state1();
  else if (count == 2)
   state2();
  else if (count == 3)
    state3();
  else 
    count = 0;

Hello dpustahija
See below a example sketch based on functions pointer. You may tailor this sketch to your needs.

/* BLOCK COMMENT
  ATTENTION: This Sketch contains elements of C++.
  https://www.learncpp.com/cpp-tutorial/
  Many thanks to LarryD
  https://europe1.discourse-cdn.com/arduino/original/4X/7/e/0/7e0ee1e51f1df32e30893550c85f0dd33244fb0e.jpeg
  https://forum.arduino.cc/t/how-to-run-infinite-loop-while-counter-is-on-specific-number/958680
*/
#define ProjectName "How to run infinite loop while counter is on specific number"
// HARDWARE AND TIMER SETTINGS
// YOU MAY NEED TO CHANGE THESE CONSTANTS TO YOUR HARDWARE AND NEEDS
constexpr byte ButtonPins[] {A0};      // portPin o---|button|---GND
// VARIABLE DECLARATION AND DEFINITION
unsigned long currentTime;
struct TIMER {              // has the following members
  unsigned long duration;   // memory for interval time
  unsigned long stamp;      // memory for actual time
};
struct BUTTON {             // has the following members
  byte pin;                 // port pin
  bool statusQuo;           // current state
  TIMER scan;               // see timer struct
};
BUTTON selector {ButtonPins[One], false, 20, 0};
// User Tasks 
void (*tasks[])() {
  task0, task1, task2, task3
};
void task0() {
  Serial.println(__func__);
}
void task1() {
  Serial.println(__func__);
}
void task2() {
  Serial.println(__func__);
}
void task3() {
  Serial.println(__func__);
}
// -------------------------------------------------------------------
void setup() {
  Serial.begin(9600);
  Serial.println(F("."));
  Serial.print(F("File   : ")), Serial.println(__FILE__);
  Serial.print(F("Date   : ")), Serial.println(__DATE__);
  Serial.print(F("Project: ")), Serial.println(ProjectName);
  pinMode (LED_BUILTIN, OUTPUT);  // used as heartbeat indicator
  //  https://www.learncpp.com/cpp-tutorial/for-each-loops/
  for (auto Input : ButtonPins) pinMode(Input, INPUT_PULLUP);
}
void loop () {
  currentTime = millis();
  digitalWrite(LED_BUILTIN, (currentTime / 500) % 2);
  static int number = 0;
  tasks[number]();
  if (currentTime - selector.scan.stamp > selector.scan.duration) { // debounce button
    selector.scan.stamp = currentTime;
    int stateNew = ! digitalRead(selector.pin);
    if (selector.statusQuo != stateNew) {
      selector.statusQuo = stateNew;
      if (stateNew)
        ++number %= sizeof(tasks) / sizeof(tasks[0]);
    }
  }
}

Have a nice day and enjoy coding in C++.

You can do this in more than one way, depending on what you actually want to do.

One way is to check if the value of the counter has changed, and if it has then execute the appropriate function. That will run the function once when counter changes, and then the program sits in loop checking the button to see if it has been pressed.

Another way is to check the value of counter in loop, then call the appropriate function depending on the value of counter. Afterwards, the function returns and you continue with loop, checking the button and then executing the same function again until the button is pressed, incrementing counter and going to the next function. This will execute the function repeatedly as long as counter remains the same.

His function, the main one, is expressed as an infinite loop. So running it once will mean running it forever. There will be no "afterwards".

And this

void task3() {
  while (1) {
     Serial.println(__func__);
     delay(400);
  }
}

Is more like what is being looked at, abstract-wise.

@johnwasser clever quick dirty trick hack is the OP's best hope.

a7

Thanks this worked with minimal tweaking :grinning:

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