Void Loop function to change and stay in next loop

Im trying to get the below to change to a separate Void function but using the buttons and have the loop stay until the button is pressed, idea behind this is so i can change the display to a different loop function.

The below works in as much as i can select the sketch i want, but it seems to block the loop and i have to cycle through the buttons...

Main sketch

#include <Arduino.h>
#define LILYGO_T5_V213
#include "boards.h"

int buttonPin1 = 39;
int buttonPin2 = 38;
int buttonPin3 = 37;



void setup() {
  // put your setup code here, to run once:
Serial.begin(9600);
  pinMode(buttonPin1, INPUT_PULLUP);
  pinMode(buttonPin2, INPUT_PULLUP);
  pinMode(buttonPin3, INPUT_PULLUP);


}



void loop() {
  // put your main code here, to run repeatedly:


  
  while (digitalRead(buttonPin1) == HIGH) {
    
    Screen1();
    
  }

  while (digitalRead(buttonPin2) == HIGH) {
     
    Screen2();
   
  }


  while (digitalRead(buttonPin3) == HIGH) {
    
    Screen3();
    
  }
 

}

Sketch example

void Screen1(){


Serial.println("Screen 1");


  
}

and the question is?

Don't use while but use if.

1 Like

as this

do one step after the other:

loop

  • read a button
  • if a button is pressed, change the value of a variable like byte lastPressedButton
  • show the menu based on the current value of lastPressedButton

i managed to get it to work this way ,, but im certain this is not very clean

#include <Arduino.h>
#define LILYGO_T5_V213
#include "boards.h"

int buttonPin1 = 37;
int buttonPin2 = 38;
int buttonPin3 = 39;



void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  pinMode(buttonPin1, INPUT);
  pinMode(buttonPin2, INPUT);
  pinMode(buttonPin3, INPUT);


}



void loop() {
  // put your main code here, to run repeatedly:

delay(1000);

  if (digitalRead(buttonPin1) == LOW) {

    Screen1();

  }

  if (digitalRead(buttonPin2) == LOW) {

    Screen2();

  }


  if (digitalRead(buttonPin3) == LOW) {

    Screen3();

  }


}

2nd part

void Screen1() {
 if (digitalRead(buttonPin2) == LOW) {

    Screen2();

  }

  if (digitalRead(buttonPin3) == LOW) {

    Screen3();

  }
  delay(1000);
  Serial.println("Screen 1");



  delay(1000);
  Serial.println("Screen 1");
  Screen1();
}

Never call a function from within the same function (directly or indirectly). It's called recusrion and you will run out of memory in no time.

Something like this does not suffer from recursion.

#include <Arduino.h>
#define LILYGO_T5_V213
#include "boards.h"

int buttonPin1 = 37;
int buttonPin2 = 38;
int buttonPin3 = 39;

// indicates the active screen
int activeScreen = 1;

void setup()
{
  Serial.begin(9600);
  pinMode(buttonPin1, INPUT);
  pinMode(buttonPin2, INPUT);
  pinMode(buttonPin3, INPUT);
}

void loop()
{

  delay(1000);

  if (digitalRead(buttonPin1) == LOW)
  {
    activeScreen = 1;
  }

  if (digitalRead(buttonPin2) == LOW)
  {
    activeScreen = 2;
  }

  if (digitalRead(buttonPin3) == LOW)
  {
    activeScreen = 3;
  }

  switch(activeScreen)
  {
    case 1:
      Screen1();
      break;
    case 2:
      Screen2();
      break;
    case 3:
      Screen3();
      break;
  }
}

void Screen1()
{
  Serial.println("screen1");
}

void Screen2()
{
  Serial.println("screen2");
}

void Screen3()
{
  Serial.println("screen3");
}

In ScreenX, do not use blocking code.

You might want to give more details of your project for better advise.

1 Like

Wow thank you for taking the time to code that information.

I'm making a small flying screen for my Paramotor with different screens that I can switch between .

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