Advertisement
Luther_deCaelum

Lamp Code

Dec 28th, 2024
249
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 9.38 KB | Software | 0 0
  1.  
  2. //Call the neopixel library, assign an output pin, and indicate the number of pixels on the strip.
  3. #include <Adafruit_NeoPixel.h>
  4. #define LED_PIN    14
  5. #define LED_COUNT 119
  6.  
  7. //These longs are used to debounce button inputs.
  8. unsigned long debounceDelay = 20;
  9. unsigned long debounceTime = 0;
  10.  
  11. //Define input pins for the encoder.
  12. int buttonPin = 4;
  13. int pinA = 2;
  14. int pinB = 3;
  15.  
  16. //boolean values for encoder input pins
  17. bool readingA;
  18. bool readingB;
  19.  
  20. int counter = 0; //counter to track position of encoder
  21. int counter2 = 0;
  22. int lastCounter2 = 0;
  23.  
  24. int encState = 0; //machine state position tracker for the encoder.
  25.  
  26. //button state / reading
  27. bool buttonReading = 1;
  28. bool lastButtonReading = 1;
  29. bool buttonState = 1;
  30. int buttonCase =0;
  31.  
  32. //integer for holding machine state
  33. int macState = 0;
  34.  
  35. //arrays and fade integers for raindrop program
  36. int pixelState[LED_COUNT][3];
  37. unsigned long pixelTime[LED_COUNT];
  38.  
  39. //brightness
  40. int bright = 50; //start at 50/255 brightness
  41. int lastBright = 50;
  42.  
  43. Adafruit_NeoPixel strip(LED_COUNT, LED_PIN, NEO_GRB + NEO_KHZ800);
  44.  
  45. void setup() {
  46.  
  47.   pinMode(buttonPin, INPUT_PULLUP);
  48.  
  49.   strip.begin();
  50.   strip.show();
  51.  
  52.   pinMode(pinA, INPUT_PULLUP);
  53.   pinMode(pinB, INPUT_PULLUP);
  54.  
  55.   Serial.begin(9600);
  56.  
  57. }
  58.  
  59. void loop() {
  60.  
  61.   if(macState > 3){macState = 0;}; //loops macState around
  62.  
  63.   switch (macState){
  64.     case 0:
  65.       Serial.print("Zero!"); Serial.println();
  66.       fullWhite();
  67.       break;
  68.    
  69.     case 1:
  70.       Serial.print("One!"); Serial.println();
  71.       raindrops();
  72.       break;
  73.  
  74.     case 2:
  75.       Serial.print("Two!"); Serial.println();
  76.       fullColour();
  77.       break;
  78.  
  79.     default:
  80.       Serial.print("Three!"); Serial.println();
  81.       rainbowDrops();
  82.       break;
  83.  
  84.   }
  85.  
  86. }
  87.  
  88.  
  89. void button(){
  90.  
  91.   //check if the program switcher is pressed
  92.   buttonReading = digitalRead(buttonPin);
  93.  
  94.   switch(buttonCase){
  95.  
  96.     case 0:
  97.       //if the button has changed position, activate debounce timer.
  98.       if(buttonReading != lastButtonReading){debounceTime = millis();};
  99.  
  100.       //check if debounce timer has timed out.
  101.       if(millis() - debounceTime > debounceDelay){
  102.         if(buttonState != lastButtonReading){
  103.           buttonState = buttonReading;
  104.           if(!buttonState){
  105.             Serial.print("Button on!"); Serial.println();
  106.  
  107.             lastCounter2 = counter2;
  108.  
  109.             buttonCase = 1;
  110.           };
  111.         };
  112.       };
  113.  
  114.     break;
  115.  
  116.     default:
  117.       if(buttonReading){
  118.         buttonState = buttonReading;
  119.         Serial.print("Button off!"); Serial.println();
  120.  
  121.         if(counter2 == lastCounter2){macState++;};
  122.         lastCounter2 = counter2;
  123.  
  124.         buttonCase = 0;
  125.       };
  126.  
  127.     break;
  128.   }
  129.  
  130.   lastButtonReading = buttonReading;
  131.  
  132. }
  133.  
  134. void encoder(){
  135.  
  136.   readingA = digitalRead(pinA);
  137.   readingB = digitalRead(pinB);
  138.  
  139.   switch(encState){
  140.     case 0 :
  141.       if(!readingA){encState = 1;}
  142.       else if(!readingB){encState = 4;};
  143.       break;
  144.  
  145.     case 1 :
  146.       if(!readingA && !readingB){encState = 2;};
  147.       break;
  148.  
  149.     case 2 :
  150.       if(readingA && !readingB){encState = 3;};
  151.       break;
  152.  
  153.     case 3 :
  154.       if(readingA && readingB){
  155.         encState = 0;
  156.         buttonState ? bright += 10 : counter2++;
  157.         };
  158.       break;
  159.  
  160.     case 4 :
  161.       if(!readingA && !readingB){encState = 5;};
  162.       break;
  163.    
  164.     case 5 :
  165.       if (!readingA && readingB){encState = 6;};
  166.       break;
  167.  
  168.     case 6 :
  169.       if(readingA && readingB){
  170.         encState = 0;
  171.         buttonState ? bright -= 10 : counter2--;
  172.         };
  173.       break;
  174.   };
  175.  
  176. }
  177.  
  178. void raindrops(){
  179.  
  180.   int blueBrightness = 50;
  181.   unsigned long tcurrent = 0;
  182.   unsigned long trandom;
  183.   unsigned long tdrip = 0;
  184.   int dripPixel;
  185.   uint32_t color;
  186.   int fadespeed = 5;
  187.   int fadeamount = 3;
  188.    
  189.   randomSeed(millis());
  190.  
  191.   strip.clear();
  192.   delay(10);
  193.   strip.setBrightness(bright);
  194.   delay(10);
  195.  
  196.  
  197.   for(int i=0; i<strip.numPixels(); i++) {
  198.     strip.setPixelColor(i, 0, 0, blueBrightness);
  199.     strip.show();
  200.     pixelState[i][0] = 0;
  201.     pixelState[i][1] = 0;
  202.     pixelState[i][2] = blueBrightness;
  203.     delay(5);
  204.   };
  205.  
  206.  
  207.   while(macState == 1){
  208.    
  209.     tcurrent = millis();
  210.  
  211.     if(tdrip == 0){
  212.       trandom = random(20, 150);
  213.       tdrip = trandom + millis();
  214.     };
  215.  
  216.     if(tcurrent > tdrip){
  217.     dripPixel = random(0,(LED_COUNT - 1));
  218.     strip.setPixelColor(dripPixel, 255, 255, 255);
  219.     tdrip = 0;
  220.     pixelState[dripPixel][0] = 255; pixelState[dripPixel][1] = 255; pixelState[dripPixel][2] = 255;
  221.     };
  222.  
  223.     for(int i = 0; i <= (LED_COUNT - 1); i++){
  224.  
  225.       if((pixelState[i][0] > 0) && (pixelTime[i] < tcurrent)){
  226.         pixelState[i][0] = pixelState[i][0] - fadeamount;
  227.         pixelState[i][1] = pixelState[i][1] - fadeamount;
  228.         int bluePixel = pixelState[i][2] - fadeamount;
  229.         pixelState[i][2] = constrain(bluePixel, blueBrightness, 255);
  230.         pixelTime[i] = tcurrent + fadespeed;
  231.         strip.setPixelColor(i, pixelState[i][0], pixelState[i][1], pixelState[i][2]);
  232.       };
  233.     };
  234.  
  235.     strip.show();
  236.  
  237.     encoder();
  238.  
  239.     if(bright != lastBright){
  240.       bright = constrain(bright, 10, 250);
  241.       strip.setBrightness(bright);
  242.       lastBright = bright;
  243.     }
  244.  
  245.     button();
  246.  
  247.   };
  248. }
  249.  
  250.  
  251. void fullWhite(){
  252.  
  253.   strip.clear();
  254.   strip.show();
  255.  
  256.   bright = constrain(bright, 10, 100);
  257.  
  258.   strip.setBrightness(bright);
  259.  
  260.   randomSeed(millis());
  261.  
  262.   //clear array for use as a boolean index
  263.   for(int i=0; i<strip.numPixels(); i++) {
  264.     pixelState[i][0] = 0;
  265.   };
  266.  
  267.  
  268.   for(int j = 1; j <= strip.numPixels(); j++){
  269.  
  270.     //pick a random LED number between one, and the number of LEDs currently turned off
  271.     int position = random(1, (strip.numPixels() - j+1));
  272.  
  273.     //set search numbers
  274.     int pixelAddr = 0;
  275.     int positionCount = 1;
  276.    
  277.     //locate the off LED on the strip
  278.     while(positionCount <= position){
  279.  
  280.       if(pixelState[pixelAddr][0] == 0){
  281.         positionCount++;
  282.       };
  283.  
  284.       if(positionCount < position){pixelAddr++;};
  285.       if((positionCount == 1) && (pixelState[pixelAddr][0] == 1)){pixelAddr++;};
  286.  
  287.     };
  288.  
  289.     //log the new pixel being turned on in the array
  290.     pixelState[pixelAddr][0] = 1;
  291.  
  292.  
  293.  
  294.     //reset the memory in the strip one pixel at a time
  295.     for(int i = 0; i < strip.numPixels(); i++){
  296.       if(pixelState[i][0] == 1){strip.setPixelColor(i,250,250,250);};
  297.       if(pixelState[i][0] == 0){strip.setPixelColor(i, 0, 0, 0);};
  298.     };
  299.  
  300.  
  301.     //display the new settings
  302.     strip.show();
  303.  
  304.     delay(10);
  305.  
  306.  
  307.   };
  308.  
  309.   while(macState == 0){
  310.  
  311.       encoder();
  312.  
  313.       if(bright != lastBright){
  314.         bright = constrain(bright, 10, 100);
  315.         strip.setBrightness(bright);
  316.         strip.show();
  317.         lastBright = bright;
  318.       }
  319.  
  320.       button();
  321.   };
  322.  
  323. }
  324.  
  325. void fullColour(){
  326.  
  327.   lastBright = bright;
  328.  
  329.   uint16_t stripHue = 1;
  330.   uint8_t lastCounter2 = counter2;
  331.   uint32_t stripColor;
  332.  
  333.   counter2 = 0;
  334.  
  335.   strip.clear();
  336.  
  337.   strip.setBrightness(bright);
  338.  
  339.   stripColor = strip.ColorHSV(stripHue);
  340.  
  341.   for(int i=0; i<strip.numPixels(); i++) {
  342.     strip.setPixelColor(i, stripColor);
  343.     strip.show();
  344.     delay(5);
  345.   };
  346.  
  347.   while(macState == 2){
  348.  
  349.     button();
  350.     encoder();
  351.  
  352.     if(counter2 != lastCounter2){
  353.       if(counter2 > 32){counter2 = 0;};
  354.       if(counter2 < 0){counter2 = 32;};
  355.       stripHue = constrain((counter2 * 2048), 1, 65535);
  356.       stripColor = strip.ColorHSV(stripHue);
  357.       strip.fill(stripColor);
  358.       strip.show();
  359.     }
  360.  
  361.     lastCounter2 = counter2;
  362.  
  363.     if(bright != lastBright){
  364.       bright = constrain(bright, 10, 250);
  365.       strip.setBrightness(bright);
  366.       strip.show();
  367.       lastBright = bright;
  368.     }
  369.   };
  370. }
  371.  
  372. void rainbowDrops(){
  373.  
  374.   unsigned long tcurrent = 0;
  375.   unsigned long trandom;
  376.   unsigned long tdrip = 0;
  377.   int dripPixel;
  378.   uint32_t color;
  379.   int fadespeed = 10;
  380.   int fadeamount = 3;
  381.    
  382.   randomSeed(millis());
  383.  
  384.   strip.clear();
  385.   strip.setBrightness(bright);  
  386.  
  387.   for(int i=0; i<strip.numPixels(); i++) {
  388.     pixelState[i][0] = 0;
  389.     pixelState[i][1] = 0;
  390.     pixelState[i][2] = 0;
  391.   };
  392.  
  393.   while(macState == 3){
  394.    
  395.     tcurrent = millis();
  396.  
  397.     if(tdrip == 0){
  398.       trandom = random(15, 75);
  399.       tdrip = trandom + millis();
  400.     };
  401.  
  402.     if(tcurrent > tdrip){
  403.     dripPixel = random(0,(LED_COUNT - 1));
  404.  
  405.     uint8_t rRandom = random(0, 255);
  406.     uint8_t gRandom = random(0, 255);
  407.     uint8_t bRandom = random(0, 255);
  408.  
  409.     strip.setPixelColor(dripPixel, rRandom, gRandom, bRandom);
  410.     tdrip = 0;
  411.     pixelState[dripPixel][0] = rRandom; pixelState[dripPixel][1] = gRandom; pixelState[dripPixel][2] = bRandom;
  412.     };
  413.  
  414.     for(int i = 0; i <= (LED_COUNT - 1); i++){
  415.  
  416.       if(((pixelState[i][0] > 0 || (pixelState[i][1] > 0) || (pixelState[i][2] > 0))) && (pixelTime[i] < tcurrent)){
  417.         pixelState[i][0] = constrain(pixelState[i][0] - fadeamount, 0, 255);
  418.         pixelState[i][1] = constrain(pixelState[i][1] - fadeamount, 0, 255);
  419.         pixelState[i][2] = constrain(pixelState[i][2] - fadeamount, 0, 255);
  420.         pixelTime[i] = tcurrent + fadespeed;
  421.         strip.setPixelColor(i, pixelState[i][0], pixelState[i][1], pixelState[i][2]);
  422.       };
  423.     };
  424.  
  425.     strip.show();
  426.  
  427.     encoder();
  428.  
  429.     if(bright != lastBright){
  430.       bright = constrain(bright, 10, 250);
  431.       strip.setBrightness(bright);
  432.       lastBright = bright;
  433.     }
  434.  
  435.     button();
  436.  
  437.   };
  438. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement