Neopixel Strip Trying to Reverse Direction of Lights

EL Wire/Tape/Panels, LEDs, pixels and strips, LCDs and TFTs, etc products from Adafruit

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
thelaravee
 
Posts: 1
Joined: Sat Jul 18, 2015 5:29 pm

Neopixel Strip Trying to Reverse Direction of Lights

Post by thelaravee »

Hi! I'm a bit new at this so my abilities are quite limited.

A friend of mine created a code for a Neopixel strip that changes modes. The only thing I need to do is change the direction of the theaterchase. I have the LED strip in a circle so it looks like a wheel and I want it to show movement so right now they look like they're going backwards.

I'm attaching the code I have now. I managed to change the direction of the colorwipe and I tried to do the same with the theaterchase but I just can't figure it out.

Thank you!

Code: Select all

// GoGo Tomago LED Effects!
// slacosplay (facebook.com/slacosplay) & ladycammi (ladycammi.com)
// Oct 2015

#include <Adafruit_NeoPixel.h>
#include <avr/power.h>

//------------------------------------------------------------------------------------------------
// SET YOUR STUFF HERE :)
// -- Side note:
//    Adafruit's NeoPixels call each LED a "pixel", so for consistency with naming 
//    conventions, every variable called "pixel" really just refers to its LEDS
// -----------------------------------------------------------------------------------------------
#define BUTTON_PIN    2       // # to the pin your button is connected to
#define PIXEL_PIN     1      // # to the pin your LEDs are connected to
#define PIXEL_COUNT   47      // # of LEDs you have

// .. just don't touch this line here:
Adafruit_NeoPixel strip = Adafruit_NeoPixel(PIXEL_COUNT, PIXEL_PIN, NEO_GRB + NEO_KHZ800);

uint32_t SERIAL_BAUD = 9600;                // Sets the baud rate for the Arduino Serial Monitor

uint32_t RED = strip.Color(225,0,0);        // The color RED, RGB=(255,0,0)
uint32_t OFF = strip.Color(0,0,0);          // Color is "BLACK" RGB=(0,0,0); in other words, "off"
uint32_t MAX_BRIGHTNESS = 200;              // Set the maximum brightness for your pixels

uint32_t DELAY_MS = 50;                     // Delay in milliseconds (1000 ms = 1 second)
uint32_t NUM_TO_REPEAT_COLOR_WIPE = 5;      // # of times to repeat the color wipe
uint32_t NUM_TO_REPEAT_THEATER_CHASE = 50;  // # of times to repeat the theater chase
uint32_t NUM_TO_REPEAT_PULSE = 10;           // # of times to repeat a color pulse
uint32_t NUM_TO_REPEAT_RAINBOW_CYCLE = 3;   // # of times to repeat a rainbow cycle


// Set the order you want your button presses to cycle through here
// Note: whatever you set to "1" is the first mode your LEDs start at
const int MODE_MULTICOLOR_WIPE = 1;
const int MODE_THEATER_CHASE = 2;
const int MODE_COLOR_PULSE = 3;
const int MODE_SET_ONE_COLOR = 4;
const int MODE_RAINBOW_CYCLE = 5;
const int MODE_TURN_OFF = 0;

//------------------------------------------------------------------------------------------------




// Starts off with the button raised (not pressed)
bool oldState = HIGH;
int mode = 0;
  
void setup() {
  pinMode(BUTTON_PIN, INPUT_PULLUP);
  strip.begin();
  strip.show();
  strip.setBrightness(MAX_BRIGHTNESS);
  //Serial.begin(SERIAL_BAUD);
  //Serial.println("Starting GoGo Tomago LED effects!");
  //Serial.println();
}

void loop() {
 
  // Get current button state.
  bool newState = digitalRead(BUTTON_PIN);
      
  // Check if state changed from high to low (button press).
  if (newState == LOW && oldState == HIGH) {
    // Short delay to debounce button.
    delay(20);
  
    // Check if button is still low after debounce.
    newState = digitalRead(BUTTON_PIN);

    // If the button is pressed...
    if (newState == LOW) {

      // Increment the mode
      mode++;
      if (mode > 5) { mode = 0; }
      
      //Serial.print("Mode = ");
      //Serial.println(mode);
      startShow(mode);
    }
  }

  // Set the last button state to the old state.
  oldState = newState;
}
  
void startShow(int mode) {
  switch(mode){
    case MODE_TURN_OFF: 
      //Serial.println("Expected: pixels turn off");
      //Serial.println();
      colorWipe(OFF, DELAY_MS);    
      break;
    case MODE_MULTICOLOR_WIPE: 
      //Serial.println("Expected: do a color wipe to red multiple times");
      multiColorWipe(RED, NUM_TO_REPEAT_COLOR_WIPE, DELAY_MS);
      break;
    case MODE_THEATER_CHASE:
      //Serial.println("Expected: do a red theater chase");
      theaterChase(RED, NUM_TO_REPEAT_THEATER_CHASE, DELAY_MS);
      break;
    case MODE_COLOR_PULSE:
      //Serial.println("Expected: do a red color pulse");
      colorPulse(RED, NUM_TO_REPEAT_PULSE, DELAY_MS);
      break;
    case MODE_SET_ONE_COLOR:
      //Serial.println("Expected: set all pixels to red instantaneously");
      //Serial.println();
      strip.setBrightness(MAX_BRIGHTNESS);
      setAllPixels(RED);
      break;
    case MODE_RAINBOW_CYCLE:
      //Serial.println("Expected: do a rainbow color cycle");
      rainbowCycle(DELAY_MS, NUM_TO_REPEAT_RAINBOW_CYCLE);
      break;
  }
}


// Fills the pixels one after the other with a color
// Source: buttoncycler.ino (modified slightly)
void colorWipe(uint32_t color, uint8_t wait) {
  for(int i=(strip.numPixels()-1); i>=0; i--) {
    strip.setPixelColor(i, color);
    strip.show();
    delay(wait);
  }
}


// Repeats a color wipe of a color for a specified number of times
// Source: buttoncycler.ino
void multiColorWipe(uint32_t color, uint16_t numToRepeat, uint8_t wait) {
  //Serial.print("Doing color wipe ...");
  for(int i = 0; i < numToRepeat; i++) {
    colorWipe(OFF, wait/2);
    colorWipe(color, wait/2);
  }
  //Serial.println("... all done!");
  //Serial.println();
}


// Performs a theater chase / crawling light effect by turning every third 
// pixel on and off
// Source: buttoncycler.ino (modified slightly)
void theaterChase(uint32_t color, uint16_t numToRepeat, uint8_t wait) {
  //Serial.print("Doing a theater chase ...");
  for(int i=0; i < numToRepeat; i++) {
    for(int q=0; q < 3; q++) {
      
      // Turn every third pixel on
      for(int pixel=0; pixel < strip.numPixels(); pixel=pixel+3) {
        strip.setPixelColor(pixel+q, color);
      }
  
      strip.show();
      delay(wait);
  
      // Turn every third pixel off
      for(int pixel=0; pixel < strip.numPixels(); pixel=pixel+3) {
        strip.setPixelColor(pixel+q, OFF);
      }
    }
  }
  setAllPixels(color);
  //Serial.println("... all done!");
  //Serial.println();
}


// Creates a pulse effect by fading in and then out of one color
void colorPulse(uint32_t color, uint16_t numToRepeat, uint8_t wait) {
  //Serial.println("Doing a color pulse ...");
  for(int num=0; num < numToRepeat; num++) {

    //Serial.print("Fading in ...");
    for(int i=0; i < MAX_BRIGHTNESS; i++) {
      strip.setBrightness(i);
      setAllPixels(color);
      delay(wait/50);
    }
    
    //Serial.println("... fading out");
    for(int i=MAX_BRIGHTNESS; i > 0; i--) {
      strip.setBrightness(i);
      setAllPixels(color);
      delay(wait/50);
    }

    delay(wait*2);
  }
  //Serial.println("... all done!");
  //Serial.println();
}


// Sets all pixels to one color at once
void setAllPixels(uint32_t color) {
  for(int pixel=0; pixel < strip.numPixels(); pixel++) {
    strip.setPixelColor(pixel, color);
  }
  strip.show();
}


// Has all pixels cycle through the colors of the rainbow in unison
// Source: buttoncycle.ino (modified slightly)
void rainbowCycle(uint8_t wait, uint16_t numToRepeat) {
  //Serial.print("Doing rainbow cycle ...");
  uint16_t i, j;
  for(j=0; j<256*numToRepeat; j++) { // 5 cycles of all colors on wheel
    for(i=0; i< strip.numPixels(); i++) {
      strip.setPixelColor(i, Wheel(((i * 256 / strip.numPixels()) + j) & 255));
    }
    strip.show();
    delay(wait);
  }
  //Serial.println("... all done!");
  //Serial.println();
}


// Input a value 0 to 255 to get a color value.
// The colours are a transition r - g - b - back to r.
// source: buttoncycle.ino
uint32_t Wheel(byte WheelPos) {
  WheelPos = 255 - WheelPos;
  if(WheelPos < 85) {
   return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3);
  } else if(WheelPos < 170) {
    WheelPos -= 85;
   return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3);
  } else {
   WheelPos -= 170;
   return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
  }
}

User avatar
adafruit_support_bill
 
Posts: 88136
Joined: Sat Feb 07, 2009 10:11 am

Re: Neopixel Strip Trying to Reverse Direction of Lights

Post by adafruit_support_bill »

To reverse the direction, you can either reverse the 'for' loop to count down, or reverse the pixel addressing by subtracting the pixel number from the number of pixels. Since this is a more complex nested loop, the second approach is simpler.

Locked
Please be positive and constructive with your questions and comments.

Return to “Glowy things (LCD, LED, TFT, EL) purchased at Adafruit”