These things have been vexing me the whole semester. Especially when I went back to make 2-6 Nightlight work. (also temperature and processing sketches)
I would wire my LED just like in the diagrams; Only some of the colors would work, others would just be off.
I did some research and found out that RGB LEDs come in common cathode and common anode varieties.
The wiring diagram is for a common cathode type LED. The second pin in this type goes to ground. This is not the type that I have. I theorized that I might have the other type, so I hooked up the second pin to 5V instead and it worked for the whole cycle! Except that the colors weren't what they were supposed to be.
That's because with a common anode the RGB pins in the code should be LOW not HIGH like it is for common cathode. So you can go back in the code and switch all the modes. For the ones that use numbers, switch 127 and 0, and for white, make them all 0. (see working code below)
A quick test to see if your LED is common anode like mine was: Connect the longest pin straight to 5V and use the resistor to make the RGB legs light up individually:
This is my altered Nightlight code under read more:
const int BLED=9; //Blue LED on Pin 9
const int GLED=10; //Green LED on Pin 10
const int RLED=11; //Red LED on Pin 11
const int BUTTON=2; //The Button is connected to pin 2
boolean lastButton = LOW; //Last Button State
boolean currentButton = LOW; //Current Button State
int ledMode = 0; //Cycle between LED states
void setup()
{
pinMode (BLED, OUTPUT); //Set Blue LED as Output
pinMode (GLED, OUTPUT); //Set Green LED as Output
pinMode (RLED, OUTPUT); //Set Red LED as Output
pinMode (BUTTON, INPUT); //Set button as input (not required)
}
/*
* Debouncing Function
* Pass it the previous button state,
* and get back the current debounced button state.
*/
boolean debounce(boolean last)
{
boolean current = digitalRead(BUTTON); //Read the button state
if (last != current) //if it's different...
{
delay(5); //wait 5ms
current = digitalRead(BUTTON); //read it again
}
return current; //return the current value
}
/*
* LED Mode Selection
* Pass a number for the LED state and set it accordingly.
*/
void setMode(int mode)
{
//RED
if (mode == 1)
{
digitalWrite(RLED, LOW);
digitalWrite(GLED, HIGH);
digitalWrite(BLED, HIGH);
}
//GREEN
else if (mode == 2)
{
digitalWrite(RLED, HIGH);
digitalWrite(GLED, LOW);
digitalWrite(BLED, HIGH);
}
//BLUE
else if (mode == 3)
{
digitalWrite(RLED, HIGH);
digitalWrite(GLED, HIGH);
digitalWrite(BLED, LOW);
}
//PURPLE (RED+BLUE)
if (mode == 4)
{
digitalWrite(RLED, 0);
digitalWrite(GLED, 127);
digitalWrite(BLED, 0);
}
//TEAL (BLUE+GREEN)
else if (mode == 5)
{
digitalWrite(RLED, 127);
digitalWrite(GLED, 0);
digitalWrite(BLED, 0);
}
//ORANGE (GREEN+RED)
else if (mode == 6)
{
digitalWrite(RLED, 0);
digitalWrite(GLED, 0);
digitalWrite(BLED, 127);
}
//WHITE (GREEN+RED+BLUE)
else if (mode == 7)
{
digitalWrite(RLED, 0);
digitalWrite(GLED, 0);
digitalWrite(BLED, 0);
}
//OFF (mode = 0)
else
{
digitalWrite(RLED, HIGH);
digitalWrite(GLED, HIGH);
digitalWrite(BLED, HIGH);
}
}
void loop()
{
currentButton = debounce(lastButton); //read deboucned state
if (lastButton == LOW && currentButton == HIGH) //if it was pressed...
{
ledMode++; //increment the LED value
}
lastButton = currentButton; //reset button value
//if you've cycled through the different options,
//reset the counter to 0
if (ledMode == 8) ledMode = 0;
setMode(ledMode); //change the LED state
}
No comments:
Post a Comment