Format Textile Potentiometer

This commit is contained in:
Craig Richardson 2018-05-14 18:04:33 +01:00
parent 42fc4881f3
commit 6558031f90

View file

@ -1,51 +1,53 @@
import board
import analogio import analogio
import time import board
import neopixel import neopixel
# Initialize input/output pins # Initialize input/output pins
sensorpin = board.A1 # input pin for the potentiometer sensor_pin = board.A1 # input pin for the potentiometer
sensor = analogio.AnalogIn(sensorpin) sensor = analogio.AnalogIn(sensor_pin)
pixpin = board.D1 # pin where NeoPixels are connected pix_pin = board.D1 # pin where NeoPixels are connected
numpix = 8 # number of neopixels num_pix = 8 # number of neopixels
strip = neopixel.NeoPixel(pixpin, numpix, brightness=.15, auto_write=False) strip = neopixel.NeoPixel(pix_pin, num_pix, brightness=.15, auto_write=False)
color_value = 0
sensor_value = 0
colorvalue = 0
sensorvalue = 0
def wheel(pos): def wheel(pos):
# Input a value 0 to 255 to get a color value. # Input a value 0 to 255 to get a color value.
# The colours are a transition r - g - b - back to r. # The colours are a transition r - g - b - back to r.
if (pos < 0) or (pos > 255): if (pos < 0) or (pos > 255):
return (0, 0, 0) return 0, 0, 0
if (pos < 85): if pos < 85:
return (int(pos * 3), int(255 - (pos*3)), 0) return int(pos * 3), int(255 - (pos * 3)), 0
elif (pos < 170): elif pos < 170:
pos -= 85 pos -= 85
return (int(255 - pos*3), 0, int(pos*3)) return int(255 - pos * 3), 0, int(pos * 3)
else:
pos -= 170
return (0, int(pos*3), int(255 - pos*3))
def remapRange(value, leftMin, leftMax, rightMin, rightMax): pos -= 170
return 0, int(pos * 3), int(255 - pos * 3)
def remap_range(value, left_min, left_max, right_min, right_max):
# this remaps a value from original (left) range to new (right) range # this remaps a value from original (left) range to new (right) range
# Figure out how 'wide' each range is # Figure out how 'wide' each range is
leftSpan = leftMax - leftMin left_span = left_max - left_min
rightSpan = rightMax - rightMin right_span = right_max - right_min
# Convert the left range into a 0-1 range (int) # Convert the left range into a 0-1 range (int)
valueScaled = int(value - leftMin) / int(leftSpan) valueScaled = int(value - left_min) / int(left_span)
# Convert the 0-1 range into a value in the right range. # Convert the 0-1 range into a value in the right range.
return int(rightMin + (valueScaled * rightSpan)) return int(right_min + (valueScaled * right_span))
# Loop forever... # Loop forever...
while True: while True:
# remap the potentiometer analog sensor values from 0-65535 to RGB 0-255 # remap the potentiometer analog sensor values from 0-65535 to RGB 0-255
colorvalue = remapRange(sensor.value, 0, 65535, 0, 255) color_value = remap_range(sensor.value, 0, 65535, 0, 255)
for i in range( 0, len(strip) ): for i in range(len(strip)):
strip[i] = wheel(colorvalue) strip[i] = wheel(color_value)
strip.write() strip.write()