macca-nz

Simple NeoPixel loop using serial inputs

Apr 12th, 2022 (edited)
723
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.  * Type "w" for wheel effect
  3.  * Type "r" and you'll get just red leds
  4.  * Type "q" to clear all leds
  5.  */
  6.  
  7. //Constants
  8. #include <Adafruit_NeoPixel.h>        //The wizardry of addressable LEDs
  9.  
  10. #define PIN 6                         // Output pin for led strip
  11. #define NUMPIXELS 12                  // Number of LED Diodes
  12. #define SPEED 30                      // Creates the speed of an effect
  13.  
  14. //Make your "strip" object. Note the macro's used "NUMPIXELS, PIN"  
  15. Adafruit_NeoPixel strip = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);
  16.  
  17. //Variables
  18. char BTdata = 0;                      //Variable to store received serial data
  19. int count = 0;                        //Used to change wipe color
  20. int r, g, b;                          //Variables for changing the color
  21. bool wipeEffect = false;              //Turns wipe effect On and OFF
  22. bool single = false;                  //Turn single color On or Off
  23.  
  24. void setup(){
  25.     Serial.begin(38400);              //Sets the baud for serial data transmission                              
  26.     strip.begin();                    //Adafruit_NeoPixel library object begin class "object.begin();)
  27.     strip.setBrightness(50);          //class to setup NeoPixel brightness level
  28.     strip.show();                     //Initialize all pixels to 'off'
  29. }
  30.  
  31. //Everything above is the "scope" and is common to both sketches
  32.  
  33. void loop(){                          
  34.  
  35.    if(Serial.available() > 0)         // Send data only when you receive data:
  36.    {
  37.       BTdata = Serial.read();         //Read the  incoming  data and store it into variable data
  38.       Serial.println(BTdata);         //Print Value inside data in Serial monito
  39.  
  40.       if(BTdata == 'q'){                       //Input q to turn strip LEDs OFF
  41.           wipeEffect = false;
  42.           r=0; g=0; b=0;
  43.           single = true;
  44.       }
  45.  
  46.       if(BTdata == 'r'){                       //Input r to turn strip LEDs OFF
  47.           wipeEffect = false;
  48.           r=255; g=0; b=0;
  49.           single = true;
  50.       }
  51.    
  52.       if(BTdata == 'w'){                          //Enter w into the serial monitor to run Wheel effect
  53.           wipeEffect = true;                      //Starts Wipe sequence
  54.           single = false;
  55.           count = 0;
  56.       }
  57.    }
  58.     if(single){
  59.         for(uint16_t i=0; i<strip.numPixels(); i++) {
  60.               strip.setPixelColor(i, strip.Color(r, g, b));
  61.               strip.show();
  62.               delay(SPEED);
  63.         }
  64.         single = false;
  65.     }
  66.    
  67.    if(wipeEffect){                            //When true wipe is runnig
  68.       if(count == 0){                         //Incrementing counter changes the wipe color
  69.         r=255;g=0;b=0;                        //Display RED
  70.       }else if(count == 1){
  71.         r=0;g=0;b=0;                          //Clear
  72.       }else if(count == 2){
  73.         r=0;g=255;b=0;                        //Display Green
  74.       }else if(count == 3){
  75.         r=0;g=0;b=0;                          //Clear
  76.       }else if(count == 4){
  77.         r=0;g=0;b=255;                        //Display Blue
  78.       }else if(count == 5){
  79.         r=0;g=0;b=0;                          //Clear
  80.       }
  81.           for(uint16_t i=0; i<strip.numPixels(); i++) {
  82.               strip.setPixelColor(i, strip.Color(r, g, b));
  83.               strip.show();
  84.                   if(i == NUMPIXELS - 1){         //When we hit the last LED
  85.                       count++;                    //We increment by +1
  86.                           if(count > 5){          //Sequence complete
  87.                              count = 0;           //So we start again by resetting the counter
  88.                           }
  89.                   }
  90.                   delay(SPEED);
  91.           }
  92.    }
  93. }
Add Comment
Please, Sign In to add comment