I am creating a controller to drive 10 meters of RGB LED's. It's essentially an Arduino Pro Mini connected to three mosfets and an IR receiver on a circuit board. The hardware doesn't really matter as I've confirmed it working with basic scripts such as a hue cycle I wrote.
Where my problem lies is in my main controller code. The code is supposed to read the IR code from the remote using a library and then it uses a switch case to change colors. The basic functionality works, but I have started to implement brightness control and this is where I am having issues.
The code uses map to change the overall brightness of the color outputs. For the red and blue LED's the brightness works as expected, but for the green LED's if the PWM value is anything less than 255 then they turn off completely. I have confirmed via serial that the values are what they should be. What is confusing is that if I change the pin numbers in the program so that outputting red is actually on the green pin, the result is the same. Even if I analogWrite a value of 250 in void setup() the result is identical to the original issue. I have uploaded some of my test codes again and they work flawlessly with the green channel fading/outputting below 255. Can any of you spot my mistake or offer any advice? The code is a little rough since it is a work in progress and there are is still more functionality I plan to add. Thanks!
//Include Libraries
#include <IRremote.h>
//Various variables
long ir = 0;
int RECV_PIN = 11;
int lastir = 0;
int brightness = 255;
int red = 5;
int green = 3;
int blue = 9;
//Prepare IR Library
IRrecv irrecv(RECV_PIN);
decode_results results;
void setup()
{
pinMode(red, OUTPUT);
pinMode(green, OUTPUT);
pinMode(blue, OUTPUT);
Serial.begin(9600);
irrecv.enableIRIn(); // Start the receiver
//Set all outputs to 0
analogWrite(red,0);
analogWrite(green,0);
analogWrite(blue,0);
}
void loop() {
//Get IR code from reciever
if (irrecv.decode(&results)) {
lastir = ir;
ir = results.value;
Serial.println(ir); //Print to serial for debugging
irrecv.resume(); // Receive the next value
}
//Start switch case
switch(ir){
case 284103405: //Power Button
power();
break;
case 284151855: //Brightness up
changeBrightness(1);
break;
case 284111055: //Brightness down
changeBrightness(0);
break;
//Color Buttons follow
case 284147775: //White
color(255,255,255);
break;
case 284131965: //red
color(255,0,0);
break;
case 284131455: //green
color(0,255,0);
break;
case 284115135: //blue
color(0,0,255);
break;
}
}
void reset(){ //resets all colors to 0 when called
analogWrite(red, 0);
analogWrite(green, 0);
analogWrite(blue, 0);
}
void power(){
reset();
}
void changeBrightness(int x){ //modifies brightness value
if(x==1){ //up
brightness = brightness+5;
brightness = constrain(brightness,0,255);
}
if(x==0){ //down
brightness = brightness-5;
brightness = constrain(brightness,0,255);
}
//diagnostics
Serial.print("Brightness: ");
Serial.println(brightness);
}
void color(int r, int g, int b){
reset();
//apply brightness by mapping the value
r = map(r,0,255,0,brightness);
g = map(g,0,255,0,brightness);
b = map(b,0,255,0,brightness);
//write the values
analogWrite(red, r);
analogWrite(green, g);
analogWrite(blue, b);
//diagnostics
Serial.println(r);
Serial.println(g);
Serial.println(b);
}