0

Code help request, import board *?
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Code help request, import board *?

by PeteH51 on Sat Jan 12, 2019 8:57 am

Hi again,

Very new to CircuitPython, sorry for the multitude of questions. I've gotten some great advice so far.

Trying to get Trinket MO to do the footswitch thing
Struggling with this code I got from https://learn.adafruit.com/USB-foot-switch-circuit-python/software
Two errors show up: I think the main one is "from board import *" results in " unable to detect undefined names"
The other error is [D0] is undefined, or defined from star imports:board

I was hoping that this code would be error free, but it doesn't seem to work for me. Any ideas?

Code: Select all | TOGGLE FULL SIZE
import digitalio
from board import *
import time
from adafruit_hid.keyboard import Keyboard
from adafruit_hid.keycode import Keycode

# A simple neat keyboard demo in circuitpython
buttonpins = [D0]

# The keycode sent for each button, optionally can be paired with a control key
buttonkeys = [44]
controlkey = Keycode.LEFT_CONTROL

# the keyboard object!
kbd = Keyboard()

# our array of button objects
buttons = []

# make all pin objects, make them inputs w/pullups
for pin in buttonpins:
    button = digitalio.DigitalInOut(pin)
    button.direction = digitalio.Direction.INPUT
    button.pull = digitalio.Pull.UP   
    buttons.append(button)
 
print("Waiting for button presses")

while True:
    # check each button
    for button in buttons:
        if (not button.value):   # pressed?
            i = buttons.index(button)
           
            print("Button #%d Pressed" % i)

            # type the keycode!
            k = buttonkeys[i]    # get the corresp. keycode
            kbd.press(k)
            # Use this line for key combos kbd.press(k, controlkey)
            kbd.release_all()

            while (not button.value):
                pass  # wait for it to be released!
    time.sleep(0.01)

PeteH51
 
Posts: 18
Joined: Sun Jan 06, 2019 12:04 pm

Re: Code help request, import board *?

by PeteH51 on Sat Jan 12, 2019 9:30 am

so after further investigation this morning I can at least use REPL to import board and do a dir(board) to see the names there. Indeed D0 shows up, but is still undefined in my code. I may end up answering my own question, but please feel free to add any help.

PeteH51
 
Posts: 18
Joined: Sun Jan 06, 2019 12:04 pm

Re: Code help request, import board *?

by PeteH51 on Sun Jan 13, 2019 8:57 am

OK further investigation, but problems.

If I add D0 in place of the *, Mu doesn't flag any errors....but...

When I run the code on the Trinket MO I get flashing error lights.
Sequence is
Green, purple and five blue flashes.
I've looked at decoding this and have decided that I still don't understand it.

Thanks for looking,
Pete

PeteH51
 
Posts: 18
Joined: Sun Jan 06, 2019 12:04 pm

Re: Code help request, import board *?

by adafruit_support_carter on Thu Jan 17, 2019 3:32 pm

Try running the following commands from the REPL and see what you get.
Code: Select all | TOGGLE FULL SIZE
from board import *
type(D0)

adafruit_support_carter
 
Posts: 10410
Joined: Tue Nov 29, 2016 2:45 pm

Re: Code help request, import board *?

by PeteH51 on Sat Jan 19, 2019 1:15 pm

Thanks for the reply here's what I did that seemed to work.

from board import D0

Thanks,

PeteH51
 
Posts: 18
Joined: Sun Jan 06, 2019 12:04 pm

Re: Code help request, import board *?

by adafruit_support_carter on Sat Jan 19, 2019 1:26 pm

That will work, but only bring in D0. It sounds like when you did a "from board import *" D0 did not show up. But it should. So those two lines above were to check that behavior.

adafruit_support_carter
 
Posts: 10410
Joined: Tue Nov 29, 2016 2:45 pm

Re: Code help request, import board *?

by PeteH51 on Sat Jan 19, 2019 1:53 pm

agreed, I could see the board by doing a 'dir' and it was all there. Works anyway, but now I'm going back to see if I can get my trinket (not MO) to do similar things. See my other post

PeteH51
 
Posts: 18
Joined: Sun Jan 06, 2019 12:04 pm

Please be positive and constructive with your questions and comments.