HELP PLEASE: Media Dial

Adafruit's tiny microcontroller platform. Please tell us which board you are using.
For CircuitPython issues, ask in the Adafruit CircuitPython forum.

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
in_cognito
 
Posts: 5
Joined: Sat Jun 16, 2018 5:42 pm

HELP PLEASE: Media Dial

Post by in_cognito »

Hello, I have been working on the media dial project. I have followed all the instructions and wired everything to the the correct pins. I uploaded the code that was provided and nothing works.

I have uploaded the basic onboard LED flashing code and it works so I know how to upload codes. I also checked all the libraries are there. I think something is wrong with the code but this is my first experience with Trinket so figuring it out is difficult. Please help me.

https://learn.adafruit.com/media-dial/code

My code:

Code: Select all

import board
import time
import digitalio
from adafruit_hid.keyboard import Keyboard
from adafruit_hid.keycode import Keycode
import neopixel
 
DOT_COLOR = 0xFF0000              # set to your favorite webhex color
PRESSED_DOT_COLOR = 0x008080      # set to your second-favorite color
LIT_TIMEOUT = 15                  # after n seconds, turn off ring

# NeoPixel LED ring on pin D1
# Ring code will auto-adjust if not 16 so change to any value!
ring = neopixel.NeoPixel(D1, 16, brightness=0.2)
dot_location = 0  # what dot is currently lit

# Encoder button is a digital input with pullup on D2
button = DigitalInOut(D2)
button.direction = Direction.INPUT
button.pull = Pull.UP

# Rotary encoder inputs with pullup on D3 & D4
rot_a = DigitalInOut(D3)
rot_a.direction = Direction.INPUT
rot_a.pull = Pull.UP
rot_b = DigitalInOut(D4)
rot_b.direction = Direction.INPUT
rot_b.pull = Pull.UP

# Used to do HID output, see below
kbd = Keyboard()

# time keeper, so we know when to turn off the LED
timestamp = time.monotonic()


# the counter counts up and down, it can roll over! 16-bit value
encoder_counter = 0
# direction tells you the last tick which way it went
encoder_direction = 0

# constants to help us track what edge is what
A_POSITION = 0
B_POSITION = 1
UNKNOWN_POSITION = -1  # initial state so we know if something went wrong

rising_edge = falling_edge = UNKNOWN_POSITION

# get initial/prev state and store at beginning
last_button = button.value
rotary_prev_state = [rot_a.value, rot_b.value]

while True:
    # reset encoder and wait for the next turn
    encoder_direction = 0

    # take a 'snapshot' of the rotary encoder state at this time
    rotary_curr_state = [rot_a.value, rot_b.value]

    if rotary_curr_state != rotary_prev_state:
        # print("Changed")
        if rotary_prev_state == [True, True]:
            # we caught the first falling edge!
            if not rotary_curr_state[A_POSITION]:
                # print("Falling A")
                falling_edge = A_POSITION
            elif not rotary_curr_state[B_POSITION]:
                # print("Falling B")
                falling_edge = B_POSITION
            else:
                # uhh something went deeply wrong, lets start over
                continue

        if rotary_curr_state == [True, True]:
            # Ok we hit the final rising edge
            if not rotary_prev_state[B_POSITION]:
                rising_edge = B_POSITION
                # print("Rising B")
            elif not rotary_prev_state[A_POSITION]:
                rising_edge = A_POSITION
                # print("Rising A")
            else:
                # uhh something went deeply wrong, lets start over
                continue

            # check first and last edge
            if (rising_edge == A_POSITION) and (falling_edge == B_POSITION):
                encoder_counter -= 1
                encoder_direction = -1
                print("%d dec" % encoder_counter)
            elif (rising_edge == B_POSITION) and (falling_edge == A_POSITION):
                encoder_counter += 1
                encoder_direction = 1
                print("%d inc" % encoder_counter)
            else:
                # (shrug) something didn't work out, oh well!
                encoder_direction = 0

            # reset our edge tracking
            rising_edge = falling_edge = UNKNOWN_POSITION

    rotary_prev_state = rotary_curr_state

    # Check if rotary encoder went up
    if encoder_direction == 1:
        kbd.press(Keycode.CONTROL, Keycode.UP_ARROW)
        kbd.release_all()

    # Check if rotary encoder went down
    if encoder_direction == -1:
        kbd.press(Keycode.CONTROL, Keycode.DOWN_ARROW)
        kbd.release_all()

    # Button was 'just pressed'
    if (not button.value) and last_button:
        print("Button pressed!")
        kbd.press(44)  # Keycode.SPACE
        kbd.release_all()
        ring[dot_location] = PRESSED_DOT_COLOR  # show it was pressed on ring
        timestamp = time.monotonic()        # something happened!
    elif button.value and (not last_button):
        print("Button Released!")
        # kbd.press(Keycode.SHIFT, Keycode.SIX)
        # kbd.release_all()
        ring[dot_location] = DOT_COLOR      # show it was released on ring
        timestamp = time.monotonic()        # something happened!
    last_button = button.value

    if encoder_direction != 0:
        timestamp = time.monotonic()        # something happened!
        # spin neopixel LED around!
        previous_location = dot_location
        dot_location += encoder_direction   # move dot in the direction
        dot_location += len(ring)  # in case we moved negative, wrap around
        dot_location %= len(ring)
        if button.value:
            ring[dot_location] = DOT_COLOR  # turn on new dot
        else:
            ring[dot_location] = PRESSED_DOT_COLOR  # turn on new dot
        ring[previous_location] = 0         # turn off previous dot

    if time.monotonic() > timestamp + LIT_TIMEOUT:
        ring[dot_location] = 0   # turn off ring light temporarily

