Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Adafruit_NeoPixel.h>
- #ifdef __AVR__
- #include <avr/power.h>
- #endif
- #define DATA_PIN 19
- #define NUM_LEDS 300
- int NUM_RAINBOW_LEDS = 300;
- const int potentiometerPin = 15;
- const int potentiometerPinPlus1 = potentiometerPin + 1;
- const int potentiometerPinMinus1 = potentiometerPin - 1;
- const int patternSwitchPin = 4;
- const int whiteSwitchPin = 3;
- const int protocolSwitchPin = 2;
- // Parameter 1 = number of pixels in strip
- // Parameter 2 = Arduino pin number (most are valid)
- // Parameter 3 = pixel type flags, add together as needed:
- // NEO_KHZ800 800 KHz bitstream (most NeoPixel products w/WS2812 LEDs)
- // NEO_KHZ400 400 KHz (classic 'v1' (not v2) FLORA pixels, WS2811 drivers)
- // NEO_GRB Pixels are wired for GRB bitstream (most NeoPixel products)
- // NEO_RGB Pixels are wired for RGB bitstream (v1 FLORA pixels, not v2)
- // NEO_RGBW Pixels are wired for RGBW bitstream (NeoPixel RGBW products)
- Adafruit_NeoPixel *strip;
- int patternSwitchState = 1;
- int whiteSwitchState = 1;
- int protocolSwitchState = 1;
- void setup() {
- pinMode(patternSwitchPin, INPUT_PULLUP);
- pinMode(potentiometerPin, INPUT_PULLUP);
- pinMode(potentiometerPinPlus1, OUTPUT);
- pinMode(potentiometerPinMinus1, OUTPUT);
- pinMode(whiteSwitchPin, INPUT_PULLUP);
- pinMode(5, OUTPUT); // debugging LED - remove later
- digitalWrite(potentiometerPinPlus1, LOW); // leave this low to give a ground pin conveniently next to the potentiometer input pin
- digitalWrite(potentiometerPinMinus1, HIGH); // leave this high to give a 5 V pin conveniently next to the potentiometer input pin
- if (digitalRead(whiteSwitchPin) == HIGH) {
- whiteSwitchState = 1;
- } else {
- whiteSwitchState = 0;
- }
- strip = new Adafruit_NeoPixel(NUM_LEDS, DATA_PIN, (whiteSwitchState ? NEO_GRB : NEO_RGBW) + NEO_KHZ800);
- strip->begin();
- strip->show(); // Initialize all pixels to 'off'
- strip->setBrightness(2); // Set BRIGHTNESS to about 1/5 (max = 255)
- }
- void loop() {
- NUM_RAINBOW_LEDS = map(analogRead(potentiometerPin), 0, 1023, 1, 60);
- patternSwitchState = digitalRead(patternSwitchPin);
- if (patternSwitchState == 1) { // for debugging LED - remove later
- digitalWrite(5, HIGH);
- }
- else {
- digitalWrite(5, LOW);
- }
- if ((patternSwitchState == 0) && (whiteSwitchState == 0)) {
- solidColoursRGB();
- } else if ((patternSwitchState == 1) && (whiteSwitchState == 0)) {
- rainbowCycle(2);
- } else if ((patternSwitchState == 0) && (whiteSwitchState == 1)) {
- solidColoursRGBW();
- } else if ((patternSwitchState == 1) && (whiteSwitchState == 1)); {
- whiteOverRainbow(20, 75, (NUM_RAINBOW_LEDS / 8 + 1));
- }
- }
- void solidColoursRGB() {
- // Clear the existing led values, then set all to green according to potentimeter reading.
- strip->clear();
- for (int led = 0; led < NUM_LEDS; led++) {
- strip->setPixelColor(led, 255, 0, 0);
- }
- strip->show();
- delay(250);
- strip->show();
- delay(250);
- strip->show();
- delay(250);
- strip->show();
- delay(250);
- strip->show();
- delay(250);
- strip->show();
- delay(250);
- strip->show();
- delay(250);
- strip->show();
- delay(250);
- // Clear the existing led values, then set all to red according to potentimeter reading.
- strip->clear();
- for (int led = 0; led < NUM_LEDS; led++) {
- strip->setPixelColor(led, 0, 255, 0);
- }
- strip->show();
- delay(250);
- strip->show();
- delay(250);
- strip->show();
- delay(250);
- strip->show();
- delay(250);
- strip->show();
- delay(250);
- strip->show();
- delay(250);
- strip->show();
- delay(250);
- strip->show();
- delay(250);
- // Clear the existing led values, then set all to blue according to potentimeter reading.
- strip->clear();
- for (int led = 0; led < NUM_LEDS; led++) {
- strip->setPixelColor(led, 0, 0, 255);
- }
- strip->show();
- delay(250);
- strip->show();
- delay(250);
- strip->show();
- delay(250);
- strip->show();
- delay(250);
- strip->show();
- delay(250);
- strip->show();
- delay(250);
- strip->show();
- delay(250);
- strip->show();
- delay(250);
- }
- void solidColoursRGBW() {
- // Clear the existing led values, then set all to green according to potentimeter reading.
- strip->clear();
- for (int led = 0; led < NUM_LEDS; led++) {
- strip->setPixelColor(led, 255, 0, 0, 0);
- }
- strip->show();
- delay(250);
- strip->show();
- delay(250);
- strip->show();
- delay(250);
- strip->show();
- delay(250);
- strip->show();
- delay(250);
- strip->show();
- delay(250);
- strip->show();
- delay(250);
- strip->show();
- delay(250);
- // Clear the existing led values, then set all to red according to potentimeter reading.
- strip->clear();
- for (int led = 0; led < NUM_LEDS; led++) {
- strip->setPixelColor(led, 0, 255, 0, 0);
- }
- strip->show();
- delay(250);
- strip->show();
- delay(250);
- strip->show();
- delay(250);
- strip->show();
- delay(250);
- strip->show();
- delay(250);
- strip->show();
- delay(250);
- strip->show();
- delay(250);
- strip->show();
- delay(250);
- // Clear the existing led values, then set all to blue according to potentimeter reading.
- strip->clear();
- for (int led = 0; led < NUM_LEDS; led++) {
- strip->setPixelColor(led, 0, 0, 255, 0);
- }
- strip->show();
- delay(250);
- strip->show();
- delay(250);
- strip->show();
- delay(250);
- strip->show();
- delay(250);
- strip->show();
- delay(250);
- strip->show();
- delay(250);
- strip->show();
- delay(250);
- strip->show();
- delay(250);
- // Clear the existing led values, then set all to white according to potentimeter reading.
- strip->clear();
- for (int led = 0; led < NUM_LEDS; led++) {
- strip->setPixelColor(led, 0, 0, 0, 255);
- }
- strip->show();
- delay(250);
- strip->show();
- delay(250);
- strip->show();
- delay(250);
- strip->show();
- delay(250);
- strip->show();
- delay(250);
- strip->show();
- delay(250);
- strip->show();
- delay(250);
- strip->show();
- delay(250);
- }
- // Slightly different, this makes the rainbow equally distributed throughout
- void rainbowCycle(uint8_t wait) {
- uint16_t i, j;
- for (j = 0; j < 256 * 5; j++) { // 5 cycles of all colors on wheel
- for (i = 0; i < NUM_RAINBOW_LEDS; i++) {
- strip->setPixelColor(i, Wheel(((i * 256 / NUM_RAINBOW_LEDS) + j) & 255));
- // strip->setPixelColor(1, 255, 255, 255);
- }
- strip->show();
- delay(wait);
- }
- }
- // Input a value 0 to 255 to get a color value.
- // The colours are a transition r - g - b - back to r.
- uint32_t Wheel(byte WheelPos) {
- WheelPos = 255 - WheelPos;
- if (WheelPos < 85) {
- return strip->Color(255 - WheelPos * 3, 0, WheelPos * 3);
- }
- if (WheelPos < 170) {
- WheelPos -= 85;
- return strip->Color(0, WheelPos * 3, 255 - WheelPos * 3);
- }
- WheelPos -= 170;
- return strip->Color(WheelPos * 3, 255 - WheelPos * 3, 0);
- }
- void whiteOverRainbow(uint8_t wait, uint8_t whiteSpeed, uint8_t whiteLength ) {
- if (whiteLength >= NUM_RAINBOW_LEDS) whiteLength = NUM_RAINBOW_LEDS - 1;
- int head = whiteLength - 1;
- int tail = 0;
- int loops = 3;
- int loopNum = 0;
- static unsigned long lastTime = 0;
- while (true) {
- for (int j = 0; j < 256; j++) {
- for (uint16_t i = 0; i < NUM_RAINBOW_LEDS; i++) {
- if ((i >= tail && i <= head) || (tail > head && i >= tail) || (tail > head && i <= head) ) {
- strip->setPixelColor(i, strip->Color(0, 0, 0, 255 ) );
- }
- else {
- strip->setPixelColor(i, Wheel(((i * 256 / NUM_RAINBOW_LEDS) + j) & 255));
- }
- }
- if (millis() - lastTime > whiteSpeed) {
- head++;
- tail++;
- if (head == NUM_RAINBOW_LEDS) {
- loopNum++;
- }
- lastTime = millis();
- }
- if (loopNum == loops) return;
- head %= NUM_RAINBOW_LEDS;
- tail %= NUM_RAINBOW_LEDS;
- strip->show();
- delay(wait);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement