Home Arduino RGB LED utilizing potentiometer and push button

RGB LED utilizing potentiometer and push button

0
RGB LED utilizing potentiometer and push button

[ad_1]

You technique sound a bit extra sophisticated that it must. What I might do is have the button change between three colours, and have the potentiometer change the brightness of the presently chosen shade.

For instance, when first beginning up, the crimson led is chosen. Turning the potentiometer modifications the brightness of the crimson led (in actual time). As an example you flip it 50%, so the crimson led is at 50% brightness. While you’re pleased with that, you press the button, and now inexperienced is chosen. (For the reason that potentiometer remains to be at 50% from the crimson, the inexperienced led will instantly activate at 50% brightness.) You possibly can then flip the pot to vary the brightness of the inexperienced led. As an example you flip all of it the best way of to 0%. Then press the button to pick out the blue led. Flip it to 100% and the mixed shade can be a darkish purple. Press the button once more to pick out crimson once more.

Right here is a few proof of idea code:


byte selectedColor = 0; // 0:crimson, 1:inexperienced, 2:blue
byte oldButtonState = HIGH;

void loop() {
  byte buttonState = digitalRead(buttonPin); // learn the button
  if( buttonState==LOW && oldButtonState==HIGH ) // see if the button is pressed, however wasn't pressed the final time
  { 
    selectedColor++; // to to subsequent shade
    if( selectedColor==3 )
      selectedColor = 0;// after blue, return to crimson
  }
  oldButtonState = buttonState; // bear in mind the buttons state for the following 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 not noted the pinMode stuff within the setup.

PPS be certain that the led leads are related to PWM pin on the Arduino (they’ve a ~ subsequent to them on the PCB).

[ad_2]

LEAVE A REPLY

Please enter your comment!
Please enter your name here