Advertisement
Guest User

version finale

a guest
Jan 18th, 2017
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.03 KB | None | 0 0
  1. /*
  2.  This code can be a good test code for your strip
  3.  function: leds.setPixelColor(i,y) can be used to let number i of your led turn on with color of y
  4.  and you can draw your idea easily with this function but never forget function: led.show()
  5. */
  6.  
  7. #include <Adafruit_NeoPixel.h>
  8. #include <Wire.h>
  9. #include <LiquidCrystal_I2C.h>
  10. #define PIN 27    //The signal pin connected with Arduino    
  11. #define LED_COUNT 60 // the amount of the leds of your strip(60 LEDs per meter)
  12.  
  13. unsigned long previousMillis = 0;        // will store last time LED was updated
  14. // constants won't change :
  15. const long interval = 50;           // interval at which to blink (milliseconds)
  16. const long mininumSound = 100;
  17. int a = 0;
  18. // Create an instance of the Adafruit_NeoPixel class called "leds".
  19. // That'll be what we refer to from here on...
  20. Adafruit_NeoPixel leds = Adafruit_NeoPixel(LED_COUNT, PIN, NEO_GRB + NEO_KHZ800);
  21. LiquidCrystal_I2C lcd(0x20,16,2);
  22.  
  23. void setup()
  24. {
  25.   lcd.init();
  26.   lcd.begin(16,2);
  27.   lcd.backlight();
  28.   leds.begin();  // Call this to start up the LED strip.
  29.   clearLEDs();   // This function, defined below, turns all LEDs off...
  30.   leds.show();   // ...but the LEDs don't actually update until you call this.
  31.  // Serial.begin(9600);
  32. }
  33.  
  34. void loop()
  35. {
  36.  
  37.  unsigned long currentMillis = millis();
  38.  int val;
  39.       val=analogRead(A6);
  40.  // Serial.println(val);
  41.   if (val > mininumSound)
  42.   {
  43.       lcd.clear0();
  44.       val=analogRead(A6);
  45.       lcd.setCursor(0,0);
  46.       lcd.print(val);
  47.  // Serial.println(val);
  48.   //clearLEDs();
  49.     //delay(1000);
  50.  
  51.   }
  52.   else {
  53.    
  54.       if (a<LED_COUNT)
  55.         {
  56.           // save the last time you blinked the LED
  57.           if(currentMillis - previousMillis >= interval) {
  58.             rainbow(a);
  59.             previousMillis = currentMillis;
  60.             a++;
  61.           }
  62.       }
  63.      else {
  64.       a = 1;
  65.       }
  66.   }      
  67. }
  68.    
  69. // Sets all LEDs to off, but DOES NOT update the display;
  70. // call leds.show() to actually turn them off after this.
  71. void clearLEDs()
  72. {
  73.   for (int i=0; i<LED_COUNT; i++)
  74.   {
  75.     leds.setPixelColor(i, 0);
  76.   }
  77. }
  78.  
  79. // Prints a rainbow on the ENTIRE LED strip.
  80. //  The rainbow begins at a specified position.
  81. // ROY G BIV!
  82. void rainbow(byte startPosition)
  83. {
  84.   // Need to scale our rainbow. We want a variety of colors, even if there
  85.   // are just 10 or so pixels.
  86.   int rainbowScale = 192 / LED_COUNT;
  87.    
  88.   // Next we setup each pixel with the right color
  89.   for (int i=0; i<LED_COUNT; i++)
  90.   {
  91.     // There are 192 total colors we can get out of the rainbowOrder function.
  92.     // It'll return a color between red->orange->green->...->violet for 0-191.
  93.     leds.setPixelColor(i, rainbowOrder((rainbowScale * (i + startPosition)) % 192));
  94.   }
  95.   // Finally, actually turn the LEDs on:
  96.   leds.show();
  97. }
  98.  
  99. // Input a value 0 to 191 to get a color value.
  100. // The colors are a transition red->yellow->green->aqua->blue->fuchsia->red...
  101. //  Adapted from Wheel function in the Adafruit_NeoPixel library example sketch
  102. uint32_t rainbowOrder(byte position)
  103. {
  104.   // 6 total zones of color change:
  105.   if (position < 31)  // Red -> Yellow (Red = FF, blue = 0, green goes 00-FF)
  106.   {
  107.     return leds.Color(0xFF, position * 8, 0);
  108.   }
  109.   else if (position < 63)  // Yellow -> Green (Green = FF, blue = 0, red goes FF->00)
  110.   {
  111.     position -= 31;
  112.     return leds.Color(0xFF - position * 8, 0xFF, 0);
  113.   }
  114.   else if (position < 95)  // Green->Aqua (Green = FF, red = 0, blue goes 00->FF)
  115.   {
  116.     position -= 63;
  117.     return leds.Color(0, 0xFF, position * 8);
  118.   }
  119.   else if (position < 127)  // Aqua->Blue (Blue = FF, red = 0, green goes FF->00)
  120.   {
  121.     position -= 95;
  122.     return leds.Color(0, 0xFF - position * 8, 0xFF);
  123.   }
  124.   else if (position < 159)  // Blue->Fuchsia (Blue = FF, green = 0, red goes 00->FF)
  125.   {
  126.     position -= 127;
  127.     return leds.Color(position * 8, 0, 0xFF);
  128.   }
  129.   else  //160 <position< 191   Fuchsia->Red (Red = FF, green = 0, blue goes FF->00)
  130.   {
  131.     position -= 159;
  132.     return leds.Color(0xFF, 0x00, 0xFF - position * 8);
  133.   }
  134. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement