Created
April 15, 2016 07:38
-
-
Save jessherzog/5267e90a2ea8cafdb200fd8de5c6e2b8 to your computer and use it in GitHub Desktop.
8 functions with button WITH debounce
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <SPI.h> | |
#include <Adafruit_DotStar.h> | |
#include "FastLED.h" | |
#define NUM_LEDS 15 | |
#define BRIGHTNESS 96 | |
#define FRAMES_PER_SECOND 120 | |
#define DATA_PIN 5 | |
#define CLOCK_PIN 6 | |
CRGB leds[NUM_LEDS]; | |
int buttonPin = 2; | |
int val, val2; | |
int buttonState; | |
int lastButtonState = LOW; | |
int counter; | |
long lastDebounceTime = 0; | |
long debounceDelay = 50; | |
void setup() { | |
pinMode(buttonPin, INPUT); | |
buttonState = digitalRead(buttonPin); | |
Serial.begin(9600); | |
LEDS.addLeds<DOTSTAR, DATA_PIN, CLOCK_PIN, RGB>(leds, NUM_LEDS); | |
LEDS.setBrightness(BRIGHTNESS); | |
} | |
void loop() { | |
int reading = digitalRead(buttonPin); | |
if (reading != lastButtonState) { | |
// reset timer | |
lastDebounceTime = millis(); | |
} | |
if ((millis() - lastDebounceTime) > debounceDelay) { | |
if (reading != buttonState) { | |
buttonState = reading; | |
counter++; | |
} | |
} | |
switch (counter) { | |
case 1: | |
oneSprinkle(); | |
break; | |
case 2: | |
threeSprinkle(); | |
case 3: | |
fadeHue(); | |
break; | |
case 4: | |
fadeNoBounce(); | |
case 5: | |
fadeBounce(); | |
break; | |
case 6: | |
fadeHalfHue(); | |
break; | |
case 7: | |
bounceFadeRain(); | |
break; | |
case 8: | |
tropicThunder(); | |
break | |
} | |
lastButtonState = reading; | |
} | |
void oneSprinkle() { | |
static uint8_t hue = 0; | |
for (int i = 0; i <= NUM_LEDS; i++) { | |
leds[i] = CHSV(255, 255, 255); | |
} | |
FastLED.show(); | |
delay(10); | |
} | |
void threeSprinkle() { | |
static uint8_t hue = 0; | |
// forth | |
for (int i = 0; i <= NUM_LEDS; i++) { | |
leds[i + 0] = CHSV(255, 255, (255 / 10) * 1); | |
leds[i + 1] = CHSV(255, 255, (255 / 10) * 2); | |
leds[i + 2] = CHSV(255, 255, (255 / 10) * 5); | |
leds[i + 3] = CHSV(255, 255, (255 / 10) * 10); | |
FastLED.show(); | |
leds[i] = CRGB::Black; | |
delay(100); | |
} | |
} | |
void fadeHue() { | |
FastLED.show(); | |
FastLED.delay(1000 / FRAMES_PER_SECOND); | |
EVERY_N_MILLISECONDS( 20 ) { | |
gHue++; | |
} | |
} | |
void fadeNoBounce() { | |
static uint8_t hue = 0; | |
// forth | |
for (int i = 0; i <= NUM_LEDS; i++) { | |
leds[i + 0] = CHSV(255, 255, (255 / 10) * 1); | |
leds[i + 1] = CHSV(255, 255, (255 / 10) * 2); | |
leds[i + 2] = CHSV(255, 255, (255 / 10) * 5); | |
leds[i + 3] = CHSV(255, 255, (255 / 10) * 7); | |
leds[i + 4] = CHSV(255, 255, (255 / 10) * 10); | |
FastLED.show(); | |
leds[i] = CRGB::Black; | |
delay(100); | |
} | |
} | |
void fadeBounce() { | |
static uint8_t hue = 0; | |
// forth | |
// for (int i = 0; i <= NUM_LEDS; i++) { | |
// leds[i + 0] = CHSV(255, 255, (255 / 10) * 1); | |
// leds[i + 1] = CHSV(255, 255, (255 / 10) * 2); | |
// leds[i + 2] = CHSV(255, 255, (255 / 10) * 5); | |
// leds[i + 3] = CHSV(255, 255, (255 / 10) * 7); | |
// leds[i + 4] = CHSV(255, 255, (255 / 10) * 10); | |
// FastLED.show(); | |
// leds[i] = CRGB::Black; | |
// delay(100); | |
// } | |
//and back | |
for (int i = (NUM_LEDS / 2); i >= 0; i--) { | |
leds[i] = CHSV(255, 255, (255 / 10) * 10); | |
leds[i] = CHSV(255, 255, (255 / 4) * 3); | |
leds[i] = CHSV(255, 255, (255 / 4) * 2); | |
leds[i] = CHSV(255, 255, (255 / 4) * 1); | |
leds[i] = CHSV(255, 255, (255 / 4) * 0.5); | |
FastLED.show(); | |
leds[i] = CRGB::Black; | |
delay(100); | |
} | |
} | |
void fadeHalfHue() { | |
fadeToBlackBy( leds, NUM_LEDS, 20); | |
int pos = beatsin16(8, 0, NUM_LEDS); | |
leds[pos] += CHSV(gHue, 200, 192); | |
if (pos >= 7) { | |
gHue = 250; | |
} else { | |
gHue = 50; | |
} | |
} | |
void bounceFadeRain() { | |
fadeToBlackBy(leds, NUM_LEDS, 20); | |
int pos = beatsin8(10, 0, NUM_LEDS); | |
leds[pos] += CHSV(gHue, 255, 192); | |
if (pos == 1 || pos == 15) { | |
hit++; | |
} | |
FastLED.show(); | |
FastLED.delay(100 / FRAMES_PER_SECOND); | |
if (hit % 4 == 0) { | |
gHue++; | |
} | |
} | |
void tropicThunder() { | |
// adapted from Michael's Unicorn Thunder | |
// https://github.com/mixania/Unicorn | |
// ☆彡 ☆。、:*:。 | |
unsigned int randSpark = random(0, (NUM_LEDS) - 1); | |
unsigned int thunderDelay = random(3, 50); | |
leds[randSpark] = CRGB::Turquoise; | |
FastLED.show(); | |
delay(thunderDelay); | |
leds[randSpark] = CRGB::Orange; | |
FastLED.show(); | |
delay(thunderDelay); | |
leds[randSpark] = CRGB::HotPink; | |
FastLED.show(); | |
delay(thunderDelay); | |
leds[randSpark] = CRGB::Black; | |
delay(thunderDelay); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks for this! However, I get an error on line 69 that says expected ';' before '}' token.