Hello again. I want to use a single rotary encoder with a pushbutton switch to change the color and the brightness of an RGB bulb. I have also been trying to use switches to mimic a rotary encoder in tinkercad as well as the rotary encoder off of one of the motors, both to no avail.
I would greatly appreciate any help as I am a noob.
Thank you in advance.
//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;
}
There is a lot more going on than you said in your description. The trick with development is to do one thing at a time.
For a start you don't wire up an RGB LED with a resistor in each lead, because that will cause a change in one colour component to affect the other two components of the light. The common connector needs to be connected directly to the 5V or Gnd depending if it is a common anode or common cathode.
You don't wire push buttons with an LED like that, what you have will not work.
The problem with trying to follow a diagram like you have is that it is only a physical layout diagram and it is almost impossible to follow. There are no labels on anything and in order to make sense of it we would have to look up the data sheet of each component and draw it out properly.
The language of electronics is schematic. Can you post one please, hand drawn is fine, and miss out all the components you don't address in your code.
I have simplified my schematic as well as labeled.
For using the libraries, I have no clue about it. It also seems to add extra precision, which in this case, is not necessarily needed. If it simplifies what I am trying to do, please let me know.
If it simplifies what I am trying to do, please let me know.
It simplifies it. The whole process is driven by interrupts in the background from one or two interrupt pins.
With the RGB Led, you have gone from one extreme to the other. You need to have three resistors with one. Not four, not one.
I know what you are thinking with the led and push switch but in practice it will not work because the input will not be pulled down to a low enough value when the button is unpressed.
The LEDs “for tinker cad only” needs to be removed if you want to have the circuit work in real life.
Make your code print out the values it is sending to your LEDs, does it match what you expect?
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;
I don't think this is going to translate colour & brightness into red, green, blue in the way you intended. Google for "hls to rgb in c" to find some example code.
Thanks for using code tags. Before you post your code again, please use the auto-format tool in the ide.
Look forget Tinkercad it is not important. What encoder to YOU have?
Please post a link to the data sheet, or where you got it from. Then I can show you some code that will read it.