code.py
 1  # SPDX-FileCopyrightText: 2021 Kattni Rembor for Adafruit Industries
 2  #
 3  # SPDX-License-Identifier: MIT
 4  
 5  """
 6  CircuitPython ANO Rotary Encoder and NeoPixel Ring example.
 7  """
 8  import board
 9  import digitalio
10  import rotaryio
11  import neopixel
12  
13  # The pin assignments for the breakout pins. Update this is you are not using a Feather.
14  ENCA = board.D13
15  ENCB = board.D12
16  COMA = board.D11
17  SW1 = board.D10
18  SW2 = board.D9
19  SW3 = board.D6
20  SW4 = board.D5
21  SW5 = board.SCL
22  COMB = board.SDA
23  
24  # Rotary encoder setup
25  encoder = rotaryio.IncrementalEncoder(ENCA, ENCB)
26  last_position = None
27  
28  # NeoPixel ring setup. Update num_pixels if using a different ring.
29  num_pixels = 12
30  pixels = neopixel.NeoPixel(board.A0, num_pixels, auto_write=False)
31  
32  # Set the COMA and COMB pins LOW. This is only necessary when using the direct-to-Feather or other
33  # GPIO-based wiring method. If connecting COMA and COMB to ground, you do not need to include this.
34  com_a = digitalio.DigitalInOut(COMA)
35  com_a.switch_to_output()
36  com_a = False
37  com_b = digitalio.DigitalInOut(COMB)
38  com_b.switch_to_output()
39  com_b = False
40  
41  # Button pin setup
42  button_pins = (SW1, SW2, SW3, SW4, SW5)
43  buttons = []
44  for button_pin in button_pins:
45      pin = digitalio.DigitalInOut(button_pin)
46      pin.switch_to_input(digitalio.Pull.UP)
47      buttons.append(pin)
48  
49  while True:
50      position = encoder.position
51      if last_position is None or position != last_position:
52          print("Position: {}".format(position))
53          last_position = position
54  
55      pixels.fill((0, 0, 0))
56      pixels[position % num_pixels] = (0, 150, 0)
57  
58      if not buttons[0].value:
59          print("Center button!")
60          pixels.fill((100, 100, 100))
61  
62      if not buttons[1].value:
63          print("Up button!")
64          pixels[0] = (150, 0 ,0)
65  
66      if not buttons[2].value:
67          print("Left button!")
68          pixels[3] = (150, 0, 0)
69  
70      if not buttons[3].value:
71          print("Down button!")
72          pixels[6] = (150, 0, 0)
73  
74      if not buttons[4].value:
75          print("Right button!")
76          pixels[9] = (150, 0, 0)
77  
78      pixels.show()