code.py
 1  # SPDX-FileCopyrightText: 2018 Kattni Rembor for Adafruit Industries
 2  #
 3  # SPDX-License-Identifier: MIT
 4  
 5  import time
 6  
 7  import adafruit_character_lcd
 8  import adafruit_fancyled.adafruit_fancyled as fancy
 9  import board
10  import digitalio
11  import neopixel
12  from analogio import AnalogIn
13  
14  lcd_rs = digitalio.DigitalInOut(board.D5)
15  lcd_en = digitalio.DigitalInOut(board.D6)
16  lcd_d7 = digitalio.DigitalInOut(board.D12)
17  lcd_d6 = digitalio.DigitalInOut(board.D11)
18  lcd_d5 = digitalio.DigitalInOut(board.D10)
19  lcd_d4 = digitalio.DigitalInOut(board.D9)
20  lcd_columns = 16
21  lcd_rows = 2
22  
23  lcd = adafruit_character_lcd.Character_LCD(
24      lcd_rs, lcd_en, lcd_d4, lcd_d5, lcd_d6, lcd_d7, lcd_columns, lcd_rows
25  )
26  
27  potH = AnalogIn(board.A0)  # pot for hue
28  potS = AnalogIn(board.A1)  # pot for saturation
29  potV = AnalogIn(board.A2)  # pot for value
30  pixpin = board.D13  # NeoPixel pin
31  numpix = 8
32  
33  strip = neopixel.NeoPixel(pixpin, numpix, brightness=1.0)
34  
35  
36  def val(pin):
37      # divides voltage (65535) to get a value between 0 and 1
38      return pin.value / 65535
39  
40  
41  def round_pot_h():
42      # rounds decimal value to 2 decimal places
43      return round(val(potH), 2)
44  
45  
46  def round_pot_s():
47      return round(val(potS), 2)
48  
49  
50  def round_pot_v():
51      return round(val(potV), 2)
52  
53  
54  while True:
55      # calls for HSV values
56      color = fancy.CHSV(val(potH), val(potS), val(potV))
57      # converts float HSV values to integer RGB values
58      packed = color.pack()
59      # writes converted int values to NeoPixels
60      strip.fill(packed)
61  
62      lcd.set_cursor(3, 0)
63      # text at the top of the screen
64      lcd.message('H + S + V =')
65      lcd.set_cursor(1, 1)
66      # sends the rounded value and converts it to a string
67      lcd.message(str(round_pot_h()))
68      lcd.set_cursor(6, 1)
69      lcd.message(str(round_pot_s()))
70      lcd.set_cursor(11, 1)
71      lcd.message(str(round_pot_v()))
72      time.sleep(0.5)
73      # refreshes screen to display most recent pot values
74      lcd.clear()