NeoPot example code

This commit is contained in:
foamyguy 2025-08-18 11:41:07 -05:00
parent 3a7683e728
commit 7057bc7a1f
3 changed files with 110 additions and 0 deletions

View file

@ -0,0 +1,74 @@
// SPDX-FileCopyrightText: 2025 Tim Cocks for Adafruit Industries
//
// SPDX-License-Identifier: MIT
#include <Adafruit_NeoPixel.h>
int sensorPin = A0; // select the input pin for the potentiometer
int neoPixelPin = D4; // select the pin for the LED
int sensorValue = 0; // variable to store the value coming from the sensor
int readings[10];
size_t count = 0;
// NEO_GRB Pixels are wired for GRB bitstream (most NeoPixel products)
// NEO_RGB Pixels are wired for RGB bitstream (v1 FLORA pixels, not v2)
// NEO_RGBW Pixels are wired for RGBW bitstream (NeoPixel RGBW products)
Adafruit_NeoPixel strip = Adafruit_NeoPixel(1, neoPixelPin, NEO_GRB + NEO_KHZ800);
// Insert an int value at index 0 of an int array, shifting all other elements up.
// If the array already contains 'maxCount' elements, the last one is dropped.
// 'count' is the number of valid elements currently stored in the array.
void insert(int arr[], size_t maxCount, int value, size_t &count){
// Determine how many elements we need to shift (cannot exceed the array bounds)
size_t shiftCount = (count < maxCount) ? count : maxCount - 1;
// Shift elements up by one position
for (size_t i = shiftCount; i > 0; --i) {
arr[i] = arr[i - 1];
}
// Insert the new value at the beginning
arr[0] = value;
// Update the element count
if (count < maxCount) {
++count; // we added a new element
} // else count stays the same the last element was overwritten
}
// Input an array of 10 or fewer int's, and a count of how many have values
// Returns average of the values in the array.
int average(int arr[], size_t count){
int sum = 0;
for(int i = 0; i < 10; i++){
sum = sum + arr[i];
}
return sum / count;
}
// 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) {
WheelPos = 255 - WheelPos;
if(WheelPos < 85) {
return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3);
}
if(WheelPos < 170) {
WheelPos -= 85;
return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3);
}
WheelPos -= 170;
return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
}
void setup() {
Serial.begin(115200);
strip.begin();
}
void loop() {
sensorValue = analogRead(sensorPin);
insert(readings, 10, sensorValue, count);
strip.setPixelColor(0, Wheel(average(readings, count) / 4));
strip.show();
}

View file

@ -0,0 +1,36 @@
# SPDX-FileCopyrightText: 2025 Tim Cocks for Adafruit Industries
# SPDX-License-Identifier: MIT
"""
NeoPot NeoPixel Rainbow Demo
"""
import board
import analogio
import neopixel
import rainbowio
analog_pin = analogio.AnalogIn(board.A0)
knob_pixel = neopixel.NeoPixel(board.D4, 1, brightness=0.75, auto_write=True)
analog_smoothing_buffer = []
def map_range(x, in_min, in_max, out_min, out_max):
return (x - in_min) / (in_max - in_min) * out_max - out_min
def average(values):
if not values:
return 0
return sum(values) / len(values)
while True:
analog_smoothing_buffer.insert(0, analog_pin.value)
if len(analog_smoothing_buffer) >= 10:
analog_smoothing_buffer.pop(-1)
knob_pixel[0] = rainbowio.colorwheel(
map_range(average(analog_smoothing_buffer), 0, 65525, 0, 255)
)