if (bluVal == 255) { // if blue is at maximum, continue to light it up
analogWrite(bluPin, bluVal);
} else { // else fade in the blue color
bluVal ++;
analogWrite(bluPin, bluVal);
}
can be shorter ==>
if (bluVal < 255) bluVal ++;
analogWrite(bluPin, bluVal);
}
also other colors ![]()
I would rethink the code,
you have 3 leds that should overlap
RED [0..512] => 0..255..0
GREEN [256-768] ==> 0..255..0
BLUE [512-768] ==> 0..255
That would give something like the following code - give it a try
//Setup all pins
int sensPin = 2; // set the touch sensor (analog) input pin on Arduino
int redPin = 3; // set the PWM (analog) output pin on Arduino controlling the red anode
int grnPin = 5; // set the PWM (analog) output pin on Arduino controlling the green anode
int bluPin = 6; // set the PWM (analog) output pin on Arduino controlling the blue anode
//Initialize variables
int redVal; // pulse width variable for red anode
int grnVal; // pulse width variable for green anode
int bluVal; // pulse width variable for blue anode
void setup()
{
pinMode(redPin, OUTPUT); // set the LED pins as output
pinMode(grnPin, OUTPUT);
pinMode(bluPin, OUTPUT);
Serial.begin(9600);
}
void loop()
{
int val = analogRead(sensPin); // read touch sensor values
if (val < 256)
{
redVal = val;
greenVal = 0;
blueVal = 0;
}
else if (val < 512)
{
redVal = 512 - val;
greenVal = val-256;
blueVal = 0;
}
else if (val < 768)
{
redVal = 0;
greenVal = 768-val;
blueVal = val-512;
}
else
{
redVal = 1023-val;
greenVal = 1023-val;
blueVal = 255;
}
display(redVal, greenVal, blueVal);
}
void display(int r, int g, int b)
{
analogWrite(redPin, r);
analogWrite(grnPin, g);
analogWrite(bluPin, b);
}