Skip to content

Instantly share code, notes, and snippets.

@zachskaggs
Created January 20, 2020 15:20
Show Gist options
  • Save zachskaggs/8502f90a758e1548e7a98ba1295d6d4a to your computer and use it in GitHub Desktop.
Save zachskaggs/8502f90a758e1548e7a98ba1295d6d4a to your computer and use it in GitHub Desktop.
#include <FastLED.h>
// How many leds in your strip?
#define NUM_LEDS 60
// 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
#define CLOCK_PIN 13
// Define the array of leds
CRGB leds[NUM_LEDS];
// Everything below until setup is rotary declarations
int val;
int encoder0PinA = 8;
int encoder0PinB = 9;
int encoder0PinALast = 0;
int n = LOW;
// button codes
const int buttonPin = 7; // the number of the pushbutton pin
// variables will change:
int buttonPushCounter = 0; // counter for the number of button presses
int buttonState = 0; // current state of the button
int lastButtonState = 0; // previous state of the button // variable for reading the pushbutton status
int newDelay = 100;
void setup() {
Serial.begin(57600);
Serial.println("resetting");
LEDS.addLeds<WS2812B,DATA_PIN,GRB>(leds,NUM_LEDS);
LEDS.setBrightness(50);
// the rest of this function is rotary setup
pinMode (encoder0PinA, INPUT);
pinMode (encoder0PinB, INPUT);
pinMode (buttonPin, INPUT);
}
void fadeall() { for(int i = 0; i < NUM_LEDS; i++) { leds[i].nscale8(250); } }
int getNewDelay() {
n = digitalRead(encoder0PinA);
if ((encoder0PinALast == LOW) && (n == HIGH)) {
if (digitalRead(encoder0PinB) == LOW) {
newDelay = newDelay + 10;
} else {
newDelay = newDelay - 10;
}
}
encoder0PinALast = n;
return newDelay;
}
int getNewColor() {
buttonState = digitalRead(buttonPin);
// compare the buttonState to its previous state
if (buttonState != lastButtonState) {
// if the state has changed, increment the counter
if (buttonState == HIGH) {
// if the current state is HIGH then the button went from off to on:
buttonPushCounter++;
} else {
// if the current state is LOW then the button went from on to off:
Serial.println("off");
}
// Delay a little bit to avoid bouncing
delay(50);
}
// save the current state as the last state, for next time through the loop
lastButtonState = buttonState;
if (buttonPushCounter % 3 == 0) {
return 64;
} else if (buttonPushCounter % 3 == 1) {
return 0;
} else if (buttonPushCounter % 3 == 2) {
return 96;
} else {
return 0;
}
}
void loop() {
// First slide the led in one direction
for(int i = 0; i < NUM_LEDS; i++) {
// Set the i'th led to red
leds[i] = CHSV(getNewColor(), 255, 255);
// Show the leds
FastLED.show();
// now that we've shown the leds, reset the i'th led to black
// leds[i] = CRGB::Black;
fadeall();
// Wait a little bit before we loop around and do it again
delay(getNewDelay());
}
// Now go in the other direction.
for(int i = (NUM_LEDS)-1; i >= 0; i--) {
// Set the i'th led to red
leds[i] = CHSV(getNewColor(), 255, 255);
// Show the leds
FastLED.show();
// now that we've shown the leds, reset the i'th led to black
// leds[i] = CRGB::Black;
fadeall();
// Wait a little bit before we loop around and do it again
delay(getNewDelay());
}
//the rest of this function is rotary code
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment