From 7057bc7a1fa4f04b3373a12c5ff9350b0cc8546f Mon Sep 17 00:00:00 2001 From: foamyguy Date: Mon, 18 Aug 2025 11:41:07 -0500 Subject: [PATCH] NeoPot example code --- .../.feather_rp2040.test.only | 0 .../Arduino_Rainbow_Pot.ino | 74 +++++++++++++++++++ .../CircuitPython_Rainbow_Pot/code.py | 36 +++++++++ 3 files changed, 110 insertions(+) create mode 100644 Adafruit_NeoPot/Arduino_Rainbow_Pot/.feather_rp2040.test.only create mode 100644 Adafruit_NeoPot/Arduino_Rainbow_Pot/Arduino_Rainbow_Pot.ino create mode 100644 Adafruit_NeoPot/CircuitPython_Rainbow_Pot/code.py diff --git a/Adafruit_NeoPot/Arduino_Rainbow_Pot/.feather_rp2040.test.only b/Adafruit_NeoPot/Arduino_Rainbow_Pot/.feather_rp2040.test.only new file mode 100644 index 000000000..e69de29bb diff --git a/Adafruit_NeoPot/Arduino_Rainbow_Pot/Arduino_Rainbow_Pot.ino b/Adafruit_NeoPot/Arduino_Rainbow_Pot/Arduino_Rainbow_Pot.ino new file mode 100644 index 000000000..509e6eb6c --- /dev/null +++ b/Adafruit_NeoPot/Arduino_Rainbow_Pot/Arduino_Rainbow_Pot.ino @@ -0,0 +1,74 @@ +// SPDX-FileCopyrightText: 2025 Tim Cocks for Adafruit Industries +// +// SPDX-License-Identifier: MIT + +#include +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(); +} diff --git a/Adafruit_NeoPot/CircuitPython_Rainbow_Pot/code.py b/Adafruit_NeoPot/CircuitPython_Rainbow_Pot/code.py new file mode 100644 index 000000000..cee3c0d18 --- /dev/null +++ b/Adafruit_NeoPot/CircuitPython_Rainbow_Pot/code.py @@ -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) + )