Skip to content
This repository has been archived by the owner on Jan 14, 2019. It is now read-only.

Commit

Permalink
Updated to fade LEDs not jump to new colours.
Browse files Browse the repository at this point in the history
  • Loading branch information
thiseldo committed May 26, 2011
1 parent 10b6e74 commit 1db00e4
Showing 1 changed file with 47 additions and 1 deletion.
48 changes: 47 additions & 1 deletion examples/EtherShield_PachubeRGB/EtherShield_PachubeRGB.pde
Expand Up @@ -39,6 +39,10 @@ static uint8_t gwip[4] = { 0,0,0,0 };
static uint8_t dnsip[4] = { 0,0,0,0 };
static uint8_t dhcpsvrip[4] = { 0,0,0,0 };

int currentRed = 0;
int currentGreen = 0;
int currentBlue = 0;

//====================================================================
// Pachube declarations
//====================================================================
Expand Down Expand Up @@ -121,11 +125,53 @@ void browserresult_callback(uint8_t statuscode,uint16_t datapos){
#endif

// Set the RGB LEDS
solid( red, green, blue, 0 );
fadeTo( red, green, blue );
}
}
}

//function fades existing values to new RGB values
void fadeTo(int r, int g, int b)
{
//map values
r = map(r, 0, 255, 0, 255);
g = map(g, 0, 255, 0, 255);
b = map(b, 0, 255, 0, 255);

//output
fadeToColour( REDPIN, currentRed, r );
fadeToColour( GREENPIN, currentGreen, g );
fadeToColour( BLUEPIN, currentBlue, b );

currentRed = r;
currentGreen = g;
currentBlue = b;
}

// Fade a single colour
void fadeToColour( int pin, int fromValue, int toValue ) {
int increment = (fromValue > toValue ? -1 : 1 );
int startValue = (fromValue > toValue ? : 1 );

if( fromValue == toValue )
return; // Nothing to do!

if( fromValue > toValue ) {
// Fade down
for( int i = fromValue; i >= toValue; i += increment ) {
analogWrite( pin, i );
delay(10);
}
} else {
// Fade up
for( int i = fromValue; i <= toValue; i += increment ) {
analogWrite( pin, i );
delay(10);
}
}
}


//function holds RGB values for time t milliseconds, mainly for demo
void solid(int r, int g, int b, int t)
{
Expand Down

0 comments on commit 1db00e4

Please sign in to comment.