Coding for 2 Wheels

CircuitPython on hardware including Adafruit's boards, and CircuitPython libraries using Blinka on host computers.

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
Thormod
 
Posts: 14
Joined: Sat Oct 21, 2017 5:42 pm

Coding for 2 Wheels

Post by Thormod »

OK I am stumbling through coding and I thought that I would try taking one of your sample codes for a neopixel strip and attempting to apply it to two neopixel wheels.
It is not working and I am stuck as to why.

Here is your original code which works on one wheel. I had to change the numpix to 16 of course but it works quite fine:

Code: Select all

# CircuitPython demo - NeoPixel
 
import board
import neopixel
import time
 
pixpin = board.D1
numpix = 10
 
strip = neopixel.NeoPixel(pixpin, numpix, brightness=0.3, auto_write=False)
 
 
def wheel(pos):
    # Input a value 0 to 255 to get a color value.
    # The colours are a transition r - g - b - back to r.
    if (pos < 0) or (pos > 255):
        return (0, 0, 0)
    if (pos < 85):
        return (int(pos * 3), int(255 - (pos*3)), 0)
    elif (pos < 170):
        pos -= 85
        return (int(255 - pos*3), 0, int(pos*3))
    else:
        pos -= 170
        return (0, int(pos*3), int(255 - pos*3))
 
def rainbow_cycle(wait):
    for j in range(255):
        for i in range(len(strip)):
            idx = int ((i * 256 / len(strip)) + j)
            strip[i] = wheel(idx & 255)
        strip.write()
        time.sleep(wait)
 
while True:
    strip.fill((255, 0, 0))
    strip.write()
    time.sleep(1)
 
    strip.fill((0, 255, 0))
    strip.write()
    time.sleep(1)
 
    strip.fill((0, 0, 255))
    strip.write()
    time.sleep(1)
 
    rainbow_cycle(0.001)    # rainbowcycle with 1ms delay per step

OK so here is what I did.

First I commented out all of the the 'While True' blinking part since I just wanted to work on the rainbow cycle.  Then I changed pixpin to pixpinLeft and Right and the right board to board.D0
I changed strip to stripLeft and Right and idx to idxL and R.  Lastly (and I suspect that this is the problem) I changed 'write' to 'show' as it is on the goggles code and made it into showLeft and showRight.  I have also tried it as writeLeft and writeRight and just write but no luck.  
What am I doing wrong?

# CircuitPython demo - NeoPixel

import board
import neopixel
import time

pixpinLeft = board.D1
pixpinRight = board.D0
numpix = 16

stripLeft = neopixel.NeoPixel(pixpin, numpix, brightness=0.3, auto_write=False)
stripRight = neopixel.NeoPixel(pixpin, numpix, brightness=0.3, auto_write=False)


def wheel(pos):
    # Input a value 0 to 255 to get a color value.
    # The colours are a transition r - g - b - back to r.
    if (pos < 0) or (pos > 256):
        return (0, 0, 0)
    if (pos < 85):
        return (int(pos * 3), int(255 - (pos*3)), 0)
    elif (pos < 170):
        pos -= 85
        return (int(255 - pos*3), 0, int(pos*3))
    else:
        pos -= 170
        return (0, int(pos*3), int(255 - pos*3))

def rainbow_cycle(wait):
    for j in range(255):
        for i in range(len(stripLeft)):
            idxL = int ((i * 256 / len(stripLeft)) + j)
            stripLeft[i] = wheel(idxL & 255)
            idxR = int ((i * 256 / len(stripRight)) + j)
            stripRight[i] = wheel(idxR & 255)
        stripLeft.show()
	stripRight.show()
        time.sleep(wait)

while True:
   # strip.fill((255, 0, 0))
   # strip.write()
   # time.sleep(1)

   # strip.fill((0, 255, 0))
   # strip.write()
   # time.sleep(1)

   # strip.fill((0, 0, 255))
   # strip.write()
   # time.sleep(1)

    rainbow_cycle(0.001)    # rainbowcycle with 1ms delay per step
Last edited by adafruit_support_bill on Tue Oct 24, 2017 5:45 am, edited 1 time in total.
Reason: Please use [code] tags when submitting code to the forums

User avatar
kattni
 
Posts: 132
Joined: Fri Aug 18, 2017 6:33 pm

Re: Coding for 2 Wheels

Post by kattni »

Hi Thormod!

I don't have two NeoPixel rings to test this, however I think I've identified the issue in your code. Try the following changes:

Code: Select all

import board
import neopixel
import time

pixpinLeft = board.D1
pixpinRight = board.D0
numpix = 16

stripLeft = neopixel.NeoPixel(pixpinLeft, numpix, brightness=0.3, auto_write=False)
stripRight = neopixel.NeoPixel(pixpinRight, numpix, brightness=0.3, auto_write=False)


def wheel(pos):
    # Input a value 0 to 255 to get a color value.
    # The colours are a transition r - g - b - back to r.
    if (pos < 0) or (pos > 255):
        return (0, 0, 0)
    if (pos < 85):
        return (int(pos * 3), int(255 - (pos*3)), 0)
    elif (pos < 170):
        pos -= 85
        return (int(255 - pos*3), 0, int(pos*3))
    else:
        pos -= 170
        return (0, int(pos*3), int(255 - pos*3))

def rainbow_cycle(wait):
    for j in range(255):
        for i in range(len(stripLeft)):
            idxL = int((i * 255 / len(stripLeft)) + j)
            stripLeft[i] = wheel(idxL & 255)
            stripLeft.show()
        for i in range(len(stripRight)):
            idxR = int((i * 255 / len(stripRight)) + j)
            stripRight[i] = wheel(idxR & 255)
            stripRight.show()
        time.sleep(wait)


