Skip to content

Instantly share code, notes, and snippets.

@bboyho
Last active September 17, 2018 20:42
Show Gist options
  • Save bboyho/8a41dc99fe874673eabb2768566f0df4 to your computer and use it in GitHub Desktop.
Save bboyho/8a41dc99fe874673eabb2768566f0df4 to your computer and use it in GitHub Desktop.
Code to control APA102s with a button press.
#include "FastLED.h"
// How many leds in your strip?
#define NUM_LEDS 8
// 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 3
#define CLOCK_PIN 13
#define button1Pin 2 // pushbutton 1 pin
int button1State; // button state
boolean prev_button1State = false;
boolean current_button1State = false;
int pattern = 0; //pattern to control LEDs (i.e. ON, OFF, sequenced)
// Define the array of leds
CRGB leds[NUM_LEDS];
void setup() {
// Set up the pushbutton pin to be an input:
pinMode(button1Pin, INPUT_PULLUP);
// Uncomment/edit one of the following lines for your leds arrangement.
// FastLED.addLeds<TM1803, DATA_PIN, RGB>(leds, NUM_LEDS);
// FastLED.addLeds<TM1804, DATA_PIN, RGB>(leds, NUM_LEDS);
// FastLED.addLeds<TM1809, DATA_PIN, RGB>(leds, NUM_LEDS);
// FastLED.addLeds<WS2811, DATA_PIN, RGB>(leds, NUM_LEDS);
// FastLED.addLeds<WS2812, DATA_PIN, RGB>(leds, NUM_LEDS);
// FastLED.addLeds<WS2812B, DATA_PIN, RGB>(leds, NUM_LEDS);
//FastLED.addLeds<NEOPIXEL, DATA_PIN>(leds, NUM_LEDS);
// FastLED.addLeds<APA104, DATA_PIN, RGB>(leds, NUM_LEDS);
// FastLED.addLeds<UCS1903, DATA_PIN, RGB>(leds, NUM_LEDS);
// FastLED.addLeds<UCS1903B, DATA_PIN, RGB>(leds, NUM_LEDS);
// FastLED.addLeds<GW6205, DATA_PIN, RGB>(leds, NUM_LEDS);
// FastLED.addLeds<GW6205_400, DATA_PIN, RGB>(leds, NUM_LEDS);
// FastLED.addLeds<WS2801, RGB>(leds, NUM_LEDS);
// FastLED.addLeds<SM16716, RGB>(leds, NUM_LEDS);
// FastLED.addLeds<LPD8806, RGB>(leds, NUM_LEDS);
// FastLED.addLeds<P9813, RGB>(leds, NUM_LEDS);
// FastLED.addLeds<APA102, RGB>(leds, NUM_LEDS);
// FastLED.addLeds<DOTSTAR, RGB>(leds, NUM_LEDS);
// FastLED.addLeds<WS2801, DATA_PIN, CLOCK_PIN, RGB>(leds, NUM_LEDS);
// FastLED.addLeds<SM16716, DATA_PIN, CLOCK_PIN, RGB>(leds, NUM_LEDS);
// FastLED.addLeds<LPD8806, DATA_PIN, CLOCK_PIN, RGB>(leds, NUM_LEDS);
// FastLED.addLeds<P9813, DATA_PIN, CLOCK_PIN, RGB>(leds, NUM_LEDS);
FastLED.addLeds<APA102, DATA_PIN, CLOCK_PIN, BGR>(leds, NUM_LEDS);
// FastLED.addLeds<DOTSTAR, DATA_PIN, CLOCK_PIN, RGB>(leds, NUM_LEDS);
FastLED.setBrightness(255); //brighteness 0-255
}
void loop() {
button1State = digitalRead(button1Pin);
//if button is pressed
if (button1State == LOW) {
current_button1State = true; // button has been pressed once
if (prev_button1State != current_button1State) { //check to see if button is still being pressed
pattern = pattern + 1;
if (pattern < 0 || pattern >= 2) { //if out of bounds, start back at 0
pattern = 0;
}
}
else {
//do nothing because finger is still on button
}
prev_button1State = current_button1State;
}
//button has not been pressed, it will be high
else {
current_button1State = false;
prev_button1State = current_button1State;
}
switch (pattern) {
case 1:
patternOFF();
break;
default:
patternON();
break;
}
}//end loop
void patternON() { //all ON
for (int i = 0; i <= 4; i++) {
// Turn the LED on, then pause
leds[i] = CRGB::White;
FastLED.show();
delay(5);
}
}
void patternOFF() { //all off
for (int i = 0; i <= 4; i++) {
// Now turn the LED off, then pause
leds[i] = CRGB::Black;
FastLED.show();
delay(5);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment