0

Controlling Dual PixelRIngs
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Controlling Dual PixelRIngs

by nominalindltd on Wed Nov 07, 2018 11:38 pm

For the last few weeks, I've been toying with various animations in the guide for the goggles here using a GemmaM0. I say "toying" because I haven't really coded anything since an intro level class about 25 years ago (coincidentally, that language was "Ada"). Needless to say, I had a lot of catching up to do. I did manage to get the goggles up and working, though, and pieced together some effects using pieces of the example codes I've seen on other Neo Pixel ring projects here, and even managed to figure out how to implement functions that I can call to for a flicker effect, and that sort of thing. So I'm doing better than I'd anticipated, which is good!

Here's the "spinny wheels" code straight from the example linked above:
Code: Select all | TOGGLE FULL SIZE
# Spinny wheels (8 LEDs on at a time)
    elif mode == 1:
        for i in range(0, numpix):
            c = 0
 
            # 4 pixels on...
            if ((offset + i) & 7) < 2:
                c = color
 
            pixels[i] = c  # First eye
            pixels[(numpix - 1) - i] = c  # Second eye (flipped)
 
        pixels.write()
        offset += 1
        time.sleep(0.05)


I have a couple questions. First, how does this line work?

Code: Select all | TOGGLE FULL SIZE
            if ((offset + i) & 7) < 2:

From what I've seen on other python forums it looks like the "&" is a bitwise AND operator, but I'm not sure what exactly it does in this context here. I've been able to figure out that the last digit (the '2' in this case) appears to affect how big the "block" of rotating LEDs is, although I'm not sure why.

My next question is: is there a relatively simple way to make it appear that only one NeoPixel ring is spinning, while the other ring "holds" its position, and roughly how would I go about that? Am I right in suspecting that the line I was unclear on above is basically a really elegant way of selecting which LEDs will light, and trying to not have it affect both rings would be... considerably less elegant?

Thank you for having such awesome products and resources here. I've had a lot of fun working on this project, even when I'm confused!

nominalindltd
 
Posts: 2
Joined: Mon Oct 29, 2018 2:50 pm

Re: Controlling Dual PixelRIngs

by adafruit_support_bill on Thu Nov 08, 2018 6:50 am

I have a couple questions. First, how does this line work?
if ((offset + i) & 7) < 2:


As you have discovered, the '&' is a bitwise AND operator. So you need to look what happens at the binary level. The decimal value 7 is 00000111 in binary. So the & operator will strip off everything but the low order 3 bits from the value of 'offset'. This effectively causes offset to 'wrap around' to zero when the value gets larger than 7.

My next question is: is there a relatively simple way to make it appear that only one NeoPixel ring is spinning


The two eyes are updated here. Eliminate the second line to stop the second eye from spinning.
Code: Select all | TOGGLE FULL SIZE
            pixels[i] = c  # First eye
            pixels[(numpix - 1) - i] = c  # Second eye (flipped)

adafruit_support_bill
 
Posts: 70027
Joined: Sat Feb 07, 2009 10:11 am

Re: Controlling Dual PixelRIngs

by nominalindltd on Thu Nov 08, 2018 6:41 pm

Actually, what I get if I comment out the line for either the First eye or Second eye is both rings spinning the same direction (normally they're opposite). They may be out of sync with each other, too, but it also could just be that my own code is adapted for the particular orientation of my rings. I did find that I can blank one ring entirely if I replace the "c" with 0.

I think you may have still nudged me in the right direction, though. If it addresses all of the LEDs on both rings sequentially (that is, with two 16 LED rings, they'd be numbered... 0-31, I think? Then maybe I can figure out one condition for 0-15 and another for 16-31.

I'll see what I can come up with. Thanks for the assist!

nominalindltd
 
Posts: 2
Joined: Mon Oct 29, 2018 2:50 pm

Please be positive and constructive with your questions and comments.