This was part of a student lecture in the computation class. We discussed PWM and applying it to multiple LED’s and tri-color LED’s. Here’s pcitures and code for the 2 circuits I built and presented in class.

Multiple LED’s with PWM

int value = 0;              // variable to keep the actual value
int pins[] = {9, 10, 11};   // light connected to digital pin 9
int numPins = 3;

void setup() {				// nothing for setup
}

void loop() {
	int i;
	for (i=0; i < numPins; i++) {
		for(value = 0 ; value <= 255; value+=5) // fade in (from min to max)
		{
			analogWrite(pins&#91;i&#93;, value);      // sets the value (range from 0 to 255)
			delay(30);              // waits for 30 milli seconds to see the dimming effect
		}
	}

	int j;
	for (j=numPins-1; j >= 0; j–) 	{
		for(value = 255; value >=0; value-=5)  // fade out (from max to min)
		{
			analogWrite(pins[j], value);
			delay(30);
		}
	}
}

Tri-Color LED

// light connected to digital pin 9, 10, 11
int pins[] = {9, 10, 11};

void setup() { // nothing for setup
}

void loop() { // variable to keep the actual value
int value = 0;

//variables to check if fade in for a color is complete
int fadedIn1 = 0;
int fadedIn2 = 0;
int fadedIn3 = 0;

//variables to check if fade out for a color is complete
int fadedOut1 = 0;
int fadedOut2 = 0;
int fadedOut3 = 0;

//fade in blue
if (fadedIn1 != 1){
for(value = 255; value >=0; value-=5)
{
analogWrite(pins[1], value);
delay(60);
fadedIn1 = 1;
}
}

//fade in green
if (fadedIn2 != 1){
for(value = 255; value >=0; value-=5)
{
analogWrite(pins[0], value);
delay(60);
fadedIn2 = 1;
}
}

//fade in red
if (fadedIn3 != 1){
for(value = 255; value >=0; value-=5)
{
analogWrite(pins[2], value);
delay(60);
fadedIn3 = 1;
}
}

//fade out red
if (fadedOut1 != 1){
for(value = 0; value <=255; value+=5) { analogWrite(pins[2], value); delay(60); fadedOut1 = 1; } } //fade out green if (fadedOut2 != 1){ for(value = 0; value <=255; value+=5) { analogWrite(pins[0], value); delay(60); fadedOut2 = 1; } } //fade out blue if (fadedOut3 != 1){ for(value = 0; value <=255; value+=5) { analogWrite(pins[1], value); delay(60); fadedOut3 = 1; } } }[/code]