Swich between modes via button

So I'm trying to make a device that when you press a button it will cycle through methods in the loop statement. I have tried to use a switch case but I can't get it to work. I'm basically trying to choose from the two methods via a pushbutton. I've gotten it to stick to what ever default method (void method1) that I leave in the default case and have it call loop() at the end. However then I can't select the second case.

I feel like I need to have the loop run some sort of code and listen for an interrupt from the button, and when it gets the signal then it switches to the next one. I also plan to store the last, most current, state in the eeprom so it will run that method at setup.

Here is one method

int ledPins[] = { 11, 3, 10 };
int inputPin = 0;


void setup()
{
  pinMode(inputPin, INPUT);
  for(int i = 0; i < 3; i++)
    pinMode(ledPins[i], LOW);
}

void loop()
{
  method1();
}

void method1() {
  int inputValue = analogRead(inputPin);
  int hueValue = map(inputValue, 0, 1024, 0, 360);
  setHueValue(hueValue);
}

void setHueValue(int hueValue)
{
  setColor(ledPins, hsvToRgb(hueValue, 1, 1));
}

void setColor(int* led, const byte* color)
{
  for(int i = 0; i < 3; i++){
    analogWrite(led[i], 255-color[i]);
  }
}

byte rgb[3];

byte* hsvToRgb(int h, double s, double v)
{
        // Make sure our arguments stay in-range
        h = max(0, min(360, h));
        s = max(0, min(1.0, s));
        v = max(0, min(1.0, v));
        if(s == 0)
        {
                // Achromatic (grey)
                rgb[0] = rgb[1] = rgb[2] = round(v * 255);
                return rgb;
        }
        double hs = h / 60.0; // sector 0 to 5
        int i = floor(hs);
        double f = hs - i; // factorial part of h
        double p = v * (1 - s);
        double q = v * (1 - s * f);
        double t = v * (1 - s * (1 - f));
        double r, g, b;
        switch(i)
        {
                case 0:
                        r = v;
                        g = t;
                        b = p;
                        break;
                case 1:
                        r = q;
                        g = v;
                        b = p;
                        break;
                case 2:
                        r = p;
                        g = v;
                        b = t;
                        break;
                case 3:
                        r = p;
                        g = q;
                        b = v;
                        break;
                case 4:
                        r = t;
                        g = p;
                        b = v;
                        break;
                default: // case 5:
                        r = v;
                        g = p;
                        b = q;
        }
        rgb[0] = round(r * 255.0);
        rgb[1] = round(g * 255.0);
        rgb[2] = round(b * 255.0);
        return rgb;
}

And the method 2

int ledPins[] = { 11, 3, 10 };
int inputPin = 0;

void setup()
{
  pinMode(inputPin, INPUT);
  for(int i = 0; i < 3; i++)
    pinMode(ledPins[i], OUTPUT);
}

void loop()
{
  int inputValue = analogRead(inputPin);
  int delayValue = map(inputValue, 0, 1024, 0, 83);

  setHueValue(computeNextValue());
  
  delay(delayValue);
}

int colorValue = 0;

int computeNextValue()
{
  colorValue = (colorValue+1) % 360;
  return colorValue;
}

void setHueValue(int hueValue)
{
  setColor(ledPins, hsvToRgb(hueValue, 1, 1));
}

void setColor(int* led, const byte* color)
{
  for(int i = 0; i < 3; i++)
    analogWrite(led[i], 255-color[i]);
}

byte rgb[3];

byte* hsvToRgb(int h, double s, double v)
{
        // Make sure our arguments stay in-range
        h = max(0, min(360, h));
        s = max(0, min(1.0, s));
        v = max(0, min(1.0, v));
        if(s == 0)
        {
                // Achromatic (grey)
                rgb[0] = rgb[1] = rgb[2] = round(v * 255);
                return rgb;
        }
        double hs = h / 60.0; // sector 0 to 5
        int i = floor(hs);
        double f = hs - i; // factorial part of h
        double p = v * (1 - s);
        double q = v * (1 - s * f);
        double t = v * (1 - s * (1 - f));
        double r, g, b;
        switch(i)
        {
                case 0:
                        r = v;
                        g = t;
                        b = p;
                        break;
                case 1:
                        r = q;
                        g = v;
                        b = p;
                        break;
                case 2:
                        r = p;
                        g = v;
                        b = t;
                        break;
                case 3:
                        r = p;
                        g = q;
                        b = v;
                        break;
                case 4:
                        r = t;
                        g = p;
                        b = v;
                        break;
                default: // case 5:
                        r = v;
                        g = p;
                        b = q;
        }
        rgb[0] = round(r * 255.0);
        rgb[1] = round(g * 255.0);
        rgb[2] = round(b * 255.0);
        return rgb;
}

This link is what you want.

But basically, you want loop to look something like:

void loop()
{
    bool buttonState = readButton();
    // look for button LOW to HIGH edge transition
    if (buttonState != lastButtonState && buttonState == HIGH)
    {
        currentFunction++;
        currentFunction = currentFunction % 2; // limit the number of choices to 2 at the moment. 0 and 1.
    }
    lastButtonState = buttonState;

    // and run the function based on our current state
    if (currentFunction == 0)
    {
          function1();
    }
    else
    {
          function2();
    }
}

I actually tried that. But I was having an issue on where to implement my code into it. I'm going to try this again though. Most likely you're better at this than me...

I can't believe the number of people that have problems reading switches that don't think it is important to explain HOW the switch is wired.

How IS your switch wired? You do have an external resistor, right?

Why? Using the internal pullup resistor, INPUT_PULLUP as the mode, and connecting one leg to the digital pin and the other leg to ground is so much easier.

PaulS:
I can't believe the number of people that have problems reading switches that don't think it is important to explain HOW the switch is wired.

How IS your switch wired? You do have an external resistor, right?

Why? Using the internal pullup resistor, INPUT_PULLUP as the mode, and connecting one leg to the digital pin and the other leg to ground is so much easier.

I have the switch's input on digital pin 2, and a 470 resistor from D2 to Gnd. Im just using a wire for now to jump D2 to +5v

Ok so I've been still playing with this. It is frustrating because if this was PHP i'd have it. My current code runs and gets stuck in the FOR which is inside the Switch on Loop()

mattenderle:
Ok so I've been still playing with this. It is frustrating because if this was PHP i'd have it. My current code runs and gets stuck in the FOR which is inside the Switch on Loop()

Post your current code.

arduinodlb:
Post your current code.

I think I got it. It's a little crude because when I want to go back from case two i just set pin seven to reset.

mattenderle:
I think I got it. It's a little crude because when I want to go back from case two i just set pin seven to reset.

Yeah, you don't need to do that. See post #1.