Please help me

Hello, I need little help... I have RGB sensor with every of 3 colors is attached to diffrent PWM pin, i have potentiometer attached to analog in and one button... I dont have 3 potentiometers for each color so i want to use button so when its clicked u change one colors brightness, and when you click it again you change brightess for another color... can you say me which command to use for that, i tried with if command but it cant work like that...

Thank you :slight_smile:

Hi,
Welcome to the forum.

Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

That is so we can see what you have and advise on how to help you.

Thanks… Tom… :slight_smile:

i tried with if command but it cant work like that...

What do you mean by that ?

Post your code (and please use code tags as shown below)

type
** **[code]** **

paste your code after that
type
** **[/code]** **
after that

So the it looks like

your code here

By the way, I guess you a RGB led, not a sensor.[/b]

Hello, I have not done anything, I got stuck at the very start of the loop... If I put it like this:

const int green = 5;
const int red = 6;
const int blue = 3;
const int button = 2;
int buttonState = 0;
int val = 0;
int analogPin = 3;



void setup() {
  // put your setup code here, to run once:
pinMode(green, OUTPUT);
pinMode(red, OUTPUT);
pinMode(blue, OUTPUT);
pinMode(button, INPUT);
}

void loop() {
  // put your main code here, to run repeatedly:
val = analogRead(analogPin);
buttonState = digitalRead(button);
if (buttonState == HIGH){
  analogWrite(green, val / 4);
}
}

it will only change brightess of green led when button is pressed... And yes i have led, not sensor :slight_smile:

thank

OK, you need to use a variable to indicate which color was selected with the button press.

void loop()
{
  static byte selectedColor = 0; // 0 == R, 1 == G, 2 == B
}

And every time the button is pressed, you can increment the variable.

void loop()
{
  static byte selectedColor = 0; // 0 == R, 1 == G, 2 == B

  if(buttonState == HIGH)
  {
    selectedColor++;
    if(selectedColor >= 3)
    {
      selectedColor = 0;
    }
  }
}

Now you can take decisions based on the value of selectedColor; it can be done with if / else if / else or with switch/case

[code]
void loop()
{
  static byte selectedColor = 0; // 0 == R, 1 == G, 2 == B

  if(buttonState == HIGH)
  {
    selectedColor++;
    if(selectedColor >= 3)
    {
      selectedColor = 0;
    }
  }

  switch(selectedColor)
  {
    case 0:
      // set red color
      ...
      ...
      break;
    case 1:
      // set green color
      ...
      ...
      break;
    case 2:
      // set blue color
      ...
      ...
      break;
  }
}

Now you will encounter a couple of problems.

The first one will be bounce; one press might result in multiple transitions from LOW to HIGH (and vice versa). The IDE comes with a debounce example; understand it and adjust to your needs.

Once you have taken that hurdle, you will find out that it still does not work because loop() is executed thousands of time in a second. So you basically need to react on a change from LOW to HIGH and not on the HIGH state. The IDE comes with a statechange example; again understand it and adjust to your needs.

I suggest you add another variable byte selectedColour =0; which can take any of the values 0, 1, 2 for Red Green or Blue.
Also make 3 variables to hold the value for each colour byte blueVal = 0; etc.

Then read check when your button changes from OFF to ON with something like

buttonVal = digitalRead(buttonPin);
if (buttonVal == LOW && prevButtonVal == HIGH) {
   selectedColour += 1; // move to next colour
   if (selectedColour > 2) {
      selectedColour = 0;
   }
}
prevButtonVal = buttonVal;

(That could all go in a buttonRead() function)

Then, elsewhere in your program you have something like

if (selectedColour == 0) {
   redVal = analogVal / 4;
}
else if (selectedColor == 1) {
   blueVal = analogVal / 4;
}
else {
   greenVal = analogVal / 4;
}

And if you were to use an array to hold the colour values it could be even shorter

colourVal[selectedColour] = analogVal / 4;

The trick is to separate the reading of the button from the setting of the colours by using a variable as an intermediary. The variable will remember its setting until the next time it is changed.

...R

sterretje:
OK, you need to use a variable to indicate ....

SNAP ...

But you don't spell colour correctly :slight_smile:

...R

Robin2:
But you don’t spell colour correctly :slight_smile:

Not native English speaking hence occasionally the f.ck.p due to the difference between English English and those non-english English languages. I try to follow the spell checker; if it says it’s OK, I usually change it so I get a squirly :wink:

Not native English speaking hence occasionally the f.ck.p due to the difference between English English and those non-english English languages.

:astonished:

... and the notebook screen gets another bath as I spew coffee! Too funny.

Ray

sterretje:
due to the difference between English English and those non-english English languages

That's why I had the smiley :slight_smile:

...R

Robin2:
That's why I had the smiley :slight_smile:

Don't worry, I did get it.