Im trying to make an RGB stirp light controlled with an IR controller. Theres 4 pins: 1…
- User
Im trying to make an RGB stirp light controlled with an IR controller. Theres 4 pins: 1 is power, the other 3 are R G and B. I figured I could hook up the power pin to a PWM port for analog writing to control the power received by the strip light to adjust the brightness here but it seems to be sporradic at the lower ends of the brightness. Is it because of the analogWriting at the bottom to the red green and blue Vals not being adjusted for the brightness value?
#include <IRremote.hpp>
const int irPin = 2;
const int pinRed = 3; const int pinGreen = 5; const int pinBlue = 6; const int pinPower = 9;
const int wait = 5; const int contrast = 255; // 0 - 255 int brightness = 255; // 0 - 255
int redVal = contrast; int greenVal = 0; int blueVal = 0;
int phase = 0;
void setup() { // Set the pin mode to output pinMode(pinRed, OUTPUT); pinMode(pinGreen, OUTPUT); pinMode(pinBlue, OUTPUT); pinMode(pinPower, OUTPUT); // IR support IrReceiver.begin(irPin, ENABLE_LED_FEEDBACK); }
void loop() { // Read IR if (IrReceiver.decode()) { const int speed = 50; switch (IrReceiver.decodedIRData.command) { case 21: // - == 21 brightness -= speed; break; case 9: // + == 9 brightness += speed; break; } brightness = constrain(brightness, 0, 255); IrReceiver.resume(); } analogWrite(pinPower, brightness); // ChatGPT phasing (im lazy) if (phase == 0) { greenVal++; if (greenVal >= contrast) phase++; } else if (phase == 1) { redVal--; if (redVal <= 0) phase++; } else if (phase == 2) { blueVal++; if (blueVal >= contrast) phase++; } else if (phase == 3) { greenVal--; if (greenVal <= 0) phase++; } else if (phase == 4) { redVal++; if (redVal >= contrast) phase++; } else if (phase == 5) { blueVal--; if (blueVal <= 0) phase = 0; } // Write the current values to the pins analogWrite(pinRed, redVal); analogWrite(pinGreen, greenVal); analogWrite(pinBlue, blueVal); // Small delay to observe the color transition smoothly delay(wait); }
- Assistant