Trying to simplify many if statements into a for loop

Full code: most of it can be ignored. BTW this is for an IR camera trigger, I know most of it works as this is an upgrade from an older revision without an OLED so there are some remenants from the previous ones.

//######################################################General Setup###################################################################################################################
int outIR = 51; // output for IR LED
int outBUZZ = 10; //buzzer/speaker pin
#include "AXE133Y.h" //OLED setup 
#define oledPin 7 //OLED pin
AXE133Y OLED = AXE133Y(oledPin);

int pulseWidth = 0;
int Inputup    = 41;
int Inputdown  = 42;
int Inputleft  = 43;
int Inputright = 44;
int Inputok    = 45;
//int InputSong = 50;//switch for changing buzzer and song
int InputLDR   = 0;
int InputPIR   = 22;






void setup() {
  // put your setup code here, to run once:
  pinMode(outIR, OUTPUT);                   //declares all in/out pins as they should be
  pinMode(outBUZZ, OUTPUT);

  pinMode(Inputup, INPUT);
  pinMode(Inputdown, INPUT);
  pinMode(Inputleft, INPUT);
  pinMode(Inputright, INPUT);
  pinMode(Inputok, INPUT);
  //pinMode(Inputhome, INPUT);//might be added if possible
  //pinMode(InputSong, INPUT);
  pinMode(InputLDR, INPUT);
  pinMode(InputPIR, INPUT);
}
//####################################################Pulse wavelength (square)#########################################################################################################
void SendPulse(int pulseWidth)
{
  //SendPulse generates the modulating signal and pulses the signal on the designated output pin.
  //argument pulseWidth must be in microseconds
  int reps = pulseWidth / 23.6;
  for (int i = 0; i <= reps; i++)
  {
    digitalWrite(outIR, HIGH);
    delayMicroseconds(11); //alter this and v
    digitalWrite(outIR, LOW);
    delayMicroseconds(5); // this to make it output exactly ^15 us v9 us
  }
}
// ##################################################### Send sequence (IR CODE)#########################################################################################################
void SendSequence()
{ //This sends the right IR Code
  for (int i = 0; i < 2; i++)
  {
    SendPulse(2000);        //pulse for 2.0 ms v
    delay(27);              //delay for 27.8 ms v
    delayMicroseconds(800);
    SendPulse(500);         //on pulse for 0.5 ms v
    delayMicroseconds(1500);     //delay for 1.5 ms v
    SendPulse(500);         //on pulse for 0.5 ms v
    delayMicroseconds(3500);     //delay for 3.5 ms v
    SendPulse(500);         //send pulse for 0.5 ms v
    if (i < 1)
    {
      delay(63);
    }
  }
}
//#########################################################################################Timelapse##########################################################################
void DoTimeLapse(int reps, int timeInterval) //(amount of times called, gap in seconds between each photo)
{
  // DoTimeLapse() transmits a pair of command signals by calling SendSequence() and then delay()
  //
  //timeInterval in ms
  for (int i = 0; i <= reps; i++) //repeats for how many times stated
  {
    SendSequence();             //IR Code
    delay(timeInterval * 1000 / 2); // changes to milliseconds
  }
}
//##################Finishing buzzer     This will be added in when modes are made and it will also have some text for the OLED
void BUZZ() {                         //Should be a way to alter this without code access (volume)
  int BUZZVol = 50; //0-255
  analogWrite(outBUZZ, BUZZVol);
  delay(1000);
  analogWrite(outBUZZ, 0);
}

//#########################################OLED Text

void OLEDblank2() {                         //makes sure the second line is blank. it means i can stop flickering caused by constantly clearing and putting text on the screen. 
  OLED.cursorHome(2);
  OLED.print("                ");
}

void OLEDmode1() {
  OLED.cursorHome(1);
  OLED.print("Manual Trigger  ");
  OLEDblank2();
}

void OLEDmode2() {
  OLED.cursorHome(1);
  OLED.print("Timelapse       ");
  OLEDblank2();
}
void OLEDmode3() {
  OLED.cursorHome(1);
  OLED.print("Bulb/Timer Mode ");
  OLEDblank2;
}
void OLEDmode4() {
  OLED.cursorHome(1);
  OLED.print("Light Detection ");
  OLEDblank2();
}
void OLEDmode5() {
  OLED.cursorHome(1);
  OLED.print("Motion Detection");
  OLEDblank2();
}
int modeChoice;
int totalModes = 5;
//###########################Mode Selection
void loop() {
  while (digitalRead(Inputok == LOW)) {               // OK completes the selection and it will go somewhere else
    OLEDmode1();
    if (digitalRead(Inputdown == HIGH) and modeChoice < totalModes) {   //makes it 'scroll' down the list of options
      modeChoice++;
    }
    if (digitalRead(Inputup == HIGH) and modeChoice > 1) {    //scroll up 
      modeChoice--;
    }
    if (modeChoice == 1) {    //Displays current selection, only exits when ok is pressed 
      OLEDmode1();
    }
    if (modeChoice == 2) {
      OLEDmode2();
    }
    if (modeChoice == 3) {
      OLEDmode3();
    }
    if (modeChoice == 4) {
      OLEDmode4();
    }
    if (modeChoice == 5) {
      OLEDmode5();
    }
  }
  //Now it has exited the while loop cause ok (done) was pressed
  //it will then pick the mode based on the current state of modeChoice
}

