Beyond the Blink: RGB LED multiple lighting effects with arduino, push button, and potentiometer

Ever want to do more with an LED than just blink? This circuit and source code demonstrates some creative possibilities for lighting effects with an RGB LED.

CC has descriptive subtitles. Sorry for the poor phone video quality.

Here’s a circuit diagram (for common anode LED):

Here’s the source code:

/*
Blink_Shift
version 9.1 / September 2017

Written by Joe Schembrie aka Engineer Zero.
In the public domain.
No attribution necessary.
Alter as you see fit.

Controls an RGB LED in multiple modes. A push button changes
the mode and a potentiometer is used to adjust intensity,
rate, and color.

***IMPORTANT ByTheWay:

I used a common anode RGB, for which
analogWrite() goes HIGH for 0 and LOW for 255. You'll have
to revise your code accordingly if you use a common cathode RGB.

Lighting Mode Descriptions:

0 OFF
1 Candle
2 Red
3 Blue
4 Green
5 Tricolor
6 Strobe
7 Pure White
8 Broken Light
9 Yellow flashing (caution)
10 Psychedelic
11 Pulse

See my blog, http://engineerzero.blog for more fun stuff.

*/

// here are the pin number assignments

int blue = 11;
int green = 10;
int red = 9;
int potpin = 3; // the pin the potentiometer is on
int button = 7; // the pin the button is on.

// various variables

int potval = 0; // adjusts for magnitude and rate
int choice = 0; // your mode selection

// these 'prev' values are for the psychedelic choice

long rprev = 128;
long gprev = 128;
long bprev = 128;

void setup() {

// initialize the pwm pins as outputs.
pinMode(red, OUTPUT);
pinMode(green, OUTPUT);
pinMode(blue, OUTPUT);
pinMode(button,INPUT);

analogWrite(red,255);
analogWrite(green,255);
analogWrite(blue,255);

// seed the random number generator
randomSeed(100);

}

void loop() {

// read the pushbutton to see if it's been pushed
int val;
val = digitalRead(button);

// val = HIGH means the button has been pushed
// and the mode is to be changed.

// SELECT CHOICE //////////////////////////////////////////////////
if(val==HIGH){
choice = choice + 1;

analogWrite(blue,255);
analogWrite(green,255);
analogWrite(red,255);
delay(500);

// indicate mode transition with blue flashes
for(int i = 0; i<5; i++){
analogWrite(blue,125);
delay(20);
analogWrite(blue,255);
delay(40);
}

delay(500);

// change this if you want more modes

if (choice == 12){
choice = 0;
}
}

//TIME TO LIFT BUTTON //////////////////////////////
// (for debounce)

while (val == HIGH){
delay(100);
val = digitalRead(button);
}

//////////////////////////////

// read the potentiometer value
potval = analogRead(potpin);

// OFF
if(choice==0){
analogWrite(red,255);
analogWrite(green,255);
analogWrite(blue,255);
}

// CANDLE FLAME
if(choice==1){
flicker();
}

// RED

if(choice==2){
analogWrite(red,255 - potval/4);
analogWrite(green,255);
analogWrite(blue,255);

}

// GREEN
if(choice==3){
analogWrite(red,255);
analogWrite(green,255 - potval/4);
analogWrite(blue,255);
}

// BLUE

if(choice==4){
analogWrite(red,255);
analogWrite(green,255);
analogWrite(blue,255 - potval/4);
}

// tricolor
if(choice==5){
analogWrite(red,255);
analogWrite(blue,255);
analogWrite(green,255);
int tricolor;
for(int j = 0; j<4; j++){
if(j==0){
tricolor = red;
}
if(j==1){
tricolor = green;
}
if(j==2){
tricolor = blue;
}
analogWrite(tricolor,0);
delay(522-potval/2);
analogWrite(tricolor,255);
delay(522-potval/2);

}

} // end of tricolor

// STROBE

if(choice==6){
analogWrite(red,0);
analogWrite(green,0);
analogWrite(blue,0);
delay(10);
analogWrite(red,255);
analogWrite(green,255);
analogWrite(blue,255);
delay(1023-potval);
}

//this is white
if(choice==7){
analogWrite(red,255 - potval/4);
analogWrite(green,255 - potval/4);
analogWrite(blue,255 - potval/4);
}

// Oh no, your light is broken!
if(choice==8){
int mag8 = random(0,255);
analogWrite(red,mag8);
analogWrite(green,mag8);
analogWrite(blue,mag8);
int d8 = random(0,100);
delay(d8);
if(random(0,30)==0){
analogWrite(red,255);
analogWrite(green,255);
analogWrite(blue,255);
delay(500);
}
}

// Caution-warning yellow flashing light
if(choice==9){
analogWrite(red,0);
analogWrite(green,125);
analogWrite(blue,255);
delay(100);
analogWrite(red,255);
analogWrite(green,255);
analogWrite(blue,255);
delay(1200);

}

// Psychedelic
if(choice==10){

// When tweaking code, careful with variable types.

long rnew = random(256);
long gnew = random(256);
long bnew = random(256);
long rval;
long gval;
long bval;

for(int i; i<101; i++){
rval = rprev + long(i *(rnew - rprev)/100);
analogWrite(red,rval);
delay(5);
}
for(int i; i<101; i++){
gval = gprev + long(i *(gnew - gprev)/100);
analogWrite(green,gval);
delay(5);
}
for(int i; i300){
p = blue;
}
if(potval>600){
p = green;
}
for(int i = 0; i<256; i++){
analogWrite(p,255-i);
delay(4);
}
for(int i = 0; i<256; i++){
analogWrite(p,i);
delay(4);
}
delay(500);

} // end of choice 11

} // end of loop, loop, loopy-doop!

////////////////////////////////////////////////////////////

void flicker(){

// makes a flickering candle light

int k = random(0,100);
potval = analogRead(potpin);
float p = float(potval)/1023;

int rmag = 255 - (15 + 155 * k/100)*p;
int gmag = 255 - (10 + 55 * k/100)*p;

analogWrite(red,rmag);
analogWrite(green,gmag);

int d = 50+300 * random(0,100)/300;

delay(d);

}

////////////////////////////////////////////////

void tricolor(){

// transitions between red, blue, and green lights

analogWrite(red,255);
analogWrite(blue,255);
analogWrite(green,255);

analogWrite(red,0);
delay(500);
analogWrite(red,255);
delay(500);

analogWrite(green,0);
delay(500);
analogWrite(green,255);
delay(500);

analogWrite(blue,0);
delay(500);
analogWrite(blue,255);
delay(500);

}

Advertisements

About engineerzero

Once and future engineer.
This entry was posted in Uncategorized and tagged , , , , , , , , . Bookmark the permalink.

One Response to Beyond the Blink: RGB LED multiple lighting effects with arduino, push button, and potentiometer

  1. engineerzero says:

    Oh, yeah. I forgot. I’m supposed to promote my books. You may enjoy reading my science fiction trilogy, starting with The Wizard From Earth at https://www.amazon.com/Wizard-Earth-Star-Wizards-Trilogy-ebook/dp/B00O5980ZY/ref=sr_1_1?ie=UTF8&qid=1505164479&sr=8-1&keywords=the+wizard+from+earth

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s