Hello. I'm building a RGB controller using an Arduino. It will have several buttons and sliding pots for effect control. I hooked up a RGB LED on pins 9,10 and 11 and 3 pushbuttons to turn them on and off. So far it is working, here is the code:
/*
Set a state of a variable when you press a pushbutton ( the
button went from off to on ).
created 01-12-2009 by kasperkamperman.com
based on example 'State change detection' by Tom Igoe
RGB LED on-off
*/
const int buttonRed = 2; // the pin that the Red pushbutton is attached to
const int buttonGreen = 3; // the pin that the Green pushbutton is attached to
const int buttonBlue = 4; // the pin that the Blue pushbutton is attached to
const int Red = 9; // the pin that the Red LED is attached to
const int Green = 10; // the pin that the Green LED is attached to
const int Blue = 11; // the pin that the Blue LED is attached to
int RedbuttonState = 0; // current state of the Red button
int lastRedButtonState = 0; // previous state of the Red button
int RedledState = 0; // remember current Red LED state
int BluebuttonState = 0; // current state of the blue button
int lastBlueButtonState = 0; // previous state of the blue button
int BlueledState = 0; // remember current blue LED state
int GreenbuttonState = 0; // current state of the green button
int lastGreenButtonState = 0; // previous state of the green button
int GreenledState = 0; // remember current green LED state
void setup() {
pinMode(buttonRed, INPUT); // initialize the button pin as a input
pinMode(buttonGreen, INPUT); // initialize the button pin as a input
pinMode(buttonBlue, INPUT); // initialize the button pin as a input
pinMode(Red, OUTPUT); // initialize the RED LED pin as a output
pinMode(Green, OUTPUT); // initialize the GREEN LED pin as a output
pinMode(Blue, OUTPUT); // initialize the BLUE LED pin as a output
}
void loop() {
// read the pushbutton input pin
RedbuttonState = digitalRead(buttonRed);
GreenbuttonState = digitalRead(buttonGreen);
BluebuttonState = digitalRead(buttonBlue);
// check if the button is pressed or released
// by comparing the buttonState to its previous state
if (RedbuttonState != lastRedButtonState) {
// change the state of the led when someone pressed the button
if (RedbuttonState == 1) {
if(RedledState==1) RedledState=0;
else RedledState=1;
}
// remember the current state of the button
lastRedButtonState = RedbuttonState;
}
if (GreenbuttonState != lastGreenButtonState) {
if (GreenbuttonState == 1) {
if(GreenledState==1) GreenledState=0;
else GreenledState=1;
}
lastGreenButtonState = GreenbuttonState;
}
if (BluebuttonState != lastBlueButtonState) {
if (BluebuttonState == 1) {
if(BlueledState==1) BlueledState=0;
else BlueledState=1;
}
lastBlueButtonState = BluebuttonState;
}
// turns LED on if the ledState=1 or off if the ledState=0
digitalWrite(Red, RedledState);
digitalWrite(Green, GreenledState);
digitalWrite(Blue, BlueledState);
// adding a small delay prevents reading the buttonState too fast
// ( debouncing )
delay(20);
}
Next step is add 3 more buttons, each of them will light a secondary color (cyan, yellow, magenta), that means each of this new buttons must turn 2 LEDs on and off at the same time. I can get those colors already by simply by pushing 2 buttons and leaving them on, but I really need another button to make the color easier to spot on the controller. How can I accomplish that ? I'm not really good with code. Thanks in adv.
if (RedbuttonState != lastRedButtonState) {
// change the state of the led when someone pressed the button
if (RedbuttonState == 1) {
if(RedledState==1) RedledState=0;
else RedledState=1;
}
// remember the current state of the button
lastRedButtonState = RedbuttonState;
}
You can do:
if (RedbuttonState != lastRedButtonState) {
// change the state of the led when someone pressed the button
if (RedbuttonState == 1)
RedledState = !RedledState; // toggle RedledState
// remember the current state of the button
lastRedButtonState = RedbuttonState;
}
As for cyan, etc. you have have states for them as well (like R, G, B). Then when you go to change the LEDs themselves, something like:
// turns LED on if the ledState=1 or off if the ledState=0
digitalWrite(Red, RedledState);
digitalWrite(Green, GreenledState || CyanledState);
digitalWrite(Blue, BlueledState || CyanledState);
In other words, you turn on green if either green or cyan is wanted.
Great! Thank you. I got it working with buttons for red, green, blue, yellow, magenta, cyan, black and white! Here is the code:
const int buttonRed = 2; // the pin that the Red pushbutton is attached to
const int buttonGreen = 3; // the pin that the Green pushbutton is attached to
const int buttonBlue = 4; // the pin that the Blue pushbutton is attached to
const int buttonYellow = 5; // the pin that the Yellow pushbutton is attached to
const int buttonMagenta = 6; // the pin that the Magenta pushbutton is attached to
const int buttonCyan = 7; // the pin that the Cyan pushbutton is attached to
const int buttonWhite = 8; // the pin that the White pushbutton is attached to
const int buttonBlack = 13; // the pin that the Black pushbutton is attached to
const int buttonEFFECT1 = 1; // the pin that the EFFECT1 pushbutton is attached to
const int chave = 12; // switch, "forcing" color
const int Red = 11; // the pin that the Red LED is attached to
const int Green = 9; // the pin that the Green LED is attached to
const int Blue = 10; // the pin that the Blue LED is attached to
int redButtonState = LOW; // current state of the Red button
int blueButtonState = LOW; // current state of the Blue button
int greenButtonState = LOW; // current state of the Green button
int yellowButtonState = LOW; // current state of the Yellow button
int magentaButtonState = LOW; // current state of the Magenta button
int cyanButtonState = LOW; // current state of the Cyan button
int whiteButtonState = LOW; // current state of the White button
int blackButtonState = LOW; // current state of the Black button
int EFFECT1ButtonState = LOW; // current state of the EFFECT1 button
void setup() {
pinMode(buttonRed, INPUT); // initialize the button pin as an input
pinMode(buttonGreen, INPUT); // initialize the button pin as an input
pinMode(buttonBlue, INPUT); // initialize the button pin as an input
pinMode(buttonYellow, INPUT); // initialize the button pin as an input
pinMode(buttonMagenta, INPUT); // initialize the button pin as an input
pinMode(buttonCyan, INPUT); // initialize the button pin as an input
pinMode(buttonWhite, INPUT); // initialize the button pin as an input
pinMode(buttonBlack, INPUT); // initialize the button pin as an input
pinMode(buttonEFFECT1, INPUT); // initialize the button pin as an input
pinMode(chave, INPUT); // initialize the switch pin as an input
pinMode(Red, OUTPUT); // initialize the RED LED pin as an output
pinMode(Green, OUTPUT); // initialize the GREEN LED pin as an output
pinMode(Blue, OUTPUT); // initialize the BLUE LED pin as an output
}
void loop() {
if (digitalRead(buttonRed) == HIGH) {
redButtonState = !redButtonState;
while (digitalRead(buttonRed) == HIGH) {}
if (digitalRead(chave)) {
redButtonState = HIGH;
greenButtonState = LOW;
blueButtonState = LOW;
}
}
if (digitalRead(buttonGreen) == HIGH) {
greenButtonState = !greenButtonState;
while (digitalRead(buttonGreen) == HIGH) {}
if (digitalRead(chave)) {
redButtonState = LOW;
greenButtonState = HIGH;
blueButtonState = LOW;
}
}
if (digitalRead(buttonBlue) == HIGH) {
blueButtonState = !blueButtonState;
while (digitalRead(buttonBlue) == HIGH) {}
if (digitalRead(chave)) {
redButtonState = LOW;
greenButtonState = LOW;
blueButtonState = HIGH;
}
}
if (digitalRead(buttonYellow) == HIGH) {
greenButtonState = !greenButtonState;
redButtonState = !redButtonState;
while (digitalRead(buttonYellow) == HIGH) {}
if (digitalRead(chave)) {
redButtonState = HIGH;
greenButtonState = HIGH;
blueButtonState = LOW;
}
}
if (digitalRead(buttonMagenta) == HIGH) {
blueButtonState = !blueButtonState;
redButtonState = !redButtonState;
while (digitalRead(buttonMagenta) == HIGH) {}
if (digitalRead(chave)) {
redButtonState = HIGH;
greenButtonState = LOW;
blueButtonState = HIGH;
}
}
if (digitalRead(buttonCyan) == HIGH) {
blueButtonState = !blueButtonState;
greenButtonState = !greenButtonState;
while (digitalRead(buttonCyan) == HIGH) {}
if (digitalRead(chave)) {
redButtonState = LOW;
greenButtonState = HIGH;
blueButtonState = HIGH;
}
}
if (digitalRead(buttonWhite) == HIGH) {
blueButtonState = !blueButtonState;
greenButtonState = !greenButtonState;
redButtonState = !redButtonState;
while (digitalRead(buttonWhite) == HIGH) {}
if (digitalRead(chave)) {
redButtonState = HIGH;
greenButtonState = HIGH;
blueButtonState = HIGH;
}
}
if (digitalRead(buttonBlack) == HIGH) {
blueButtonState = !blueButtonState;
greenButtonState = !greenButtonState;
redButtonState = !redButtonState;
while (digitalRead(buttonBlack) == HIGH) {}
if (digitalRead(chave)) {
redButtonState = LOW;
greenButtonState = LOW;
blueButtonState = LOW;
}
}
digitalWrite(Red, redButtonState);
digitalWrite(Green, greenButtonState);
digitalWrite(Blue, blueButtonState);
}
As you can see I've added a EFFECT1 button, that will strobe the red LED, but I need help implementing that. I know I have to use millis and create some sort of interval for the effect to happen, but that's pretty much it,.. any help would be great, thanks in advance,
That forces the Red LED off if the low-order 7 bits of the millis time are < 63. Effectively this makes the LED go off every 64ms seconds and then on for 64ms, giving a frequency of 7.8Hz.
You can play with the figures to give different rates of flashing.