Execute APP by array listed name

Hi all.
I am planning to execute few games one by one failed, how to fix?
Thanks
Adam

char APPsetupA[4][40] = {"a_setup();", "b_setup();", "c_setup();", "d_setup();"};
char APPloopA[4][40] = {{"a_loop();"}, {"b_loop();"}, {"c_loop();"}, {"d_loop();"}};
char *setupB;
char *loopB;

String setupBS;
String loopBS;

///char aaa, bbb, ccc, ddd;
int ta = 1, tb = 2, tc = 3, td = 4;


void setup() {
  // put your setup code here, to run once:
SerialSetting();
}

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

   for (int i = 0; i < 4; i++) {
    setupB = &APPsetupA[i][40];
    loopBS = &APPloopA[i][40];

    Serial.print("APPsetupA["); Serial.print(i); Serial.print("][40] =");
    Serial.println(&APPsetupA[i][40]);
    Serial.print("APPloopA["); Serial.print(i); Serial.print("][40] =");
    Serial.println(&APPloopA[i][40]);

  }
  Serial.print("");
  &setupB;
  loopBS;

  Serial.print("39 setupB = ");
  Serial.println(setupB);
  Serial.print("41 &loopB = ");
  Serial.println(loopBS);
  
  delay(1000);
}


void executeAPP() {
  for (int i = 0; i < 4; i++) {
    &APPsetupA[i][20];
    APPloopA[i][20];

    Serial.print("APPsetupA["); Serial.print(i); Serial.print("][20] =");
    Serial.println(APPsetupA[i][20]);
    Serial.print("APPloopA["); Serial.print(i); Serial.print("][20] =");
    Serial.println(APPloopA[i][20]);

  }
}

void SerialSetting()
{
  Serial.begin(115200);
  //  while (!Serial) ; // Needed for Leonardo only
  Serial.print("File   : "), Serial.println(__FILE__);
  const char compile_date[] = __DATE__ " " __TIME__;
  Serial.print("Compile timestamp: ");
  Serial.println(compile_date);

}


void a_setup(){
  ta = 11;
}

void a_loop(){

   Serial.print("aaa = ");
   Serial.print(ta);
}

void b_setup(){
  tb = 22;
}

void b_loop(){

   Serial.print("bbb = ");
   Serial.print(tb);
}

void c_setup(){
  tc = 33;
}

void c_loop(){

   Serial.print("ccc = ");
   Serial.print(tc);
}

void d_setup(){
  td = 44;
}

void d_loop(){

   Serial.print("ddd = ");
   Serial.print(td);
}

output:

APPsetupA[0][40] =b_setup();
APPloopA[0][40] =b_loop();
APPsetupA[1][40] =c_setup();
APPloopA[1][40] =c_loop();
APPsetupA[2][40] =d_setup();
APPloopA[2][40] =d_loop();
APPsetupA[3][40] =⸮⸮⸮?
APPloopA[3][40] =a_setup();
39 setupB = ⸮⸮⸮?
41 &loopB = a_setup();

questions:

  1. why APPsetupA[0][40] =b_setup(); not APPsetupA[0][40] =a_setup(); ?
  2. what this is: APPsetupA[3][40] =⸮⸮⸮? 3 small square?
  3. why the a_setup(); didn't carry out as expected?

Sry, this caught my eye.

a7

2 Likes

That sketch... I'm sorry, but I don't even know where to begin.

I'm going to make a SWAG and say that maybe what's below was the kind of thing you were trying for.

void a_setup();
void b_setup();
void c_setup();
void d_setup();

void (*setups[4])(void) = {
  a_setup,
  b_setup,
  c_setup,
  d_setup
};

void a_loop();
void b_loop();
void c_loop();
void d_loop();

void (*loops[4])(void) = {
  a_loop,
  b_loop,
  c_loop,
  d_loop
};

///char aaa, bbb, ccc, ddd;
int ta = 1, tb = 2, tc = 3, td = 4;


void setup() {
  // put your setup code here, to run once:
  SerialSetting();
  for (int i = 0; i < 4; i++) {
    (*setups[i])();
  }
}

void loop() {
  // put your main code here, to run repeatedly:
  //executeAPP();
      
  for (int i = 0; i < 4; i++) {
    (*loops[i])();
  }
  delay(1000);
}


void executeAPP() {
  for (int i = 0; i < 4; i++) {
     (*setups[i])();
     (*loops[i])();
  }
}

void SerialSetting()
{
  Serial.begin(115200);
  while (!Serial && millis() < 5000);
  Serial.print("File   : ");
  Serial.println(__FILE__);
  const char compile_date[] = __DATE__ " " __TIME__;
  Serial.print("Compile timestamp: ");
  Serial.println(compile_date);

}


void a_setup(){
  ta = 11;
}

void a_loop(){

   Serial.print("aaa = ");
   Serial.println(ta);
}

void b_setup(){
  tb = 22;
}

void b_loop(){

   Serial.print("bbb = ");
   Serial.println(tb);
}

void c_setup(){
  tc = 33;
}

void c_loop(){

   Serial.print("ccc = ");
   Serial.println(tc);
}

void d_setup(){
  td = 44;
}

void d_loop(){

   Serial.print("ddd = ");
   Serial.println(td);
}

Output

File   : /home/me/Documents/sketchbook/Uno_R4_Minima/test/test.ino
Compile timestamp: Mar 19 2024 19:59:32
aaa = 11
bbb = 22
ccc = 33
ddd = 44
aaa = 11
bbb = 22
ccc = 33
ddd = 44
1 Like

Thank you van_der_decken.

yours got the results what I were looking for.

I put your code into sleep mode that works well.
Question is seems that the sleep mode just execute simple action? how about if I have buttons selection like below, never got good result, how?

//......................................................Rui_ESP32_Timer_Wake_Up_from_Deep_Sleep_gd
#define uS_TO_S_FACTOR 1000000  /* Conversion factor for micro seconds to seconds */
#define TIME_TO_SLEEP  8        /* Time ESP32 will go to sleep (in seconds) */

RTC_DATA_ATTR int bootCount = 0;
//......................................................Rui_ESP32_Timer_Wake_Up_from_Deep_Sleep_gd

#define buttonSelectA 13
int buttonSelectAMarker = 0;

#define buttonSelectB 33
int buttonSelectBMarker = 0;

void a_setup();
void b_setup();
void c_setup();
void d_setup();

void (*setups[4])(void) = {
  a_setup,
  b_setup,
  c_setup,
  d_setup
};

void a_loop();
void b_loop();
void c_loop();
void d_loop();

void (*loops[4])(void) = {
  a_loop,
  b_loop,
  c_loop,
  d_loop
};

///char aaa, bbb, ccc, ddd;
int ta = 1, tb = 2, tc = 3, td = 4;


void setup() {
  // put your setup code here, to run once:
  SerialSetting();
  for (int i = 0; i < 4; i++) {
    (*setups[i])();
  }

  //......................................................Rui_ESP32_Timer_Wake_Up_from_Deep_Sleep_gd

  for (int i = 0; i < 4; i++) {
    (*loops[i])();
  }

  //Increment boot number and print it every reboot
  ++bootCount;
  Serial.println("Boot number: " + String(bootCount));

  /*
    First we configure the wake up source
    We set our ESP32 to wake up every 5 seconds
  */
  esp_sleep_enable_timer_wakeup(TIME_TO_SLEEP * uS_TO_S_FACTOR);
  Serial.println("Setup ESP32 to sleep for every " + String(TIME_TO_SLEEP) +
                 " Seconds");

  /*
    Now that we have setup a wake cause and if needed setup the
    peripherals state in deep sleep, we can now start going to
    deep sleep.
    In the case that no wake up sources were provided but deep
    sleep was started, it will sleep forever unless hardware
    reset occurs.
  */
  Serial.println("Going to sleep now");
  delay(1000);
  Serial.flush();
  esp_deep_sleep_start();
  Serial.println("This will never be printed");

  //......................................................Rui_ESP32_Timer_Wake_Up_from_Deep_Sleep_gd

}

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

  //  for (int i = 0; i < 4; i++) {
  //    (*loops[i])();
  //  }
  //  delay(1000);
}


