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;
}