Multiple pins controlled by one 'for' statement

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
ethan22boone
 
Posts: 16
Joined: Thu Apr 03, 2014 11:16 pm

Multiple pins controlled by one 'for' statement

Post by ethan22boone »

I have three neopixel strands each different in length. I cant figure out how to combine there code into one 'for' statement in order to have them all do something simultaneously.

This is the first 'for' line for the 144 neopixel strip
//Strip.
for (i=0; i < strip_A.numPixels()/2; i++) {
strip_A.setPixelColor(i, c1);
strip_A.setPixelColor(i + strip_A.numPixels()/2, c2);
strip_A.show();
delay(wait);

This is the first 'for' line for the 12 neopixel ring
//Ring.uint
for (i=0; i < strip_B.numPixels()/2; i++) {
strip_B.setPixelColor(i, c5);
strip_B.show();
delay(600);

I have a third bit of code for 4 flora pixels but if i can just get some understanding on combining these two ill be able to expand from there.

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

Re: Multiple pins controlled by one 'for' statement

Post by adafruit_support_bill »

Since 144 is an even multiple of 12, something like this should work:

Code: Select all

for (i=0; i < strip_A.numPixels()/2; i++) 
{ 
  strip_A.setPixelColor(i, c1); 
  strip_A.setPixelColor(i + strip_A.numPixels()/2, c2);
  strip_A.show(); 
  
  if (i % 12 == 0) // if i is an even multiple of 12
  {
    strip_B.setPixelColor(i/12, c5); 
    strip_B.show();
  }

  delay(wait);
}

User avatar
ethan22boone
 
Posts: 16
Joined: Thu Apr 03, 2014 11:16 pm

Re: Multiple pins controlled by one 'for' statement

Post by ethan22boone »

Thank you for the reply. I only have one strip at the moment so I've been swapping pins to check the code, but I'm not sure if that's an accurate way to check. This code looks like it's what I need and Ill test it when my ring gets here tomorrow, or monday. Thanks again, I'm sure I'll have many more questions in the future!

User avatar
ethan22boone
 
Posts: 16
Joined: Thu Apr 03, 2014 11:16 pm

Re: Multiple pins controlled by one 'for' statement

Post by ethan22boone »

Actually, I've thought of the best way to phrase what I'm trying to do. This may be what you've coded for me already, but, during a basic colorChase of strip_A (144 pixels) I'd like every 12th, or every pixel divisible by 12 to illuminate 1 pixel on strip_B (12 pixels). It would look a little like this;

strip_A 1...12...24...36...48...60...72...84...96...108...120...132...144
strip_B 0 1 2 3 4 5 6 7 8 9 10 11 12

I apologize if this is basically what you've already coded for me, I'm very new to the Arduino and to coding so I'm trying to ask as many questions as possible.

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

Re: Multiple pins controlled by one 'for' statement

Post by adafruit_support_bill »

That is they way I understood it. I don't have a setup to test the code right now, but I believe it should be close to that.

User avatar
ethan22boone
 
Posts: 16
Joined: Thu Apr 03, 2014 11:16 pm

Re: Multiple pins controlled by one 'for' statement

Post by ethan22boone »

Okay awesome, I'll wait till my other strips get here then get back to you with the results. Thank you for the help, I'll reply to this post on Monday with the results!

User avatar
ethan22boone
 
Posts: 16
Joined: Thu Apr 03, 2014 11:16 pm

Re: Multiple pins controlled by one 'for' statement

Post by ethan22boone »

I guess while I wait till Monday, I do have a few very basic questions that will greatly help my overall understanding of how to write this code.
My first question is; within most sample code, and the code I have so far, the integer has been set to 'i'.
So most of my code starts with "for" ('i'=0; or "if" ('i' % 12==0
I know that = is an assignment operator, so what exactly does the 'i' represent and why is it always set = to zero. I apologize for the basic nature of this question and hope its not a bother to you.

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

Re: Multiple pins controlled by one 'for' statement

Post by adafruit_support_bill »

The 'i' is the loop variable. Its value changes on every pass through the loop. In this case. we initialize it to zero "i = 0" because that is the first pixel in your strip. We test for "i < 144" so we can stop when we are at the last pixel in the strip. And the "i++" increments the value of i after each pass through the loop.

http://arduino.cc/en/Reference/For
Image

User avatar
ethan22boone
 
Posts: 16
Joined: Thu Apr 03, 2014 11:16 pm

Re: Multiple pins controlled by one 'for' statement

Post by ethan22boone »

Well the code does exactly what its suppose to, but each of my "for" statements only go to half of the strip in a double chase. so the strip_B (12 pixels) stops half way, as it should. But now I'm stuck on how to get it the rest of the way through the strip.

User avatar
ethan22boone
 
Posts: 16
Joined: Thu Apr 03, 2014 11:16 pm

Re: Multiple pins controlled by one 'for' statement

Post by ethan22boone »

So I think i good solution would be to illuminate two pixels of strip_B for every 12 on strip_A.
I just cant seem to make it work.

User avatar
ethan22boone
 
Posts: 16
Joined: Thu Apr 03, 2014 11:16 pm

Re: Multiple pins controlled by one 'for' statement

Post by ethan22boone »

Haha, i apologize for this but it just hit me after a long break from it. I simply changed the multiple to 6 rather than 12. Thank you for all your help this far, ill be sure to ask you if i have any more questions.

User avatar
ethan22boone
 
Posts: 16
Joined: Thu Apr 03, 2014 11:16 pm

Re: Multiple pins controlled by one 'for' statement

Post by ethan22boone »

Okay, here is a question I have no idea about, the double colorChase you have been helping me with runs in the "setup" only. My "loop" is the "rainbowCycle". I'm very unsure on how to once again combine my strip_B(12 pixels) into this "for" statement.

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

Re: Multiple pins controlled by one 'for' statement

Post by adafruit_support_bill »

My "loop" is the "rainbowCycle". I'm very unsure on how to once again combine my strip_B(12 pixels) into this "for" statement.
The first step is to define precisely what you want the 12-pixel strip to do relative to what is happening on the 144 pixel strip.

User avatar
ethan22boone
 
Posts: 16
Joined: Thu Apr 03, 2014 11:16 pm

Re: Multiple pins controlled by one 'for' statement

Post by ethan22boone »

Is it possible to have two bits of separate code read at the same time? Having to base strip_B off of what strip_A does limits me greatly do to my inexperience with any type of code. If not, then Id like to start with this; "void setup"- strip_A will do a double colorChase, while strip_B will fade from 0-255 in responce to strip_A.
"void loop"- strip_A will do a "rainbowCycle" while strip_B will do a normal "rainbow". strip_B is a ring so "rainbowCycle" just looks white.
I really appreciate your help! Thank you for baring with me.

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

Re: Multiple pins controlled by one 'for' statement

Post by adafruit_support_bill »

Is it possible to have two bits of separate code read at the same time?
That is known as "multi-threading" and it is not supported on the Arduino. Multithreading is a double-edged sword. Most processors that do support multithreading cannot maintain the timing precision required to drive the Neopixels. There are ways to achieve similar results without multithreading, but that is a whole different approach to programming.
"void setup"- strip_A will do a double colorChase, while strip_B will fade from 0-255 in responce to strip_A.
Using the same basic technique as I showed you before, write a loop to do the double color chase on the 144 pixel strip. Then inside your loop, you can use your loop variable to calculate the fade value for the 12 pixel strip. To convert the 0-144 of the loop variable to the 0-255 of the fade, the "Map" function will be useful: http://arduino.cc/en/reference/map

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

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