while True:
    rainbow_cycle(0.001)  # rainbowcycle with 1ms delay per step
You need to separate stripRight and stripLeft in the rainbowcycle function. Hope this helps!
~Kattni

User avatar
Thormod
 
Posts: 14
Joined: Sat Oct 21, 2017 5:42 pm

Re: Coding for 2 Wheels

Post by Thormod »

Ah I was on the right track at least with the 'show' as opposed to 'write.'
Your fix did light both rings up but now the colors no longer rotate. I tried putting different known patterns in after the 'rainbow_cycle' (such as 64 for Chase) so that it read

rainbow_cycle(0.001, 64, 64)

but no effect.

One thing that i noticed is that in the original code it reads:

def rainbow_cycle(wait):
for j in range(255):
for i in range(len(strip)):
idx = int ((i * 256 / len(strip)) + j)
strip = wheel(idx & 255)
strip.write()
time.sleep(wait)

Whereas in your fix you changed

idx = int ((i * 256 / len(strip)) + j)

into

idxL = int((i * 255 / len(stripLeft)) + j)

I tried changing the numerical value in the idx line fix back to 256 (both for idxL and R) but there was no change.

Can you point me to the section that affects rotation pattern?

User avatar
tannewt
 
Posts: 3304
Joined: Thu Oct 06, 2016 8:48 pm

Re: Coding for 2 Wheels

Post by tannewt »

Kattni's code looks like it should rotate to me. The rotation is achieve through the outer loop of j. It drives the animation because it has the time.sleep at the bottom which determines the animation's speed. The inner loops of i are changing the colors for each pixel in the ring.

Thormod, please post your latest code here in a code block and we can take a look.

User avatar
Thormod
 
Posts: 14
Joined: Sat Oct 21, 2017 5:42 pm

Re: Coding for 2 Wheels

Post by Thormod »

I am using her code that she posted but upon double-checking, I see that the colors are rotating, just extremely slowly. This is odd because their rotation was easily visible with the old code on your website and from what I can see, she did not alter anything other that to add 'Left' and 'Right' lines.

This leads me to another interesting experiment and that is how to affect rotation speed.
I am not quite grasping how altering the J loop will affect the rotation. I tried changing the numbers but that did nothing.
Last edited by Thormod on Tue Oct 24, 2017 3:57 pm, edited 1 time in total.

User avatar
kattni
 
Posts: 132
Joined: Fri Aug 18, 2017 6:33 pm

Re: Coding for 2 Wheels

Post by kattni »

I it's rainbow_cycle delay that affects the speed of rotation. Inside the rainbowcycle definition it would be the time.sleep that would affect it. However, that's what the number does in rainbow_cycle at the end of the code.

So this would be a faster rotation.

Code: Select all

while True:
    rainbow_cycle(0.000001)
While this would be slower.

Code: Select all

while True:
    rainbow_cycle(1)
Here is a change that needed to be made to the code:

Code: Select all

import board
import neopixel
import time

pixpinLeft = board.D1
pixpinRight = board.D0
numpix = 16

stripLeft = neopixel.NeoPixel(pixpinLeft, numpix, brightness=0.3 auto_write=False)
stripRight = neopixel.NeoPixel(pixpinRight, numpix, brightness=0.3, auto_write=False)


def wheel(pos):
    # Input a value 0 to 255 to get a color value.
    # The colours are a transition r - g - b - back to r.
    if (pos < 0) or (pos > 255):
        return (0, 0, 0)
    if (pos < 85):
        return (int(pos * 3), int(255 - (pos*3)), 0)
    elif (pos < 170):
        pos -= 85
        return (int(255 - pos*3), 0, int(pos*3))
    else:
        pos -= 170
        return (0, int(pos*3), int(255 - pos*3))


def rainbow_cycle(wait):
    for j in range(255):
        for i in range(len(stripLeft)):
            idxL = int((i * 255 / len(stripLeft)) + j)
            stripLeft[i] = wheel(idxL & 255)
        stripLeft.show()
        for i in range(len(stripRight)):
            idxR = int((i * 255 / len(stripRight)) + j)
            stripRight[i] = wheel(idxR & 255)
        stripRight.show()
        time.sleep(wait)


while True:
    rainbow_cycle(0.00001)  # rainbowcycle with 1ms delay per step
Notice the tab difference on stripLeft.show() and stripRight.show(). Indentations affect how and when commands are run. I altered these two lines and it sped up the change considerably. I'm testing with a single strip, not two rings.

Sorry about the oversight! Hope this helps!
~Kattni

User avatar
Thormod
 
Posts: 14
Joined: Sat Oct 21, 2017 5:42 pm

Re: Coding for 2 Wheels

Post by Thormod »

Kattni,
Fantastic! You have been a great help!
Hopefully you will be available to answer more of my noob questions as they come up!
This stuff is interesting but often confusing and frustrating.

User avatar
kattni
 
Posts: 132
Joined: Fri Aug 18, 2017 6:33 pm

Re: Coding for 2 Wheels

Post by kattni »

Excellent! I'm so glad it worked! Everyone's new to things when they start. Feel free to ask anytime, there's plenty of people who can help. You can check out the Adafruit Discord as well. https://adafru.it/discord It's also full of great people who can offer all kinds of assistance!

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

Return to “Adafruit CircuitPython”