void executeAPP() {
  for (int i = 0; i < 4; i++) {
    (*setups[i])();
    (*loops[i])();
  }
}

/*
  void SerialSetting()
  {
  Serial.begin(115200);
  while (!Serial && millis() < 5000);
  Serial.print("File   : ");
  Serial.println(__FILE__);
  const char compile_date[] = __DATE__ " " __TIME__;
  Serial.print("Compile timestamp: ");
  Serial.println(compile_date);

  }  */


void a_setup() {
  ta = 11;
}


void a_loop() {
  if (!digitalRead(buttonSelectA)) {
    buttonSelectAMarker = 1;
  }

  if (buttonSelectAMarker == 1) {
    ta += 1;
    Serial.print("aaa = ");
    Serial.println(ta);
    buttonSelectBMarker = 0;
  }

  if (!digitalRead(buttonSelectB)) {
    buttonSelectBMarker = 1;
  }

  if (buttonSelectBMarker == 1) {
    ta += 9;
    Serial.print("aaa = ");
    Serial.println(ta);

    buttonSelectBMarker = 0;
  }
}  

/*
void a_loop() {

  Serial.print("aaa = ");
  Serial.println(ta);
}  */

void b_setup() {
  tb = 22;
}

void b_loop() {

  Serial.print("bbb = ");
  Serial.println(tb);
}

void c_setup() {
  tc = 33;
}

void c_loop() {

  Serial.print("ccc = ");
  Serial.println(tc);
}


void SerialSetting()
{
  Serial.begin(115200);
  //  while (!Serial) ; // Needed for Leonardo only
  Serial.print("File   : "), Serial.println(__FILE__);
  const char compile_date[] = __DATE__ " " __TIME__;
  Serial.print("Compile timestamp: ");
  Serial.println(compile_date);

}

//..............

void d_setup() {
  td = 44;
}

void d_loop() {

  Serial.print("ddd = ");
  Serial.println(td);
}

buttonSelectA pressed

File   : C:\Users\HUA.DELLV-PC\Documents\Arduino\executeAPPtest_N3_ls\executeAPPtest_N3_ls.ino
Compile timestamp: Mar 20 2024 10:01:54
aaa = 12
aaa = 21
bbb = 22
ccc = 33
ddd = 44
Boot number: 4

buttonSelectB pressed or no button pressed:

File   : C:\Users\HUA.DELLV-PC\Documents\Arduino\executeAPPtest_N3_ls\executeAPPtest_N3_ls.ino
Compile timestamp: Mar 20 2024 10:01:54
aaa = 20
bbb = 22
ccc = 33
ddd = 44
Boot number: 5

code does what's coded.
learn function pointers

1 Like

add
SERIAL PRINTING
to finally understand how C++ code works

1 Like

Thank you StefanL38.
add 'else if' and found that one of the button lost power.

just another qbwc

buttons can't loose power.
buttons might be not wired to whatever but they can't loose power

have you ever heard of constructivism?

1 Like

well, sry used inappropriate word.
broken one button's connection when wiring another button.

one thing is the button work only when pressed long time enough over the sleep period, is normal?

hahaha
this is maximum your "the next five minutes"-one thing.

Within 30 minutes you will increase your # of posts.

sleeping means sleeping.
measure booting time with a second microcontroller or an oscilloscope.

re-read deep-sleep tutorials about external wakeup

???????

Sure. Short postings are riddles.

use a second microcontroller or an oscilloscope to record voltage of your button-input pin over pressing time

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