How to switching between more than two loops?

hello all, i,m new to programming and this is my second very simple project for my own learning base. i,v successfully managed to switch between two loops and now i want to add three more loops for additional blue led blinking and rest two loops for fading and color mixing, my main target is switching between more than two loops. your help will be appreciated to solving this method, thanks in advance.
here is my code.

int greenled = 4; //Led's and pins
int redled = 2;
int modeSwitch = 14;
int buttonState;
int redledState = LOW;
int greenledState = LOW;
boolean modeState;

void setup()
{
  pinMode(greenled, OUTPUT); //Pinmodes of the leds
  pinMode(redled, OUTPUT);

  pinMode(modeSwitch, INPUT);
}

void modeOne()
{
digitalWrite(redled, 255);
digitalWrite(greenled, 0);
delay(500);
digitalWrite(redled, 255);
digitalWrite(greenled, 255);
delay(500);
 
 
 }

void modeTwo()
{
  digitalWrite(redled, 0);
digitalWrite(greenled, 255);
delay(500);
digitalWrite(redled, 255);
digitalWrite(greenled, 255);
delay(500);
 
 
}
void loop() {

  buttonState = digitalRead(modeSwitch);
if (buttonState == LOW) { 

  modeState = !modeState; // switch the state of the loops
  
}
if (modeState == false) {

  modeOne();

}

else { // (if modeState == true)

  modeTwo();

}
}

You can use a switch case.

1 Like

thanks for reply mate. asking just for my knowledge can we also switch between multi loops ?

You can switch between as many loops as you like.

Did you look at the switch/case statement?

How are you planning to switch? Perhaps each button press moves to the next mode function?

Can you write a program to count button presses? This would be the heart of such an approach. There are example programs in the IDE which could lead you to be able to count a button press, if you can't already.

Sooner or later you will notice that your mode functions temporarily blind you to other activities like button presses, so your button will be unresponsive and sluggish.

You may have "modes" in mind that are, in effect, infinite loops, never returning, therefor you would never ever see the button getting pressed. At that time you will need to start programming your modes in a fundamental different way, look forward to that.
'For now, count a button press, use switch/case to select and run the mode of your choice.

switch/case

a7

1 Like

consider

int greenled = 4; //Led's and pins
int redled = 2;
int modeSwitch = 14;
int buttonState;
int redledState = LOW;
int greenledState = LOW;

#define N_MODE 3
int mode;

// -----------------------------------------------------------------------------
void setup()
{
    pinMode(greenled, OUTPUT); //Pinmodes of the leds
    pinMode(redled,   OUTPUT);
    pinMode(modeSwitch, INPUT_PULLUP);
}

void modeOne()
{
    digitalWrite(redled, 255);
    digitalWrite(greenled, 0);
    delay(500);
    digitalWrite(redled, 255);
    digitalWrite(greenled, 255);
    delay(500);
}

void modeTwo()
{
    digitalWrite(redled, 0);
    digitalWrite(greenled, 255);
    delay(500);
    digitalWrite(redled, 255);
    digitalWrite(greenled, 255);
    delay(500);
}

void loop()
{
    byte but = digitalRead(modeSwitch);
    if (buttonState != but)  {
        buttonState = but;
        delay (10);

        if (LOW == but)
            mode++;
            if (N_MODE <= mode)
                mode = 0;
    }

    switch (mode)  {
    case 0:
        break;

    case 1:
        modeOne ();
        break;

    case 2:
        modeTwo ();
        break;
    }
}
1 Like

Or just wait for someone to write the program for you.

a7

so i'm an EE. if i'm trying to explain an electronic circuit or digital logic, would it be more efficient to use the nomenclature of that field.

hurray.......... thanks mate. you did excellent job. thank so much.

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