ported to CircuitPython

This commit is contained in:
Mikey Sklar 2018-02-26 05:57:47 -07:00
parent af7e60e621
commit e43649c265
4 changed files with 102 additions and 0 deletions

View file

@ -0,0 +1,4 @@
# Textile_Potentiometer_Hoodie
Code to accompany this tutorial:
https://learn.adafruit.com/textile-potentiometer-hoodie

View file

@ -0,0 +1,47 @@
#include <Adafruit_NeoPixel.h>
#define PIN 1
// Parameter 1 = number of pixels in strip
// Parameter 2 = Arduino pin number (most are valid)
// Parameter 3 = pixel type flags, add together as needed:
// NEO_KHZ800 800 KHz bitstream (most NeoPixel products w/WS2812 LEDs)
// NEO_KHZ400 400 KHz (classic 'v1' (not v2) FLORA pixels, WS2811 drivers)
// NEO_GRB Pixels are wired for GRB bitstream (most NeoPixel products)
// NEO_RGB Pixels are wired for RGB bitstream (v1 FLORA pixels, not v2)
Adafruit_NeoPixel strip = Adafruit_NeoPixel(8, PIN, NEO_GRB + NEO_KHZ800);
int sensorPin = 1; // select the input pin for the potentiometer (analog 1 is digital 2)
int sensorValue = 0; // variable to store the value coming from the sensor
int colorValue = 0;
void setup() {
// Set internal pullup resistor for sensor pin (analog 1 is digital 2)
pinMode(2, INPUT_PULLUP);
strip.begin();
strip.setBrightness(40); //adjust brightness here
strip.show(); // Initialize all pixels to 'off'
}
void loop() {
// read the value from the sensor:
sensorValue = analogRead(sensorPin);
colorValue = map(sensorValue, 0, 1024, 0, 255); //map sensor values from 0-124 to 0-255
for (int i = 0; i<strip.numPixels(); i++){
strip.setPixelColor(i, Wheel(colorValue)); //use Wheel function to set color
}
strip.show();
}
// Input a value 0 to 255 to get a color value.
// The colours are a transition r - g - b - back to r.
uint32_t Wheel(byte WheelPos) {
if(WheelPos < 85) {
return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
} else if(WheelPos < 170) {
WheelPos -= 85;
return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3);
} else {
WheelPos -= 170;
return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3);
}
}

View file

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