Jewel example error

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
jerryn
 
Posts: 1868
Joined: Sat Sep 14, 2013 9:05 am

Jewel example error

Post by jerryn »

Just an observation.
I was trying out my new Jewel:
https://www.adafruit.com/product/2226
but trying the "rainbow" example from:
https://learn.adafruit.com/gemma-m0-she ... nbow-cycle

the example code did not work - it just left the pixels off
Here is the example:

Code: Select all

# Gemma IO demo - NeoPixel

from digitalio import *
from board import *
import neopixel
import time

pixpin = D2
numpix = 7

led = DigitalInOut(D13)
led.direction = Direction.OUTPUT

strip = neopixel.NeoPixel(pixpin, numpix, brightness=0.3)


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):
        return [0, 0, 0]
    if (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:
    rainbow_cycle(0.001)
I made a few changes to make the return values of "wheel" use "()" instead of "[]" and it now works fine.
I also removed the setting for D13 and the call to strip.write() since they were not needed.
Revised example:

Code: Select all

# Gemma IO demo - NeoPixel

from digitalio import *
from board import *
import neopixel
import time

pixpin = D2
numpix = 7

strip = neopixel.NeoPixel(pixpin, numpix, brightness=0.3)


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):
        return (0, 0, 0)
    if (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)
        time.sleep(wait)

while True:
    rainbow_cycle(0.001)
In the interest of full disclosure, I was running this on a Metro M0 Express, not a gemma but I think this is a simple python typo and not related to the boards.
I'm not proficient enough in Python to clearly explain the difference between "()" and "[]" so I'll let someone else comment of they want.

this works for me - your mileage may vary ;-)

User avatar
jerryn
 
Posts: 1868
Joined: Sat Sep 14, 2013 9:05 am

Re: Jewel example error

Post by jerryn »

minor update - as noted in the Adafruit_CircuitPython_NeoPixel readme,https://github.com/adafruit/Adafruit_Ci ... n_NeoPixel there is a parameter to control whether the pixels are automatically updated as written or if they can be loaded then set together via .show().
I made a change to use this feature and it works nicely on the Jewel. I also added code to turn the neopixels off when the script is exited.
Note: the change from "[]" to "()" for the return tuple from wheel was still necessary!

Code: Select all

# Gemma IO demo - NeoPixel
 
from digitalio import *
from board import *
import neopixel
import time
 
pixpin = D2
numpix = 7
 
#led = DigitalInOut(D13)
#led.direction = Direction.OUTPUT
 
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):
        return (0, 0, 0)
    if (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.show()
        time.sleep(wait)
 
try:
    while True:        
        rainbow_cycle(0.001)
except:
    pass
finally:
    for i in range(len(strip)):
        strip[i] = (0,0,0)
    strip.show()


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

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