Black Lives Matter - Action and Equality. ... Adafruit is open and shipping.
0

help debug, need to print pin#
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

help debug, need to print pin#

by Rcayot on Fri Jun 05, 2020 11:25 am

I am trying to do some circuitpython coding. basically trying to replicate some of the work I have done using Arduino.

This is a blinky type program, I am trying to get my leds to light up in one direction and then the other. Without ther "leds.reverse()" line the program runs fine, that is it lights one led at a time in sequence. I('d like to get it to run backwards, by using the list reverse function, but when I do, it goes a little nuts. I cannot seem to be able to debug by printing the led pin#. If I print " print(leds[index])" the return is <PWM> or some such. I am thinking the issue is WHERE I place teh "leds.reverse()" function, but I cannot really tell what is going on. Is there a way to print the pin number?

Thanks,

Roger

Code: Select all | TOGGLE FULL SIZE
import board
import pulseio
import time

l1 = pulseio.PWMOut(board.D1, frequency=500, duty_cycle=0)
l2 = pulseio.PWMOut(board.D2, frequency=500, duty_cycle=0)
l3 = pulseio.PWMOut(board.D3, frequency=500, duty_cycle=0)
l4 = pulseio.PWMOut(board.D4, frequency=500, duty_cycle=0)
l5 = pulseio.PWMOut(board.D5, frequency=500, duty_cycle=0)
l6 = pulseio.PWMOut(board.D6, frequency=500, duty_cycle=0)
l7 = pulseio.PWMOut(board.D7, frequency=500, duty_cycle=0)
l8 = pulseio.PWMOut(board.D8, frequency=500, duty_cycle=0)
l9 = pulseio.PWMOut(board.D9, frequency=500, duty_cycle=0)
l10 = pulseio.PWMOut(board.D10, frequency=500, duty_cycle=0)
l11 = pulseio.PWMOut(board.D11, frequency=500, duty_cycle=0)
l12 = pulseio.PWMOut(board.D12, frequency=500, duty_cycle=0)


leds = [l1, l2, l3, l4, l5, l6, l7, l8, l9, l10, l11, l12]
pattern1 = [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

def duty_cycle_value(percent):   
    return int(percent * 65535)
i = 0
deltat = 1.00
initial = time.monotonic()
index = 0
while True:
    now = time.monotonic()
    if now - initial > .01:
        if index == 0:
            temp = pattern1[index]
        if index < 11:
            pattern1[index] = pattern1[index+1]
        if index == 11:
            pattern1[index] = temp
        leds[index].duty_cycle = duty_cycle_value(pattern1[index])
        # print(pattern1[index])
        # print(leds[index])
        index = index + 1
        if index == 12:
            index = 0
            # leds.reverse()
            initial = now 

Rcayot
 
Posts: 41
Joined: Sat Feb 08, 2020 6:48 pm

Re: help debug, need to print pin#

by siddacious on Thu Jun 18, 2020 12:32 pm

You're very close!
Regarding printing, `print(leds[index])` is using the current index to choose a pin object from the list of pin objects you created earlier in the program and then printing the pin object which isn't a number or string so you're seeing CircuitPython's best attempt to convert it to a string, "<PWM> "or whatever.

Your `leds` list has the pins in order starting with D1 up to D12, so just `index`+1 (since `index` starts at zero) will give you the pin number (minus the D).

With that said, I suspect I know what the issue is with the `reverse` not working as expected, and it's basically the same issue as you were running into with printing: the list you are trying to reverse is a list of PWM objects, not a list of numbers so it is sorting on ??? I don't know, maybe the object ID? Regardless it's not what you're after.

I would suggest that instead of using `index` to directly pull a PWM/pin object out of the list, you use an intermediary list that contains indices into the list of pins. Here's the jist of what I'm suggesting:
Code: Select all | TOGGLE FULL SIZE
>>> PIN_COUNT = 12
>>> pins  = ["pin_%d"%(i+1) for i in range(PIN_COUNT)]
>>> pins
['pin_1', 'pin_2', 'pin_3', 'pin_4', 'pin_5', 'pin_6', 'pin_7', 'pin_8', 'pin_9', 'pin_10', 'pin_11', 'pin_12']
>>> pin_order = list(range(PIN_COUNT))
>>> pin_order
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
>>> for index in range(PIN_COUNT):
...     print("index:", index, "current pin:", pins[pin_order[index]])
...
index: 0 current pin: pin_1
index: 1 current pin: pin_2
index: 2 current pin: pin_3
index: 3 current pin: pin_4
index: 4 current pin: pin_5
index: 5 current pin: pin_6
index: 6 current pin: pin_7
index: 7 current pin: pin_8
index: 8 current pin: pin_9
index: 9 current pin: pin_10
index: 10 current pin: pin_11
index: 11 current pin: pin_12
>>> pin_order.reverse()
>>> pin_order
[11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
>>> for index in range(PIN_COUNT):
...     print("index:", index, "current pin:", pins[pin_order[index]])
...
index: 0 current pin: pin_12
index: 1 current pin: pin_11
index: 2 current pin: pin_10
index: 3 current pin: pin_9
index: 4 current pin: pin_8
index: 5 current pin: pin_7
index: 6 current pin: pin_6
index: 7 current pin: pin_5
index: 8 current pin: pin_4
index: 9 current pin: pin_3
index: 10 current pin: pin_2
index: 11 current pin: pin_1
>>>


I took the liberty to use a few niceties that Python makes available,
list comprehensions (https://www.pythonforbeginners.com/basi ... in-python/) to create the lists, and
ranges (https://docs.python.org/3/library/stdtypes.html#ranges)

Also note that you could use a backwards counting range (see the docs above) to move backwards through the list of pins rather than reversing a list. There are many ways to pet this cat!

siddacious
 
Posts: 271
Joined: Fri Apr 21, 2017 3:09 pm

Re: help debug, need to print pin#

by Rcayot on Mon Jun 22, 2020 7:26 am

Thank you, very helpful.

Rcayot
 
Posts: 41
Joined: Sat Feb 08, 2020 6:48 pm

Please be positive and constructive with your questions and comments.