Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Learning how a 2D Array works using NeoPixel and longhand style scripting
- *
- * Type anything in serial monitor to start or stop effect
- * Pattern is LED1, RED ON, RED OFF, GREEN ON, GREEN OFF, BLUE ON, BLUE OFF
- * then step to LED2 and repeat the RGB blink sequence etc etc
- * The loop will continue till you stop with serial command
- */
- //Constants
- #include <Adafruit_NeoPixel.h> //The wizardry of addressable LEDs
- #define PIN 6 // Output pin for led strip
- #define NUMPIXELS 12 // Number of LED Diodes
- #define BLINK 500 // Blink rate time IE 500ms on or off
- int colors[][3]{ //2D Array holds the color pattern
- {255,0,0}, //RED 0
- {0,0,0}, //OFF 1
- {0,255,0}, //GREEN 2
- {0,0,0}, //OFF 3
- {0,0,255}, //BLUE 4
- {0,0,0} //OFF 5
- };
- byte r,g,b; //Variables to hold color levels
- //Make your "strip" object. Note the macro's used "NUMPIXELS, PIN"
- Adafruit_NeoPixel strip = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);
- //Variables
- char BTdata = 0; //Variable to store received serial data
- byte led, colorIndex; //Variables for led number and which color to use
- bool runEffect = false; //Used to start and stope the effect
- unsigned long prevTime = 0; //Create a Non-blocking blink timer
- void setup(){
- Serial.begin(38400); //Sets the baud for serial data transmission
- strip.begin(); //Adafruit_NeoPixel library object begin class "object.begin();)
- strip.setBrightness(50); //class to setup NeoPixel brightness level
- strip.show(); //Initialize all pixels to 'off'
- }
- //Everything above is the "scope" and is common to both sketches
- void loop(){
- if(Serial.available() > 0){ // Send data only when you receive data:
- BTdata = Serial.read(); //Read the incoming data and store it into variable data
- Serial.println(BTdata); //Print Value inside data in Serial monito
- runEffect = !runEffect; //Toggle effect on and off
- if(runEffect == false){ //When condition met it stops the effect and turns off all leds
- for(uint16_t i=0; i<strip.numPixels(); i++) {
- strip.setPixelColor(i, strip.Color(0, 0, 0));
- strip.show();
- }
- }else{ //When "runEffect == true" the conditions will turn on the first led
- colorIndex = 0; //red and the effect sequence runs
- r = colors[colorIndex][0]; //sets RED value for current 2D array colorIndex
- g = colors[colorIndex][1]; //sets GREEN value for current 2D array colorIndex
- b = colors[colorIndex][2]; //sets BLUE value for current 2D array colorIndex
- led = 0; //the first array address is always 0
- }
- }
- if(runEffect == true){
- if(millis() - prevTime >= BLINK){ //create the blink timer
- strip.setPixelColor(led, strip.Color(r,g,b)); //writes current led rgb values
- strip.show();
- colorIndex++; //Steps to the next color value in array
- if(colorIndex > 5){ //reset count at end of array
- colorIndex = 0; //back to red being the first color in 2D aqrray
- led++; //step to the next led
- }
- led %= NUMPIXELS; //When we reach the end of the strip reset to 0
- r = colors[colorIndex][0]; //sets RED value for current 2D array colorIndex
- g = colors[colorIndex][1]; //sets GREEN value for current 2D array colorIndex
- b = colors[colorIndex][2]; //sets BLUE value for current 2D array colorIndex
- prevTime = millis(); //Resets timer for next BLINK period
- }
- }
- }
Add Comment
Please, Sign In to add comment