#include "FastLED.h"
#define NUM_LEDS 10 // Number of LEDs used
#define PIN_LEDS 6 // Pin used for LEDs data
#define SWITCH 4 // Pin used for On/Off Switch
#define CONNECTED 2 // Pin used for NAS connection feedback from RaspberryPi
CRGB leds[NUM_LEDS];
const int speedDelay = 50;
const int startupColorR = 0xFF; // Start-up color, Red LED
const int startupColorG = 0xFF; // Start-up color, Green LED
const int startupColorB = 0xFF; // Start-up color, Blue LED
const int connectedColorR = 0xFF; // Connected color, Red LED
const int connectedColorG = 0xFF; // Connected color, Green LED
const int connectedColorB = 0xFF; // Connected color, Blue LED
const float increment = 0.006;
const float brightness = 1.8*PI; // Actual brightness used in functions; 1.5*PI is 0% and 2.5*PI is 100% brightness
int steps = 0;
void setup()
{
FastLED.addLeds<WS2811, PIN_LEDS, GRB>(leds, NUM_LEDS).setCorrection(TypicalLEDStrip);
pinMode(SWITCH,INPUT); // Input from selector switch
pinMode(CONNECTED,INPUT); // Input from RaspberryPi
setAll(0x00, 0x00, 0x00); // Starts with all turned off LEDs
}
void loop()
{
if (digitalRead(SWITCH) == HIGH)
{
switch (steps)
{
case 0: // STEP 1 : progressive start of the start-up color lighting
{
startLighting(startupColorR, startupColorG, startupColorB);
delay(20*speedDelay); // 1s delay
steps = 1;
break;
}
case 1: // STEP 2 : LEDs animation while RapberryPi Starts and connects to NAS server
{
if (digitalRead(CONNECTED) == LOW)
{
serverSearch(startupColorR, startupColorG, startupColorB, 10, true);
}
else
{
setAll(0x00,0x00,0x00);
delay(20*speedDelay); // 1s delay
steps = 2;
}
break;
}
case 2: // STEP 3 : progressive start of the connected color lighting
{
if (digitalRead(CONNECTED) == HIGH)
{
startedLighting(connectedColorR, connectedColorG, connectedColorB);
delay(20*speedDelay); // 1s delay
steps = 3;
}
else
{
steps = 1;
}
break;
}
case 3: // STEP 3 :steady connected color lighting while ready
{
if (digitalRead(CONNECTED) == HIGH)
{
steadyLighting(connectedColorR, connectedColorG, connectedColorB);
}
else
{
stopLighting(connectedColorR, connectedColorG, connectedColorB);
delay(20*speedDelay); // 1s delay
steps = 1;
}
break;
}
}
}
else
{
setAll(0x00, 0x00, 0x00);
steps = 0;
}
}
void setPixel(int Pixel, byte Red, byte Green, byte Blue) // Sets RGB colors to one LED from the strip
{
leds[Pixel].r = Red;
leds[Pixel].g = Green;
leds[Pixel].b = Blue;
}
void setAll(byte Red, byte Green, byte Blue) // This function sets all LEDs in the strip to their RGB colors
{
for(int i = 0; i < NUM_LEDS; i )
{
setPixel(i, Red, Green, Blue);
}
FastLED.show();
}
void startLighting(byte Red, byte Green, byte Blue)
{
float r, g, b;
for(float k = 1.5*PI; k < 2.3*PI; k=k increment)
{
r = (sin(k)*127.5 127.5)/256.0*Red;
g = (sin(k)*127.5 127.5)/256.0*Green;
b = (sin(k)*127.5 127.5)/256.0*Blue;
setAll(r, g, b);
FastLED.show();
}
delay(20*speedDelay); // 1s delay
for(float k = 2.3*PI; k >= 1.5*PI; k=k-2*increment)
{
r = (sin(k)*127.5 127.5)/256.0*Red;
g = (sin(k)*127.5 127.5)/256.0*Green;
b = (sin(k)*127.5 127.5)/256.0*Blue;
setAll(r, g, b);
FastLED.show();
}
}
void startedLighting(byte Red, byte Green, byte Blue)
{
float r, g, b;
for(float k = 1.5*PI; k < 2.3*PI; k=k increment)
{
r = (sin(k)*127.5 127.5)/256.0*Red;
g = (sin(k)*127.5 127.5)/256.0*Green;
b = (sin(k)*127.5 127.5)/256.0*Blue;
setAll(r, g, b);
FastLED.show();
}
delay(20*speedDelay); // 1s delay
for(float k = 2.3*PI; k >= brightness; k=k-2*increment)
{
r = (sin(k)*127.5 127.5)/256.0*Red;
g = (sin(k)*127.5 127.5)/256.0*Green;
b = (sin(k)*127.5 127.5)/256.0*Blue;
setAll(r, g, b);
FastLED.show();
}
}
void steadyLighting(byte Red, byte Green, byte Blue)
{
float r, g, b;
float k = brightness;
r = (sin(k)*127.5 127.5)/256.0*Red;
g = (sin(k)*127.5 127.5)/256.0*Green;
b = (sin(k)*127.5 127.5)/256.0*Blue;
setAll(r, g, b);
FastLED.show();
}
void stopLighting(byte Red, byte Green, byte Blue)
{
float r, g, b;
for(float k = brightness; k >= 1.5*PI; k=k-2*increment)
{
r = (sin(k)*127.5 127.5)/256.0*Red;
g = (sin(k)*127.5 127.5)/256.0*Green;
b = (sin(k)*127.5 127.5)/256.0*Blue;
setAll(r, g, b);
FastLED.show();
}
}
void serverSearch(byte Red, byte Green, byte Blue, int Count, boolean OnlyOne)
{
for(int i = 0; i < NUM_LEDS-3; i )
{
setAll(0x00,0x00,0x00);
for (int i = 0; i < Count; i )
{
setPixel(random(NUM_LEDS),Red,Green,Blue);
FastLED.show();
delay(1.5*speedDelay);
if(OnlyOne)
{
setAll(0x00,0x00,0x00);
}
}
}
}
|