Hi there,
I'm new to Arduino, this is my first post here. I don't know if my problem is hardware or software but I thought sanity-checking the software would be a good start.
I've got 3 LEDs, a clear 5mm red one, clear 5mm green and clear 5mm blue, connected to pins 9, 10 and 11 of an Arduino Uno, each of them with a 240 ohm resistor. I'm not great at figuring out resistors but from what I've read in the books and online tutorials I've seen (which tend to recommend 270 ohms - I don't have any of those), 240 should be fine. Anyway, as you can guess from the setup, I want to fade the LEDs in and out in order to cycle through all of the colours in the rainbow.
The problem I'm having is that although I'd expect a linear fade up and down in the LEDs, that's not what I'm seeing. The LEDs fade up quite sharply from off to something that looks full brightness with relatively small values, then stay bright for most of the fade up and the fade back down, finally ramping steeply down to off in the lower values again. Oddly, the behaviour seems to be different for each pin. Pin 9 is the least-afflicted and has the smoothest fade up and down, pin 10 fades up and down quite quickly, and pin 11 fades up quickly but fades down pretty smoothly. I've swapped the LEDs around and found that the issue seems to be with the pins rather than the LEDs.
Here's my code. Any help would be much appreciated.
// Pins
const int redPin = 9;
const int greenPin = 10;
const int bluePin = 11;
// Brightness values
int red = 0;
int green = 0;
int blue = 0;
int zone = 0; // Used to work out which LEDs should be on, off, or ramping up or down
int phase = 0; // Used to ramp brightnesses up or down (ranges between 0 - 255)
int fadeSpeed = 1; // amount phase is incremented by each update
void UpdateLedColours()
{
phase += fadeSpeed;
if(phase > 255)
{
phase = 0;
zone += 1;
if(zone > 5)
{
zone = 0;
}
}
switch(zone)
{
case 0:
red = 255;
green = phase;
blue = 0;
break;
case 1:
red = 255 - phase;
green = 255;
blue = 0;
break;
case 2:
red = 0;
green = 255;
blue = phase;
break;
case 3:
red = 0;
green = 255 - phase;
blue = 255;
break;
case 4:
red = phase;
green = 0;
blue = 255;
break;
case 5:
red = 255;
green = 0;
blue = 255 - phase;
break;
}
}
void setup()
{
pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
}
void loop()
{
UpdateLedColours();
analogWrite(redPin, red);
analogWrite(greenPin, green);
analogWrite(bluePin, blue);
delay(30);
}