0

fade Neopixel strip with slider
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

fade Neopixel strip with slider

by scotch1 on Wed Sep 22, 2021 1:02 am

Trying to turn a neopixel strip into a fader using analog PWM inputs. Using an M4 with circuitpython. (Plan is to convert to Arduino).

AM able to map the values of analog input (voltage from 0 to 3.3) to the number of neopixels (I.e. 0=0, 3.3v=8) using this examplehttps://learn.adafruit.com/led-breath-stats-mask/circuitpython-code and can get it to light UP on the first go, but can't reduce the effect with a lower PWM value since the pixels are "on". Basic look is that of a fader that corresponds to a slider or potentiometer level.

The noted example works but the pixels have that blinking (breathing effect) and would prefer to keep them on. Any advice to sequentially turn off only the ones that aren't meeting the higher (previous) value rather rgab scrub all of them? Is there a simple example of code someone has already done for this? Similar to LED fade for Arduino?


Current COde:
Code: Select all | TOGGLE FULL SIZE
"""CircuitPython Essentials Analog In example"""
import time
import board
from analogio import AnalogIn

import neopixel

analog_in = AnalogIn(board.A1)
pixel_pin = board.D11  ##for M4
num_leds = 8
##ORDER = neopixel.GRB
tvoc_pix = neopixel.NeoPixel(pixel_pin, num_leds, brightness=0.3, auto_write=False)

led_draw = .05  # delay for LED pixel turn on/off

def clear_pix(delay):
    # clear all LEDs for breathing effect
    for i in range(0, num_leds):
        tvoc_pix[i] = (0, 0, 0)
        time.sleep(delay)

def tvoc_led_meter():
    # Show Total Volatile Organic Compounds on a NeoPixel Strip
    tvoc_floor = 0.0
    tvoc_ceiling = 3.3

    # Map CO2 range to 8 LED NeoPixel Stick
    tvoc_range = tvoc_ceiling - tvoc_floor
    tvoc_led_steps = tvoc_range / num_leds
    volts = get_voltage(analog_in)
    print(volts)
    tvoc_leds = int(volts / tvoc_led_steps)
    print(tvoc_leds)

    # Insert Colors
    for i in range(0, (tvoc_leds +1)):
        tvoc_pix[i] = (10, 90, 25)
        print(tvoc_pix[i])
        time.sleep(led_draw)

def get_voltage(pin):
    return (pin.value * 3.3) / 65536


while True:
    print(get_voltage(analog_in))
    tvoc_led_meter()
    time.sleep(0.5)
    ##clear_pix(led_draw)
    tvoc_pix.show()

scotch1
 
Posts: 240
Joined: Mon Nov 26, 2012 12:13 am

Re: fade Neopixel strip with slider

by dastels on Wed Sep 22, 2021 9:13 am

When you set the pixel colors, loop over all of them, not just the ones that should be on: set the ones that should be on to the appropriate color, set the others to (0, 0, 0). Also, why aren't you showing immediately after setting the colors?

Dave

dastels
 
Posts: 7543
Joined: Tue Oct 20, 2015 3:22 pm

Re: fade Neopixel strip with slider

by scotch1 on Thu Sep 23, 2021 2:14 am

As in:
Code: Select all | TOGGLE FULL SIZE
    for i in range(0, num_leds):
        if i <= tvoc_leds:
            tvoc_pix[i] = (10, 90, 25)
        else:
            tvoc_pix[i] = (0,0,0)
        tvoc_pix.show()


Seems to work :) Can't believe you made me figure that out myself: NIce what 15 minutes of silence and rereading your note over and over can do :) Appreciate the mentoring approach :)

scotch1
 
Posts: 240
Joined: Mon Nov 26, 2012 12:13 am

Re: fade Neopixel strip with slider

by dastels on Thu Sep 23, 2021 7:54 am

Nicely done. It's less fun if you're just handed the answer... and you tend to learn far less.

You can do fun things like set the brightness of the LED "on the border" to indicate a partial turning on for values between the two.

Dave

dastels
 
Posts: 7543
Joined: Tue Oct 20, 2015 3:22 pm

Please be positive and constructive with your questions and comments.