2 Strings of NeoPixels controlled via 2 Data Pins

Post here about your Arduino projects, get help - for Adafruit customers!

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
dcousins21
 
Posts: 1
Joined: Mon Nov 24, 2014 6:37 pm

2 Strings of NeoPixels controlled via 2 Data Pins

Post by dcousins21 »

Hello

I hope someone can help me, I am very new to Arduino / C++ so please excuse me if I am asking a silly question!

I have 2 strings of NeoPixels (I am using 8mm individual Pixels, 8 per string) - Connected to pins 10 and 11 onto a Arduino Uno.

I have code which runs though a set of different patterns which is ideal for one strip - but I would like the second strip to run though just a single pattern along side the first strip.

The code I have at the moment works - however it does the first strip, then second, then first, then second etc - but I want them both to work at the same time (with different patterns)

Here is the code I am currently using :

Code: Select all

#include <Adafruit_NeoPixel.h>
 

Adafruit_NeoPixel strip2 = Adafruit_NeoPixel(6, 11, NEO_RGB + NEO_KHZ800);
Adafruit_NeoPixel strip1 = Adafruit_NeoPixel(6, 10, NEO_RGB + NEO_KHZ800);
 
#define Brightness 10 //Set brightness to 1/10th
#define Full (255/Brightness)


//Create scrolling red and white candy cane stripes.
//Try adjusting the width in pixels for various results.
//Value "sets" should evenly divide into strand length
void CandyCane  (int sets,int width,int wait) {
  int L;
  for(int j=0;j<(sets*width);j++) {
    for(int i=0;i< strip2.numPixels();i++) {
      L=strip2.numPixels()-i-1;
      if ( ((i+j) % (width*2) )<width)
        strip2.setPixelColor(L,Full,0,0);
      else
        strip2.setPixelColor(L,Full,Full, Full);
    };
    strip2.show();
    delay(wait);
  };
};
 
//Create sets of random white or gray pixels
void RandomWhite (int sets, int wait) {
  int V,i,j;
  for (i=0;i<sets;i++) {
    for(j=0;j<strip2.numPixels();j++) {
      V=random(Full);
      strip2.setPixelColor(j,V,V,V);
    }
    strip2.show();
    delay(wait);
  }
};
//Create sets of random colors
void RandomColor (int sets, int wait) {
  int i,j;
  for (i=0;i<sets;i++) {
    for(j=0;j<strip2.numPixels();j++) {
      strip2.setPixelColor(j,random(255)/Brightness,random(255)/Brightness,random(255)/Brightness);
    }
    strip2.show();
    delay(wait);
  }
};
void RainbowStripe (int sets,int width,int wait) {
  int L;
  for(int j=0;j<(sets*width*6);j++) {
    for(int i=0;i< strip2.numPixels();i++) {
      L=strip2.numPixels()-i-1;
      switch ( ( (i+j)/width) % 6 ) {
        case 0: strip2.setPixelColor(L,Full,0,0);break;//Red
        case 1: strip2.setPixelColor(L,Full,Full,0);break;//Yellow
        case 2: strip2.setPixelColor(L,0,Full,0);break;//Green
        case 3: strip2.setPixelColor(L,0,Full,Full);break;//Cyan
        case 4: strip2.setPixelColor(L,0,0,Full);break;//Blue
        case 5: strip2.setPixelColor(L,Full,0,Full);break;//Magenta
//        default: strip2.setPixelColor(L,0,0,0);//Use for debugging only
      }
    };
    strip2.show();
    delay(wait);
  };
};
//These routines were modified from Adafruit strand test sketch
// Fill the dots one after the other with a color
void colorWipe(uint32_t c, uint8_t wait) {
  for(uint16_t i=0; i<strip2.numPixels(); i++) {
      strip2.setPixelColor(i, c);
      strip2.show();
      delay(wait);
  }
}

 
void rainbowCycle(uint8_t sets, uint8_t wait) {
  uint16_t i, j;
  for(j=0; j<256*sets; j++) { //cycles of all colors on wheel
    for(i=0; i< strip2.numPixels(); i++) {
      strip2.setPixelColor(strip2.numPixels()-i-1, Wheel(((i * 256 / strip2.numPixels()) + j) & 255));
    }
    strip2.show();
    delay(wait);
  }
}