User avatar
adafruit_support_carter
 
Posts: 29151
Joined: Tue Nov 29, 2016 2:45 pm

Re: HELP PLEASE: Media Dial

Post by adafruit_support_carter »

The top of your code looks a little different than the guide code. You have this:

Code: Select all

import board
import time
import digitalio
from adafruit_hid.keyboard import Keyboard
from adafruit_hid.keycode import Keycode
import neopixel
but the guide has this:

Code: Select all

import time
from digitalio import *
from board import *
from adafruit_hid.keyboard import Keyboard
from adafruit_hid.keycode import Keycode
import neopixel
Did you make those changes?

User avatar
in_cognito
 
Posts: 5
Joined: Sat Jun 16, 2018 5:42 pm

Re: HELP PLEASE: Media Dial

Post by in_cognito »

I did make those changes. The original code straight off the page doesn't work (I have tried it multiple times) and neither do any edits I make.

User avatar
adafruit_support_carter
 
Posts: 29151
Joined: Tue Nov 29, 2016 2:45 pm

Re: HELP PLEASE: Media Dial

Post by adafruit_support_carter »

Your edits will change the way in which the rest of the code will need to refer to the modules. What errors do you get if you run the code as is from the guide?

User avatar
in_cognito
 
Posts: 5
Joined: Sat Jun 16, 2018 5:42 pm

Re: HELP PLEASE: Media Dial

Post by in_cognito »

Okay so I completely restarted and loaded the original code from the site. There aren't any errors i can see.

I am using Mu to edit the code and when I click save, it confirms that the code is saved. The code which is named "code.py" shows up in the CIRCUITPY folder like it should and then my whole assembly just sits there. When I turn the rotary encoder, nothing happens on the LED ring. The Trinket sits with the green light on (so it is powered).

I don't seem to get errors although I am not sure how I would know where the error would show up. The Trinket doesn't start flashing or doing anything distressing. So far as I can tell, the code saves onto the Trinket, and it just sits there.

User avatar
adafruit_support_carter
 
Posts: 29151
Joined: Tue Nov 29, 2016 2:45 pm

Re: HELP PLEASE: Media Dial

Post by adafruit_support_carter »

Could be a couple of things. Let's see what your setup looks like. Post a photo showing how everything is connected.

User avatar
in_cognito
 
Posts: 5
Joined: Sat Jun 16, 2018 5:42 pm

Re: HELP PLEASE: Media Dial

Post by in_cognito »

So to get around the file size limit, I am posting links to streamable.

Everything should be wired up right, I have checked it so many times so if it is wrong, I have zero clue. I can solder the wires direct to the trinket if that may be an issue but I wanted everything to work before I did that. Also I still have to design and built the enclosure for all this.

https://streamable.com/8q5uw

https://streamable.com/wf3pw

https://streamable.com/f5w20

https://streamable.com/1lmdv

https://streamable.com/feo0l

https://streamable.com/l3hrh

https://streamable.com/hrz4s

User avatar
adafruit_support_carter
 
Posts: 29151
Joined: Tue Nov 29, 2016 2:45 pm

Re: HELP PLEASE: Media Dial

Post by adafruit_support_carter »

You need to solder these wires on the Trinket:
IMG_004.jpg
IMG_004.jpg (58.24 KiB) Viewed 166 times
Or you could solder on headers so that you could put the Trinket on the breadboard.

User avatar
in_cognito
 
Posts: 5
Joined: Sat Jun 16, 2018 5:42 pm

Re: HELP PLEASE: Media Dial

Post by in_cognito »

So my connections aren't good? I will solder them tomorrow then.

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

Return to “Trinket ATTiny, Trinket M0”