Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "FastLED/FastLED.h"
- FASTLED_USING_NAMESPACE;
- /*******************************************************************************
- * GROUND code
- * minimalistic colour animation based on hex colour palette
- * animation is affected by external "ping" (groundPingOnChange)
- *
- * Publishes: NA
- * Subscribes: ping
- *
- * Setters:
- * - set_bright(String val)
- * - set_speed(String val)
- * Convention for setters, return 1 if successful, -1 otherwise
- *
- * Built for Photon, depends on FastLED library
- *
- *******************************************************************************/
- // KEEP HANDY
- // https://docs.particle.io/support/troubleshooting/troubleshooting-support/photon/
- #define LED_PIN D6
- #define STATUS_LED D7
- #define NUM_LEDS (2 + 2) + (12 +12) + (20 + 20) + (30 + 30) + (38 + 38) + (46 + 46) + 56
- #define COLOR_ORDER GRB
- #define BRIGHTNESS 64
- #define LED_TYPE WS2812B
- #define TIMEOUT 5000
- /* Initialize Variables */
- int UPDATES_PER_SECOND = 40; // refresh rate
- int fluidity; // affects animation
- int palette;
- /* Debug */
- int signalStrength;
- /* Initialize LED objects */
- CRGBPalette16 black, currentPalette, targetPalette;
- TBlendType currentBlending;
- CRGB leds[NUM_LEDS];
- int block1 = 7;
- int block2 = 13;
- int block3 = 17;
- int block4 = 21;
- CRGB colours[] = { 0x2F00D0, 0x5500AB, 0x84007C, 0xB5004B, 0xAB1A25, 0xC2003E, 0x8F0071,
- // br violet, violet pink lilac lilac l purple purple
- 0xE5001B, 0xE81700, 0xB84700, 0xAB7700, 0xABAB00, 0xD97925,
- // red/or d amber amber straw red pal sun
- 0xAB5500, 0xDD2200, 0xF2000E, 0x0007F9,
- // sunset vivid su d sunset magenta
- 0x5F00A1, 0x2F00D0, 0x00FF9E, 0x27FF6A
- // d blue bri blue aqua turquoise
- };
- SYSTEM_MODE(AUTOMATIC); // this means I will be able to connect manually
- SYSTEM_THREAD(ENABLED); // this means my code runs in a separate thread form particle stuff
- STARTUP(WiFi.selectAntenna(ANT_AUTO)); // continually switches at high speed between antennas
- void setup() {
- /* Attempt connecting to network */
- waitFor(Particle.connected, TIMEOUT); //it will follow otherwise
- /* Declare and initialize indicator LED */
- pinMode(STATUS_LED, OUTPUT);
- pinResetFast(STATUS_LED);
- /* Setup AUX */
- fluidity = 1;
- palette = 0; // 0 - Black 1 - Normal - 2 Generated
- /* Declare particle variables and functions */
- Particle.variable( "signal", &signalStrength, INT ),
- //curl "https://api.particle.io/v1/devices/1b002c001247353236343033/signal?access_token=e9ff4e5ec3b85e66f7e50e1c0e2fa606d0722c02"
- Particle.subscribe( "ping", myHandler, MY_DEVICES );
- Particle.function( "set_bright", setBrightness );
- Particle.function( "set_speed", setSpeed );
- /* Setup Colours */
- currentPalette = CRGBPalette16(CRGB::Black);
- paletteFromHex();
- currentBlending = LINEARBLEND;
- /* Setup LEDs */
- delay( 3000 ); // power-up safety delay
- FastLED.addLeds<LED_TYPE, LED_PIN, COLOR_ORDER>(leds, NUM_LEDS).setCorrection( TypicalLEDStrip );
- FastLED.setBrightness( BRIGHTNESS );
- }
- void loop() {
- /* Update aux */
- static uint8_t startIndex = 0;
- startIndex++;
- signalStrength = WiFi.RSSI();
- /* Call colour functions*/
- FillLEDsFromPaletteColors(startIndex); // writes palette to LEDs
- uint8_t maxChanges = 6;
- nblendPaletteTowardPalette(currentPalette, targetPalette, maxChanges);
- //EVERY_N_HOURS(1) { lessFluidity(); }
- EVERY_N_MINUTES(3) { lessFluidity(); }
- /* Update LED and wait */
- FastLED.show();
- FastLED.delay(1000 / UPDATES_PER_SECOND );
- pinResetFast(STATUS_LED);
- }
- /*******************************************************************************
- * Function Name : myHandler
- * Description : Cloud Function - receives interaction
- * Input : char event name, char event data
- * Output : update fluidity
- * Return : 1 on success and -1 otherwise
- *******************************************************************************/
- void myHandler(const char *event, const char *data) {
- pinSetFast(STATUS_LED);
- if (fluidity < 3) fluidity++;
- newPalette();
- }
- /*******************************************************************************
- * Function Name : lessFluidity
- * Description : decreases fluidity value
- * Input : NA
- * Output : update fluidity
- * Return : NA
- *******************************************************************************/
- void lessFluidity() {
- if (fluidity > 1) fluidity --;
- }
- /*******************************************************************************
- * Function Name : paletteFromHex
- * Description : creates palette from hex values
- * Input : which palette to update
- * Output : update targetPalette
- * Return : NA
- *******************************************************************************/
- void paletteFromHex(){
- targetPalette = CRGBPalette16(
- 0x5500AB, 0x84007C, 0xB5004B, 0xE5001B,
- 0xE81700, 0xB84700, 0xAB7700, 0xABAB00,
- 0xAB5500, 0xDD2200, 0xF2000E, 0xC2003E,
- 0x8F0071, 0x5F00A1, 0x2F00D0, 0x0007F9
- );
- // violet, pink, lilac, red / magenta,
- // red / orange, dark amber, amber, straw ( could be nicer )
- // sunset orange, vivid sunset, dark sunset, magenta
- // light purple, purple, dark blue, bright blue
- }
- /*******************************************************************************
- * Function Name : newPalette
- * Description : creates palette from list of colours
- * Input : NA
- * Output : update targetPalette
- * Return : NA
- *******************************************************************************/
- void newPalette(){
- int r1 = random(0, block1 + 1);
- int r2 = random(block1, block2 + 1);
- int r3 = random(block2, block3 + 1);
- int r4 = random(block3, block4 + 1);
- CRGB c1 = colours[r1];
- CRGB c2 = colours[r2];
- CRGB c3 = colours[r3];
- CRGB c4 = colours[r4];
- targetPalette = CRGBPalette16(c1, c2, c3, c4);
- }
- /*******************************************************************************
- * Function Name : FillLEDsFromPaletteColors
- * Description : update colour value for LED pixels
- * Input : int colorIndex
- * Output : NA
- * Return : NA
- *******************************************************************************/
- void FillLEDsFromPaletteColors(uint8_t colorIndex) {
- uint8_t brightness = 255;
- for( int i = 0; i < NUM_LEDS; i++) {
- leds[i] = ColorFromPalette( currentPalette, colorIndex, brightness, currentBlending);
- colorIndex += 3;
- }
- }
- /*******************************************************************************
- * Function Name : setBrightness
- * Description : Cloud Function - overrides LED brightness
- * Input : int brightness value
- * Output : NA
- * Return : 1 if successful, -1 otherwise
- *******************************************************************************/
- int setBrightness(String val) {
- int newVal;
- if (newVal = val.toInt()){
- FastLED.setBrightness( newVal );
- return 1;
- }
- return -1;
- }
- /*******************************************************************************
- * Function Name : setSpeed
- * Description : Cloud Function - overrides animation speed
- * Input : int speed value
- * Output : NA
- * Return : 1 if successful, -1 otherwise
- *******************************************************************************/
- int setSpeed(String val) {
- int newVal;
- if (newVal = val.toInt()){
- UPDATES_PER_SECOND = newVal;
- return 1;
- }
- return -1;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement