RGB LED Animations Using PWM

/*
 * rgbClock.pde
 * 
 * Copyright 2012 Bill Heaster <TheCreator at apexlogic d0t net>
 * 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 * MA 02110-1301, USA.
 * 
 * 
 */


//pins
const int red = 9;
const int blue = 10;
const int green = 11;




void setup ()
{
	pinMode(red, OUTPUT);
	pinMode(blue, OUTPUT);
	pinMode(green, OUTPUT);
	
	digitalWrite(red, HIGH);
	digitalWrite(blue, HIGH);
	digitalWrite(green, HIGH);
		
	
}

void loop ()
{
	
  slowCycle();
  fastCycle();
  pulse();
  heartBeat();
	
	
}

void fadeOut(int color, int myStep)
{
	int cnt;
	for(cnt = 0; cnt<255; cnt+=myStep)
	{
			if(cnt>255)
		{
			cnt = 255;
		}
		
		analogWrite(color, cnt);
		delay(15);
	}
	return;
}

void fadeIn(int color, int myStep)
{
	int cnt;
	for(cnt = 255; cnt>0; cnt-=myStep)
	{
		if(cnt<0)
		{
			cnt = 0;
		}
		
		analogWrite(color, cnt);
		delay(15);
	}
	return;
}

void slowCycle()
{
	int myStep = 2;
	
	fadein(red, myStep);
	fadein(blue, myStep);
	fadeOut(red, myStep);
	fadein(green, myStep);
	fadeOut(blue, myStep);
	fadein(red, myStep);
	fadeOut(green,myStep);
	fadeOut(red,myStep);
	
	return;
}

void fastCycle()
{
	int myStep = 10;
	
	fadein(red, myStep);
	fadein(blue, myStep);
	fadeOut(red, myStep);
	fadein(green, myStep);
	fadeOut(blue, myStep);
	fadein(red, myStep);
	fadeOut(green,myStep);
	fadeOut(red,myStep);
	
	return;	
}

void pulse()
{
	int myStepIn = 5;
	int myStepOut = 25;
	
	fadein(red, myStepIn);
	fadein(blue, myStepIn);
	fadeOut(red, myStepOut);
	fadein(green, myStepIn);
	fadeOut(blue, myStepOut);
	fadein(red, myStepIn);
	fadeOut(green,myStepOut);
	fadeOut(red,myStepOut);	
}

void heartBeat()
{
   	int myStepIn = 25;
	int myStepOut = 2;
	
	fadein(red, myStepIn);
	fadeOut(red, myStepOut);

	fadein(blue, myStepIn);
  	fadeOut(blue, myStepOut);

	fadein(green, myStepIn);
	fadeOut(green,myStepOut);

	
	fadein(red, myStepIn);
	fadein(blue, myStepIn);
	fadeOut(red, myStepOut);
  	fadeOut(blue, myStepOut);

	fadein(green, myStepIn);
	fadein(red, myStepIn);
  	fadeOut(red, myStepOut);
	fadeOut(green,myStepOut);

	fadein(blue, myStepIn);
	fadein(green, myStepIn);
  	fadeOut(blue, myStepOut);
	fadeOut(green,myStepOut);

}

you may note that fade in and fade out don’t increment how you think they would. The reason for this is because of the PNP transistors that were being used for this project. If you are connecting an RGB LED directly to your arduino board simply change the function decleration name from void Fadein to void FadeOut; and vice versa.

This video is an example of the above code.

clock from Apex Logic on Vimeo.

Leave a Reply