Hello all,
I am new to this form so thanks for any input you may be able to give to a noobie.
I am having a little bit of trouble with the code that would be required to switch between controlling color and controlling brightness with a rotary encoder. I am using the click button on the encoder to do the switching but right now I have to hold the button down to control color, else the brightness is controlled. I would like to be able to switch over to controlling the color with just a single click, and then click again to return to controlling brightness.
Here is the code that I have currently:
//rotary encoder inputs
int EN_PIN_BUTTON = 5;
int EN_PIN_A = 6;
int EN_PIN_B = 7;
//Led Outputs
int greenled = 9;
int redled = 10;
int blueled = 11;
int brightnessLevel = 0;
int Color = 0;
int Red = 0;
int Green = 0;
int Blue = 0;
//EN_PIN_A current state and last state
int aState;
int aLastState;
void setup() {
pinMode (EN_PIN_A, INPUT);
pinMode (EN_PIN_B, INPUT);
pinMode (EN_PIN_BUTTON, INPUT);
pinMode (redled, OUTPUT);
pinMode (greenled, OUTPUT);
pinMode (blueled, OUTPUT);
Serial.begin (9600);
brightnessLevel = 250; //initial brightness
Color = 35; //initial color
aLastState = digitalRead(EN_PIN_A);
}
void loop() {
// switching between color and brightness
if (digitalRead(EN_PIN_BUTTON) == LOW) {
aState = digitalRead(EN_PIN_A);
if (aState != aLastState) {
if (digitalRead(EN_PIN_B) != aState) {
Color += 1;
} else {
Color -= 1;
}
}
} else {
aState = digitalRead(EN_PIN_A);
if (aState != aLastState) {
if (digitalRead(EN_PIN_B) != aState) {
brightnessLevel += 10;
} else {
brightnessLevel -= 10;
}
brightnessLevel = constrain (brightnessLevel, -255, 250);
}
}
//define color functions
Red = 255 * sin(0.1 * Color - 2 * 3.14159265 / 3) + brightnessLevel;
Green = 255 * sin(0.1 * Color) + brightnessLevel;
Blue = 255 * sin(0.1 * Color + 2 * 3.14159265 / 3) + brightnessLevel;
Red = constrain(Red, 0, 250);
Green = constrain(Green, 0, 125);
Blue = constrain(Blue, 0, 125);
// Write color to outputs
analogWrite(redled, Red);
analogWrite(greenled, Green);
analogWrite(blueled, Blue);
aLastState = aState;
}
I hope this all makes sense. Thanks in advance for the help!