I want a simpler way to call the right screen to show (section of current code below). I think there should be a more efficient way of doing this.

    if (modeChoice == 1) {    //Displays current selection, only exits when ok is pressed 
      OLEDmode1();
    }
    if (modeChoice == 2) {
      OLEDmode2();
    }
    if (modeChoice == 3) {
      OLEDmode3();
    }
    if (modeChoice == 4) {
      OLEDmode4();
    }
    if (modeChoice == 5) {
      OLEDmode5();
    }

I was thinking of a for loop like this but don’t know how to call the function (OLEDmode + str(i) ()) this doesnt work but is there a way of adding the current value of i to the other part of the function name? Or is there a way of using the arguments and combining the 5 OLEDmode functions into one with an argument? If so, how?
Or if there are any other methods of fixing.

    for (int i = 1; i <= totalModes; i++) {
      if (modeChoice == i) {
        //OLEDmode + str(i) ()
      }
    }

The screen I’m using is a 16x2 Picaxe OLED. Using the AXE133Y library: Arduino Playground - AXE133Y

You really need to get familiar with the switch statement....

Regards,
Ray L.

You could use an array of pointers to constant strings,
basically you are only switching the first line of the display.

This would replace all the ifs and have no for.

const char blk[] = "                ";
const char sm1[] = "Manual Trigger  ";
const char sm2[] = "Timelapse       ";
const char sm3[] = "Bulb/Timer Mode ";
const char sm4[] = "Light Detection ";
const char sm5[] = "Motion Detection";

const char * const msgs[] = { blk, sm1, sm2, sm3, sm4, sm5};

  OLED.cursorHome(1);
  OLED.print(msgs[modeChoice]);
  OLED.cursorHome(2);
  OLED.print(msgs[0]);

If your code runs on an 8 bit Arduino,
it would be a good idea to place the array and the strings in PROGMEM.

const char blk[] PROGMEM = "                ";
const char sm1[] PROGMEM = "Manual Trigger  ";
const char sm2[] PROGMEM = "Timelapse       ";
const char sm3[] PROGMEM = "Bulb/Timer Mode ";
const char sm4[] PROGMEM = "Light Detection ";
const char sm5[] PROGMEM = "Motion Detection";

const char * const msgs[] PROGMEM = { blk, sm1, sm2, sm3, sm4, sm5};

void setup() {
  Serial.begin(250000);
  for (byte modeChoice = 1; modeChoice < 6; modeChoice++) {
    Serial.print(F("choice "));
    Serial.print(modeChoice);
    Serial.print(F(" 1:\""));
    Serial.print((__FlashStringHelper*)pgm_read_word(&msgs[modeChoice]));
    Serial.print(F("\", 2:\""));
    Serial.print((__FlashStringHelper*)pgm_read_word(&msgs[0]));
    Serial.println(F("\""));
  }
}

void loop() {}
choice 1 1:"Manual Trigger  ", 2:"                "
choice 2 1:"Timelapse       ", 2:"                "
choice 3 1:"Bulb/Timer Mode ", 2:"                "
choice 4 1:"Light Detection ", 2:"                "
choice 5 1:"Motion Detection", 2:"                "

Your for loop idea won't work. There are no variable names in the compiled code, only memory locations. You can't create a variable name in a string to refer to a variable.

Whenever you catch yourself wanting numbers on the ends of your variable names, what you really want is an array. That makes it possible to do what you want with the for loop because now the number means something and isn't just part of the name.

RayLivingston:
You really need to get familiar with the switch statement....

Yeah I didn't know that existed. That looks quite useful.

Whandall:
it would be a good idea to place the array and the strings in PROGMEM.

What is the benifit of doing this?

MaxTheDog73:
What is the benifit of doing this?

Saves RAM, which the smaller Arduinos have very little of.

wildbill:
Saves RAM, which the smaller Arduinos have very little of.

Ah ok. That makes sense. I guess it doesn't really matter becuase the only board I have is a mega 2560 and it has rather a lot of space.