0

I'm currently working on an assignment which has to turn on an RGB LED using a potentiometer and button. The problem I currently have is that the button is supposed to change the color I want the LED to display (presses one time: red (pin9), again green (pin10), again blue (pin11)). As I'm changing through the ports with the potentiometer, the potentiometer will read it with as analogWrite. For example, if I move the potentiometer all the way to the left and press the button the LED will display red and move it back all the way to 0 and press the button twice the LED will stay red (do nothing). If I press it one more time it will turn off. Another example, I turn on red again, then use green and turn off blue using the potentiometer and button giving me orange. How do I make it so to accomplish all of this while using both and combining colors?

THIS IS A HW QUESTION AND NOT ASKING FOR A WHOLE SOLUTION BUT RATHER WHERE TO START BECAUSE I ALREADY GOT STUCK AND WANT TO RESTART PER SE

Edit:I solved my doubt! Thanks for the help!

  • So - if I'm understanding that right - you want your button to be the set button, which is used 3 times sequentially to mix the colors red, green and blue with the value, that the poti has at the respective stages. So building the RGB color in 3 stages out of its components. Is that, what you want? And what exactly is your problem here? Reading the poti? Outputting 3 PWM signals for the RGB LED? calculating the values? Managing the program flow? – chrisl Sep 27 at 19:26
  • 1
    Oh, sorry for not making it clear. I'm kind of new to this (as shown). I believe my main problem is reading the reading the button and poti as to mix the colors as aforementioned. – Matt Weber Sep 27 at 19:33
  • 1
    Edit: The current problem I'm experiencing is the next: I'm running the simulation with if and counter for the button and my first color should be green. The next part in my code is to turn on blue but that's on two presses. On the first press it seems to skip green and go straight to blue. – Matt Weber Sep 27 at 19:40
  • So that means, that it actually is more of a software question. You need to show us your code (you can edit your question to include it there), so that we can help you with that – chrisl Sep 27 at 20:00
  • 1
    The most common problem beginners have with buttons is coding it to be "If HIGH do this" and then wonder why it keeps happening rather than just happening once when the button is pressed. You have to learn to watch for a change in the state of the button - as in "If changed from LOW to HIGH do this". – Majenko Sep 28 at 10:07
0

You method sound a bit more complicated that it needs to. What I would do is have the button switch between three colors, and have the potentiometer change the brightness of the currently selected color.

For example, when first starting up, the red led is selected. Turning the potentiometer changes the brightness of the red led (in real time). Let's say you turn it 50%, so the red led is at 50% brightness. When you're happy with that, you press the button, and now green is selected. (Since the potentiometer is still at 50% from the red, the green led will immediately turn on at 50% brightness.) You can then turn the pot to change the brightness of the green led. Let's say you turn it all the way of to 0%. Then press the button to select the blue led. Turn it to 100% and the combined color would be a dark purple. Press the button again to select red again.

Here is some proof of concept code:


byte selectedColor = 0; // 0:red, 1:green, 2:blue
byte oldButtonState = HIGH;

void loop() {
  byte buttonState = digitalRead(buttonPin); // read the button
  if( buttonState==LOW && oldButtonState==HIGH ) // see if the button is pressed, but wasn't pressed the last time
  { 
    selectedColor++; // to to next color
    if( selectedColor==3 )
      selectedColor = 0;// after blue, go back to red
  }
  oldButtonState = buttonState; // remember the buttons state for the next loop

  int brightness = map(analogRead(potPin), 0, 1023, 0, 255);

  if( selectedColor==0 )
    analogWrite(redPin, brightness);
  else if( selectedColor==0 )
    analogWrite(greenPin, brightness);
  else
    analogWrite(bluePin, brightness);
}

PS I left out the pinMode stuff in the setup.

PPS make sure the led leads are connected to PWM pin on the Arduino (they have a ~ next to them on the PCB).

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.