Hi there,
I'm a noob to both the arduino and coding, so please be gentle...
My goal is to have three buttons, each assigned to a color on a tricolored LED. When pressed, button 2 would fade in blue to full, and when release, blue would fade out. I would like each button to control the color independently of the other buttons, so blue could be fading in, while green fades out.
I have the led and the buttons working, and I run other samples that fade an LED in and out, but they don't suite my purpose.
The code I've written seems to return the correct values for the buttons, as well as the RGB values, based on what I see in the serial monitor, but my analogWrite statement seems to ignore them, leaving the LED off until it gets to 127 (or so), then turning it on full.
I've simplified the code with the idea that the LED slowly gets brighter, then turns off and restarts, but like I say, it's just off until the Blu value hits 127 or so, then it turns on full.
Thanks for any ideas that can help me along...
/*
* This program dims and LED, resets it, then dims it again
* Debugging code assumes Arduino 0004, as it uses Serial.begin()-style functions
* 470 Ohm between LEDS
* 10k Ohm between switches
*/
// INPUT: Digital switch should be connected to 5V via 10k Ohm resistor and GND
int s2Pin = 2; // digital pin 2
//int s3Pin = 3;
// OUTPUT: Use digital pins 9-11, the Pulse-width Modulation (PWM) pins
// LED's cathodes should be connected to digital GND
int redPin = 10; // Red LED, connected to digital pin 9
int grnPin = 11; // Green LED, connected to digital pin 10
int bluPin = 12; // Blue LED, connected to digital pin 11
// Program variables
// Value of 255 = OFF
int redVal = 255; // Variables to store the values to send to the pins
int grnVal = 255;
int bluVal = 255;
int s2Val = 0;
int s3Val = 0;
int DEBUG = 1; // Set to 1 to turn on debugging output
void setup()
{
pinMode(redPin, OUTPUT); // sets the pins as output
pinMode(grnPin, OUTPUT);
pinMode(bluPin, OUTPUT);
pinMode(s2Pin, INPUT);
//pinMode(s3Pin, INPUT);
if (DEBUG) { // If we want to see the pin values for debugging...
Serial.begin(9600); // ...set up the serial ouput in 0004 format
}
}
// Main program
void loop()
{
s2Val = digitalRead(s2Pin); // read input value
//s3Val = digitalRead(s3Pin); // read input value
if (s2Val == LOW) // Button is ON
{
if (bluVal > 0) // LED is NOT at full value of ZERO
{
bluVal--;
}
else
{
bluVal = 255;
}
}
analogWrite(bluPin, bluVal);
analogWrite(redPin, redVal); // Write values to LED pins
analogWrite(grnPin, grnVal);
DEBUG = 1; // Reset the counter
// Serial output using 0004-style functions
Serial.print("R:"); // Indicate that output is red value
Serial.print(redVal); // Print red value
Serial.print("\t"); // Print a tab
Serial.print("G:"); // Repeat for grn and blu...
Serial.print(grnVal);
Serial.print("\t");
Serial.print("B:");
Serial.print(bluVal); // println, to end with a carriage return
Serial.print("\t");
Serial.print("S2:"); // Indicate that output is red value
Serial.print(s2Val); // Print red value
Serial.print("\t"); // Print a tab
Serial.print("S3:"); // Indicate that output is red value
Serial.println(s3Val); // Print red value
}