macca-nz

2D-Array for NeoPixel Effect Pattern

Apr 13th, 2022 (edited)
356
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.  * Learning how a 2D Array works using NeoPixel and longhand style scripting
  3.  *
  4.  * Type anything in serial monitor to start or stop effect
  5.  * Pattern is LED1, RED ON, RED OFF, GREEN ON, GREEN OFF, BLUE ON, BLUE OFF
  6.  * then step to LED2 and repeat the RGB blink sequence etc etc
  7.  * The loop will continue till you stop with serial command
  8.  */
  9.  
  10. //Constants
  11. #include <Adafruit_NeoPixel.h>        //The wizardry of addressable LEDs
  12.  
  13. #define PIN 6                         // Output pin for led strip
  14. #define NUMPIXELS 12                  // Number of LED Diodes
  15. #define BLINK 500                     // Blink rate time IE 500ms on or off
  16.  
  17. int colors[][3]{                      //2D Array holds the color pattern
  18.     {255,0,0},                        //RED 0
  19.     {0,0,0},                          //OFF 1
  20.     {0,255,0},                        //GREEN 2
  21.     {0,0,0},                          //OFF 3
  22.     {0,0,255},                        //BLUE 4
  23.     {0,0,0}                           //OFF 5
  24. };
  25. byte r,g,b;                           //Variables to hold color levels
  26.  
  27. //Make your "strip" object. Note the macro's used "NUMPIXELS, PIN"  
  28. Adafruit_NeoPixel strip = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);
  29.  
  30. //Variables
  31. char BTdata = 0;                      //Variable to store received serial data                        
  32. byte led, colorIndex;                 //Variables for led number and which color to use
  33. bool runEffect = false;               //Used to start and stope the effect
  34. unsigned long prevTime = 0;           //Create a Non-blocking blink timer
  35.  
  36. void setup(){
  37.     Serial.begin(38400);              //Sets the baud for serial data transmission                              
  38.     strip.begin();                    //Adafruit_NeoPixel library object begin class "object.begin();)
  39.     strip.setBrightness(50);          //class to setup NeoPixel brightness level
  40.     strip.show();                     //Initialize all pixels to 'off'
  41. }
  42.  
  43. //Everything above is the "scope" and is common to both sketches
  44.  
  45. void loop(){                          
  46.  
  47.    if(Serial.available() > 0){         // Send data only when you receive data:
  48.       BTdata = Serial.read();          //Read the  incoming  data and store it into variable data
  49.       Serial.println(BTdata);          //Print Value inside data in Serial monito
  50.       runEffect = !runEffect;          //Toggle effect on and off
  51.           if(runEffect == false){      //When condition met it stops the effect and turns off all leds
  52.              for(uint16_t i=0; i<strip.numPixels(); i++) {
  53.                 strip.setPixelColor(i, strip.Color(0, 0, 0));
  54.                 strip.show();
  55.              }
  56.           }else{                          //When "runEffect == true" the conditions will turn on the first led
  57.               colorIndex = 0;             //red and the effect sequence runs
  58.               r = colors[colorIndex][0];  //sets RED value for current 2D array colorIndex
  59.               g = colors[colorIndex][1];  //sets GREEN value for current 2D array colorIndex
  60.               b = colors[colorIndex][2];  //sets BLUE value for current 2D array colorIndex
  61.               led = 0;                    //the first array address is always 0
  62.           }
  63.    }
  64.    if(runEffect == true){
  65.       if(millis() - prevTime >= BLINK){                   //create the blink timer
  66.           strip.setPixelColor(led, strip.Color(r,g,b));   //writes current led rgb values
  67.           strip.show();
  68.           colorIndex++;                                   //Steps to the next color value in array
  69.           if(colorIndex > 5){                             //reset count at end of array
  70.               colorIndex = 0;                             //back to red being the first color in 2D aqrray
  71.               led++;                                      //step to the next led
  72.           }
  73.           led %= NUMPIXELS;                               //When we reach the end of the strip reset to 0
  74.           r = colors[colorIndex][0];                      //sets RED value for current 2D array colorIndex
  75.           g = colors[colorIndex][1];                      //sets GREEN value for current 2D array colorIndex
  76.           b = colors[colorIndex][2];                      //sets BLUE value for current 2D array colorIndex
  77.           prevTime = millis();                            //Resets timer for next BLINK period
  78.       }
  79.    }
  80. }
Add Comment
Please, Sign In to add comment