void rainbowCycle1(uint8_t sets, uint8_t wait) {
  uint16_t i, j;
  for(j=0; j<256*sets; j++) { //cycles of all colors on wheel
    for(i=0; i< strip1.numPixels(); i++) {
      strip1.setPixelColor(strip2.numPixels()-i-1, Wheel(((i * 256 / strip1.numPixels()) + j) & 255));
    }
    strip1.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) {
  if(WheelPos < 85) {
   return strip2.Color((WheelPos * 3)/Brightness, (255 - WheelPos * 3)/Brightness, 0);
  } else if(WheelPos < 170) {
   WheelPos -= 85;
   return strip2.Color((255 - WheelPos * 3)/Brightness, 0, (WheelPos * 3)/Brightness);
  } else {
   WheelPos -= 170;
   return strip2.Color(0,(WheelPos * 3)/Brightness, (255 - WheelPos * 3)/Brightness);
  }
}
 
void setup() {
  pinMode(10, INPUT_PULLUP); //initializes strip1  
  pinMode(11, INPUT_PULLUP); //initializes strip2 
  strip1.begin();
  strip1.show(); // Initialize all pixels to 'off'
  strip2.begin();
  strip2.show();
  randomSeed(1234);//Set up random number generator
}
 
void loop() {
  
  CandyCane(30,8,50);//30 sets, 8 pixels wide, 50us delay
  rainbowCycle1(10,2);//10 rainbow cycles
  
  
  RainbowStripe(5,4,75);//5 cycles, 4 pixels wide, 50 delay
  rainbowCycle1(10,2);//10 rainbow cycles
  
  
  RandomWhite(50,200);//50 sets of random grayscale
  rainbowCycle1(10,2);//10 rainbow cycles
  
  
  RandomColor(50,200);//50 sets of random colors
  rainbowCycle1(10,2);//10 rainbow cycles
  
  
  colorWipe(strip2.Color(Full, 0, 0), 50); // Red
  colorWipe(strip2.Color(Full, Full, 0), 50); // Yellow
  colorWipe(strip2.Color(0, Full, 0), 50); // Green
  colorWipe(strip2.Color(0, Full, Full), 50); // Cyan
  colorWipe(strip2.Color(0, 0, Full), 50); // Blue
  colorWipe(strip2.Color(Full, 0, Full), 50); // Magenta
  rainbowCycle1(10,2);//10 rainbow cycles
  
 
  rainbowCycle(10,2);//10 rainbow cycles
  rainbowCycle1(10,2);//10 rainbow cycles
  
  
  
  colorWipe(0,5);//Black
  
}

Any help would be much appreciated! Thanks in advanced :D

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

Re: 2 Strings of NeoPixels controlled via 2 Data Pins

Post by adafruit_support_bill »

The solution will involve a significant re-arrangement of your code. The Ardino can only do one thing at a time, so you need to re-write your patterns in a way that lets you interleave the pixel operations between the two strips. I have a tutorial planned on the subject, but no example code for neopixels yet. You can get an idea of the basic technique from part 1 of the series: https://learn.adafruit.com/multi-taskin ... 1/overview

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

Re: 2 Strings of NeoPixels controlled via 2 Data Pins

Post by adafruit_support_bill »

Here is part 3 of the series. it deals specifically with Neopixels and should help you to do what you want there:
https://learn.adafruit.com/multi-taskin ... ino-part-3

User avatar
PilotC150
 
Posts: 132
Joined: Sun May 04, 2014 9:39 pm

Re: 2 Strings of NeoPixels controlled via 2 Data Pins

Post by PilotC150 »

Great tutorial! These same design patterns could definitely be applied to many different applications.

Thanks for the writeup!

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

Return to “Arduino”