Hi there!
I'm a total newbie with programming so this might me a simple thing I haven't thought of.
For school I'm doing a little assignment which comes down to this;
I have to be able to control a RGB led with ONE potentiometer and a button.
With each button press one of the base colors needs to be selected and then with the potentiometer I have to be able to adjust the brightness. So after 3 presses you have created a color you like consisting of the 3 base colors. the fourth press of the button is a lock state. nothing happens when you turn the potentiometer.
I managed to get the code to do just that EXCEPT, when I press the button again so I can re-adjust the brightness of a base color, the color immediately takes on the value of the potentiometer. But when I want to change the second color... green in this case. Red has changed as well.
So i want to press the button but not change the color immediately, lets say I want to change the color when I adjust the potentiometer so that when I press the button twice...the red color stays the same (even though the potentiometer is not in the same position I first put it in) and on the second color (green) when I turn the potentiometer that color changes brightness.
I hope I'm being clear (Dutch, so English is not my native language). As I said I am very new to programming and have no experience whatsoever.
It feels like a simple bit of code I need but I can't figure it out. And couldn't find (or didn't know what to look for) a solution on the web.
code is written for an Arduino Uno. Code is from a larger whole. But is able to run standalone.
const int red = 9;
const int green = 10;
const int blue = 11;
const int colorMixerButton = 13;
int colorMixerButtonState = 0;
int lastColorMixerButtonState = 0;
int potMeter = A0;
int potMeterValue = 0;
int outputValue = 0;
int colorMixerCount = 0;
void colorAdjust()
{
colorMixerButtonState = digitalRead(colorMixerButton);
if (colorMixerButtonState != lastColorMixerButtonState)
{
delay(200);
if (colorMixerButtonState == HIGH)
{
colorMixerCount++;
}
}
if (colorMixerCount == 1)
{
potMeterValue = analogRead(potMeter);
outputValue = map(potMeterValue, 0, 1023, 0, 255);
analogWrite(red, outputValue);
delay(2);
}
if (colorMixerCount == 2)
{
potMeterValue = analogRead(potMeter);
outputValue = map(potMeterValue, 0, 1023, 0, 255);
analogWrite(green, outputValue);
delay(2);
}
if (colorMixerCount == 3)
{
potMeterValue = analogRead(potMeter);
outputValue = map(potMeterValue, 0, 1023, 0, 255);
analogWrite(blue, outputValue);
delay(2);
}
if (colorMixerCount == 4)
{
colorMixerCount = 0;
delay(100);
}
}
void setup()
{
}
void loop()
{
colorAdjust();
}
Hope someone is able to give me at least pointers or a bit of code to help me.
Thanks for reading!