Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
LED Tree
/**************** LED Tree Software **************/
/* This Software does SW PWM with 9 Pins. */
/* If you connet LEDs with Anode, you will get */
/* a nice sparkle effect ;-) */
/* */
/* Autor: Stefan-Xp */
/* Version History: */
/* - 2014-12-04 Created this sparkely thing */
/* from LED_Kerze_V3 */
#define LED_PIN_1 24
#define LED_PIN_2 25
#define LED_PIN_3 26
#define LED_PIN_4 27
#define LED_PIN_5 28
#define LED_PIN_6 29
#define LED_PIN_7 30
#define LED_PIN_8 31
#define LED_PIN_9 23
#define LED_PIN_10 22
#define LED_PIN_11 19 // Some kind of overhead, but i like it
#define BRIGHTNESS_LOW 30 // Use Values greater than 0
#define BRIGHTNESS_HIGH 250 // Use Values lower than 255
#define SPARKLE_SPEED 200; // This Value determines how slow the LEDs sparkle (the bigger the slower)
#define LED_VALUE_ON HIGH
#define LED_VALUE_OFF LOW
byte SoftPWMValues[9];
byte LED_Pins[9];
byte Sparkle[9];
int FrameCount;
int TickCount;
void setup()
{
byte iCount;
LED_Pins[0] = LED_PIN_1;
LED_Pins[1] = LED_PIN_2;
LED_Pins[2] = LED_PIN_3;
LED_Pins[3] = LED_PIN_4;
LED_Pins[4] = LED_PIN_5;
LED_Pins[5] = LED_PIN_6;
LED_Pins[6] = LED_PIN_7;
LED_Pins[7] = LED_PIN_8;
LED_Pins[8] = LED_PIN_9;
for(iCount = 0; iCount<=8; iCount++)
{
pinMode(LED_Pins[iCount], OUTPUT);
Sparkle[iCount] = random(0,256);
}
}
void loop()
{
byte iCount;
// if every PWM Value is passed, activate all LEDs
if(TickCount == 0)
{
digitalWrite(LED_Pins[0], LED_VALUE_ON);
digitalWrite(LED_Pins[1], LED_VALUE_ON);
digitalWrite(LED_Pins[2], LED_VALUE_ON);
digitalWrite(LED_Pins[3], LED_VALUE_ON);
digitalWrite(LED_Pins[4], LED_VALUE_ON);
digitalWrite(LED_Pins[5], LED_VALUE_ON);
digitalWrite(LED_Pins[6], LED_VALUE_ON);
digitalWrite(LED_Pins[7], LED_VALUE_ON);
digitalWrite(LED_Pins[8], LED_VALUE_ON);
}
else
{
if(SoftPWMValues[0]<TickCount){digitalWrite(LED_Pins[0], LED_VALUE_OFF);};
if(SoftPWMValues[1]<TickCount){digitalWrite(LED_Pins[1], LED_VALUE_OFF);};
if(SoftPWMValues[2]<TickCount){digitalWrite(LED_Pins[2], LED_VALUE_OFF);};
if(SoftPWMValues[3]<TickCount){digitalWrite(LED_Pins[3], LED_VALUE_OFF);};
if(SoftPWMValues[4]<TickCount){digitalWrite(LED_Pins[4], LED_VALUE_OFF);};
if(SoftPWMValues[5]<TickCount){digitalWrite(LED_Pins[5], LED_VALUE_OFF);};
if(SoftPWMValues[6]<TickCount){digitalWrite(LED_Pins[6], LED_VALUE_OFF);};
if(SoftPWMValues[7]<TickCount){digitalWrite(LED_Pins[7], LED_VALUE_OFF);};
if(SoftPWMValues[8]<TickCount){digitalWrite(LED_Pins[8], LED_VALUE_OFF);};
}
if(TickCount > 254)
TickCount = 0;
else
TickCount++;
// Count Dowb to update the Brigthness Values
FrameCount--;
// Done, now adjust the Brigthness Values
if(FrameCount <= 0)
{
// For each LED ...
for(iCount = 0; iCount<=8; iCount++)
{
// If the Sparkle Value is higher than the brigthness, increase brightness
if(Sparkle[iCount] > SoftPWMValues[iCount])
SoftPWMValues[iCount]++;
else
{
// if the Sparkle Value is lower than the brightness, keep it as that until next "sparkle"
Sparkle[iCount]--;
SoftPWMValues[iCount]--;
}
}
// This Value determines how slow the LEDs sparkle
FrameCount = SPARKLE_SPEED;
// check if LEDs need to sparkle again from start value
for(iCount = 0; iCount<=8; iCount++)
{
if(Sparkle[iCount] <= BRIGHTNESS_LOW) // this is the lowes desired brightness
Sparkle[iCount] = BRIGHTNESS_HIGH; // this is the highest desired brightness
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.