Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Call the neopixel library, assign an output pin, and indicate the number of pixels on the strip.
- #include <Adafruit_NeoPixel.h>
- #define LED_PIN 14
- #define LED_COUNT 119
- //These longs are used to debounce button inputs.
- unsigned long debounceDelay = 20;
- unsigned long debounceTime = 0;
- //Define input pins for the encoder.
- int buttonPin = 4;
- int pinA = 2;
- int pinB = 3;
- //boolean values for encoder input pins
- bool readingA;
- bool readingB;
- int counter = 0; //counter to track position of encoder
- int counter2 = 0;
- int lastCounter2 = 0;
- int encState = 0; //machine state position tracker for the encoder.
- //button state / reading
- bool buttonReading = 1;
- bool lastButtonReading = 1;
- bool buttonState = 1;
- int buttonCase =0;
- //integer for holding machine state
- int macState = 0;
- //arrays and fade integers for raindrop program
- int pixelState[LED_COUNT][3];
- unsigned long pixelTime[LED_COUNT];
- //brightness
- int bright = 50; //start at 50/255 brightness
- int lastBright = 50;
- Adafruit_NeoPixel strip(LED_COUNT, LED_PIN, NEO_GRB + NEO_KHZ800);
- void setup() {
- pinMode(buttonPin, INPUT_PULLUP);
- strip.begin();
- strip.show();
- pinMode(pinA, INPUT_PULLUP);
- pinMode(pinB, INPUT_PULLUP);
- Serial.begin(9600);
- }
- void loop() {
- if(macState > 3){macState = 0;}; //loops macState around
- switch (macState){
- case 0:
- Serial.print("Zero!"); Serial.println();
- fullWhite();
- break;
- case 1:
- Serial.print("One!"); Serial.println();
- raindrops();
- break;
- case 2:
- Serial.print("Two!"); Serial.println();
- fullColour();
- break;
- default:
- Serial.print("Three!"); Serial.println();
- rainbowDrops();
- break;
- }
- }
- void button(){
- //check if the program switcher is pressed
- buttonReading = digitalRead(buttonPin);
- switch(buttonCase){
- case 0:
- //if the button has changed position, activate debounce timer.
- if(buttonReading != lastButtonReading){debounceTime = millis();};
- //check if debounce timer has timed out.
- if(millis() - debounceTime > debounceDelay){
- if(buttonState != lastButtonReading){
- buttonState = buttonReading;
- if(!buttonState){
- Serial.print("Button on!"); Serial.println();
- lastCounter2 = counter2;
- buttonCase = 1;
- };
- };
- };
- break;
- default:
- if(buttonReading){
- buttonState = buttonReading;
- Serial.print("Button off!"); Serial.println();
- if(counter2 == lastCounter2){macState++;};
- lastCounter2 = counter2;
- buttonCase = 0;
- };
- break;
- }
- lastButtonReading = buttonReading;
- }
- void encoder(){
- readingA = digitalRead(pinA);
- readingB = digitalRead(pinB);
- switch(encState){
- case 0 :
- if(!readingA){encState = 1;}
- else if(!readingB){encState = 4;};
- break;
- case 1 :
- if(!readingA && !readingB){encState = 2;};
- break;
- case 2 :
- if(readingA && !readingB){encState = 3;};
- break;
- case 3 :
- if(readingA && readingB){
- encState = 0;
- buttonState ? bright += 10 : counter2++;
- };
- break;
- case 4 :
- if(!readingA && !readingB){encState = 5;};
- break;
- case 5 :
- if (!readingA && readingB){encState = 6;};
- break;
- case 6 :
- if(readingA && readingB){
- encState = 0;
- buttonState ? bright -= 10 : counter2--;
- };
- break;
- };
- }
- void raindrops(){
- int blueBrightness = 50;
- unsigned long tcurrent = 0;
- unsigned long trandom;
- unsigned long tdrip = 0;
- int dripPixel;
- uint32_t color;
- int fadespeed = 5;
- int fadeamount = 3;
- randomSeed(millis());
- strip.clear();
- delay(10);
- strip.setBrightness(bright);
- delay(10);
- for(int i=0; i<strip.numPixels(); i++) {
- strip.setPixelColor(i, 0, 0, blueBrightness);
- strip.show();
- pixelState[i][0] = 0;
- pixelState[i][1] = 0;
- pixelState[i][2] = blueBrightness;
- delay(5);
- };
- while(macState == 1){
- tcurrent = millis();
- if(tdrip == 0){
- trandom = random(20, 150);
- tdrip = trandom + millis();
- };
- if(tcurrent > tdrip){
- dripPixel = random(0,(LED_COUNT - 1));
- strip.setPixelColor(dripPixel, 255, 255, 255);
- tdrip = 0;
- pixelState[dripPixel][0] = 255; pixelState[dripPixel][1] = 255; pixelState[dripPixel][2] = 255;
- };
- for(int i = 0; i <= (LED_COUNT - 1); i++){
- if((pixelState[i][0] > 0) && (pixelTime[i] < tcurrent)){
- pixelState[i][0] = pixelState[i][0] - fadeamount;
- pixelState[i][1] = pixelState[i][1] - fadeamount;
- int bluePixel = pixelState[i][2] - fadeamount;
- pixelState[i][2] = constrain(bluePixel, blueBrightness, 255);
- pixelTime[i] = tcurrent + fadespeed;
- strip.setPixelColor(i, pixelState[i][0], pixelState[i][1], pixelState[i][2]);
- };
- };
- strip.show();
- encoder();
- if(bright != lastBright){
- bright = constrain(bright, 10, 250);
- strip.setBrightness(bright);
- lastBright = bright;
- }
- button();
- };
- }
- void fullWhite(){
- strip.clear();
- strip.show();
- bright = constrain(bright, 10, 100);
- strip.setBrightness(bright);
- randomSeed(millis());
- //clear array for use as a boolean index
- for(int i=0; i<strip.numPixels(); i++) {
- pixelState[i][0] = 0;
- };
- for(int j = 1; j <= strip.numPixels(); j++){
- //pick a random LED number between one, and the number of LEDs currently turned off
- int position = random(1, (strip.numPixels() - j+1));
- //set search numbers
- int pixelAddr = 0;
- int positionCount = 1;
- //locate the off LED on the strip
- while(positionCount <= position){
- if(pixelState[pixelAddr][0] == 0){
- positionCount++;
- };
- if(positionCount < position){pixelAddr++;};
- if((positionCount == 1) && (pixelState[pixelAddr][0] == 1)){pixelAddr++;};
- };
- //log the new pixel being turned on in the array
- pixelState[pixelAddr][0] = 1;
- //reset the memory in the strip one pixel at a time
- for(int i = 0; i < strip.numPixels(); i++){
- if(pixelState[i][0] == 1){strip.setPixelColor(i,250,250,250);};
- if(pixelState[i][0] == 0){strip.setPixelColor(i, 0, 0, 0);};
- };
- //display the new settings
- strip.show();
- delay(10);
- };
- while(macState == 0){
- encoder();
- if(bright != lastBright){
- bright = constrain(bright, 10, 100);
- strip.setBrightness(bright);
- strip.show();
- lastBright = bright;
- }
- button();
- };
- }
- void fullColour(){
- lastBright = bright;
- uint16_t stripHue = 1;
- uint8_t lastCounter2 = counter2;
- uint32_t stripColor;
- counter2 = 0;
- strip.clear();
- strip.setBrightness(bright);
- stripColor = strip.ColorHSV(stripHue);
- for(int i=0; i<strip.numPixels(); i++) {
- strip.setPixelColor(i, stripColor);
- strip.show();
- delay(5);
- };
- while(macState == 2){
- button();
- encoder();
- if(counter2 != lastCounter2){
- if(counter2 > 32){counter2 = 0;};
- if(counter2 < 0){counter2 = 32;};
- stripHue = constrain((counter2 * 2048), 1, 65535);
- stripColor = strip.ColorHSV(stripHue);
- strip.fill(stripColor);
- strip.show();
- }
- lastCounter2 = counter2;
- if(bright != lastBright){
- bright = constrain(bright, 10, 250);
- strip.setBrightness(bright);
- strip.show();
- lastBright = bright;
- }
- };
- }
- void rainbowDrops(){
- unsigned long tcurrent = 0;
- unsigned long trandom;
- unsigned long tdrip = 0;
- int dripPixel;
- uint32_t color;
- int fadespeed = 10;
- int fadeamount = 3;
- randomSeed(millis());
- strip.clear();
- strip.setBrightness(bright);
- for(int i=0; i<strip.numPixels(); i++) {
- pixelState[i][0] = 0;
- pixelState[i][1] = 0;
- pixelState[i][2] = 0;
- };
- while(macState == 3){
- tcurrent = millis();
- if(tdrip == 0){
- trandom = random(15, 75);
- tdrip = trandom + millis();
- };
- if(tcurrent > tdrip){
- dripPixel = random(0,(LED_COUNT - 1));
- uint8_t rRandom = random(0, 255);
- uint8_t gRandom = random(0, 255);
- uint8_t bRandom = random(0, 255);
- strip.setPixelColor(dripPixel, rRandom, gRandom, bRandom);
- tdrip = 0;
- pixelState[dripPixel][0] = rRandom; pixelState[dripPixel][1] = gRandom; pixelState[dripPixel][2] = bRandom;
- };
- for(int i = 0; i <= (LED_COUNT - 1); i++){
- if(((pixelState[i][0] > 0 || (pixelState[i][1] > 0) || (pixelState[i][2] > 0))) && (pixelTime[i] < tcurrent)){
- pixelState[i][0] = constrain(pixelState[i][0] - fadeamount, 0, 255);
- pixelState[i][1] = constrain(pixelState[i][1] - fadeamount, 0, 255);
- pixelState[i][2] = constrain(pixelState[i][2] - fadeamount, 0, 255);
- pixelTime[i] = tcurrent + fadespeed;
- strip.setPixelColor(i, pixelState[i][0], pixelState[i][1], pixelState[i][2]);
- };
- };
- strip.show();
- encoder();
- if(bright != lastBright){
- bright = constrain(bright, 10, 250);
- strip.setBrightness(bright);
- lastBright = bright;
- }
- button();
- };
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement