Skip to content

Instantly share code, notes, and snippets.

@mtheoryx
Created July 27, 2019 18:19
Show Gist options
  • Save mtheoryx/2ed88df25977354fd8e959650c2cf6e9 to your computer and use it in GitHub Desktop.
Save mtheoryx/2ed88df25977354fd8e959650c2cf6e9 to your computer and use it in GitHub Desktop.
#include <FastLED.h>
FASTLED_USING_NAMESPACE;
// How many leds in your strip?
#define NUM_LEDS 25
// For led chips like Neopixels, which have a data line, ground, and power, you just
// need to define DATA_PIN. For led chipsets that are SPI based (four wires - data, clock,
// ground, and power), like the LPD8806, define both DATA_PIN and CLOCK_PIN
#define DATA_PIN 6
bool TURNED_ON; //Is the led on
// Define the array of leds
CRGB leds[NUM_LEDS];
void setup()
{
Particle.function("led", ledToggle);
LEDS.addLeds<NEOPIXEL, DATA_PIN>(leds, NUM_LEDS);
LEDS.setBrightness(84);
TURNED_ON = false;
}
void fadeall()
{
for (int i = 0; i < NUM_LEDS; i++)
{
leds[i].nscale8(60);
}
}
void loop()
{
if (TURNED_ON == true)
{
// First slide the led in one direction for cylon effect
for (int i = 0; i < 9; i++)
{
leds[i] = CRGB::Red;
// Show the leds
FastLED.show();
fadeall();
// Wait a little bit before we loop around and do it again
delay(200);
}
// Now go in the other direction for cylon effect
for (int i = (9) - 1; i >= 0; i--)
{
leds[i] = CRGB::Red;
// Show the leds
FastLED.show();
fadeall();
// Wait a little bit before we loop around and do it again
delay(200);
}
}
else
{
for (int i = 0; i < 9; i++)
{
leds[i] = CRGB::Black;
}
FastLED.show();
}
}
// Handle cloud function call for on/off state
int ledToggle(String command)
{
/* Particle.functions always take a string as an argument and return an integer.
Since we can pass a string, it means that we can give the program commands on how the function should be used.
In this case, telling the function "on" wi`ll turn the LED on and telling it "off" will turn the LED off.
Then, the function returns a value to us to let us know what happened.
In this case, it will return 1 for the LEDs turning on, 0 for the LEDs turning off,
and -1 if we received a totally bogus command that didn't do anything to the LEDs.
*/
if (command == "on")
{
TURNED_ON = true;
return 1;
}
else if (command == "off")
{
TURNED_ON = false;
return 0;
}
else
{
return -1;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment