1

I'm trying to use a remote control to control the red/green/blue values of an RGB LED using an Arduino Uno. Here's what I've got so far:

#include <IRremote.h>
#define IRPin 7
#define bluePin 9
#define greenPin 10
#define redPin 11

int blueValue = 0;
int greenValue = 0;
int redValue = 0;

IRrecv irrecv(7);
decode_results results;

void RGBLED(int blue, int green, int red)
{
  analogWrite(redPin, red);
  analogWrite(greenPin, green);
  analogWrite(bluePin, blue);
}

void setup(){
  irrecv.enableIRIn();
  pinMode(sensorPin, INPUT);
  Serial.begin(9600);
}


void loop(){
if (irrecv.decode(&results)){
  switch(results.value)
  {
    case 0xFF6897:
      Serial.println("Red+16");
      redValue = redValue + 16;
      if(redValue >= 256)
      {
        redValue = 255;
      }
    break;
    case 0xFF9867:
      Serial.println("Green+16");
      greenValue = greenValue + 16;
      if(greenValue >= 256)
      {
        greenValue = 255;
      }
    break;
    case 0xFFB04F:
      Serial.println("Blue+16");
      blueValue = blueValue + 16;
      if(blueValue >= 256)
      {
        blueValue = 255;
      }
    break;
    case 0xFF30CF:
      Serial.println("Red-16");
      redValue = redValue - 16;
      if(redValue < 0)
      {
        redValue = 0;
      }
    break;
    case 0xFF18E7:
      Serial.println("Green-16");
      greenValue = greenValue - 16;
      if(greenValue < 0)
      {
        greenValue = 0;
      }
    break;
    case 0xFF7A85:
      Serial.println("Blue-16");
      blueValue = blueValue - 16;
      if(blueValue < 0)
      {
        blueValue = 0;
      }
    break;       
  }
irrecv.resume();
RGBLED(blueValue, greenValue, redValue);
}

Everything works fine, except the adding up to redValue. It's the same exact code that is used for greenValue and blueValue. The substraction from redValue works fine too. Everytime the problematic function gets called, the serial monitor prints Re and the whole thing just stops working. I have tried rewriting that code from scratch, copying the other code, but nothing worked. I have switched up the codes from my IR remote but that had nothing to do with it either. Can anyone spot the mistakes or tell me what I'm doing wrong?

Thanks in advance

  • try a short delay and see if that helps – dandavis Jun 27 '18 at 17:44
  • Try swapping the red case with the green case, and see if now the green will give the problem. (i.e. maybe the first case somehow breaks things). – Gerben Jun 27 '18 at 17:55
  • 1
    Are you sure this is the code you are using? setup() uses sensorPIN but it does not appear to be declared anywhere. – Craig Jun 27 '18 at 21:21
  • @Craig I'm also using a sound sensor as a clap sensor to turn on the LED, but that isn't causing the issue, so I removed it. – Wardk Jun 28 '18 at 13:47
  • @Gerben I tried switching the two around and the red one is still broken. – Wardk Jun 28 '18 at 13:53

Your Answer

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

Browse other questions tagged or ask your own question.