code.py
 1  # SPDX-FileCopyrightText: 2018 Mikey Sklar for Adafruit Industries
 2  #
 3  # SPDX-License-Identifier: MIT
 4  
 5  import analogio
 6  import board
 7  from rainbowio import colorwheel
 8  import neopixel
 9  
10  # Initialize input/output pins
11  sensor_pin = board.A1  # input pin for the potentiometer
12  sensor = analogio.AnalogIn(sensor_pin)
13  
14  pix_pin = board.D1  # pin where NeoPixels are connected
15  num_pix = 8  # number of neopixels
16  strip = neopixel.NeoPixel(pix_pin, num_pix, brightness=.15, auto_write=False)
17  
18  color_value = 0
19  sensor_value = 0
20  
21  
22  def remap_range(value, left_min, left_max, right_min, right_max):
23      # this remaps a value from original (left) range to new (right) range
24      # Figure out how 'wide' each range is
25      left_span = left_max - left_min
26      right_span = right_max - right_min
27  
28      # Convert the left range into a 0-1 range (int)
29      valueScaled = int(value - left_min) / int(left_span)
30  
31      # Convert the 0-1 range into a value in the right range.
32      return int(right_min + (valueScaled * right_span))
33  
34  
35  # Loop forever...
36  while True:
37  
38      # remap the potentiometer analog sensor values from 0-65535 to RGB 0-255
39      color_value = remap_range(sensor.value, 0, 65535, 0, 255)
40  
41      for i in range(len(strip)):
42          strip[i] = colorwheel(color_value)
43      strip.write()