Skip to content

Commit 015b1f3

Browse files
authored
Merge pull request #3107 from FoamyGuy/neopot
NeoPot example code
2 parents 3a7683e + 7057bc7 commit 015b1f3

File tree

3 files changed

+110
-0
lines changed

3 files changed

+110
-0
lines changed

Adafruit_NeoPot/Arduino_Rainbow_Pot/.feather_rp2040.test.only

Whitespace-only changes.
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
// SPDX-FileCopyrightText: 2025 Tim Cocks for Adafruit Industries
2+
//
3+
// SPDX-License-Identifier: MIT
4+
5+
#include <Adafruit_NeoPixel.h>
6+
int sensorPin = A0; // select the input pin for the potentiometer
7+
int neoPixelPin = D4; // select the pin for the LED
8+
int sensorValue = 0; // variable to store the value coming from the sensor
9+
10+
int readings[10];
11+
size_t count = 0;
12+
13+
// NEO_GRB Pixels are wired for GRB bitstream (most NeoPixel products)
14+
// NEO_RGB Pixels are wired for RGB bitstream (v1 FLORA pixels, not v2)
15+
// NEO_RGBW Pixels are wired for RGBW bitstream (NeoPixel RGBW products)
16+
Adafruit_NeoPixel strip = Adafruit_NeoPixel(1, neoPixelPin, NEO_GRB + NEO_KHZ800);
17+
18+
// Insert an int value at index 0 of an int array, shifting all other elements up.
19+
// If the array already contains 'maxCount' elements, the last one is dropped.
20+
// 'count' is the number of valid elements currently stored in the array.
21+
void insert(int arr[], size_t maxCount, int value, size_t &count){
22+
// Determine how many elements we need to shift (cannot exceed the array bounds)
23+
size_t shiftCount = (count < maxCount) ? count : maxCount - 1;
24+
25+
// Shift elements up by one position
26+
for (size_t i = shiftCount; i > 0; --i) {
27+
arr[i] = arr[i - 1];
28+
}
29+
30+
// Insert the new value at the beginning
31+
arr[0] = value;
32+
33+
// Update the element count
34+
if (count < maxCount) {
35+
++count; // we added a new element
36+
} // else count stays the same – the last element was overwritten
37+
}
38+
39+
// Input an array of 10 or fewer int's, and a count of how many have values
40+
// Returns average of the values in the array.
41+
int average(int arr[], size_t count){
42+
int sum = 0;
43+
for(int i = 0; i < 10; i++){
44+
sum = sum + arr[i];
45+
}
46+
return sum / count;
47+
}
48+
49+
// Input a value 0 to 255 to get a color value.
50+
// The colours are a transition r - g - b - back to r.
51+
uint32_t Wheel(byte WheelPos) {
52+
WheelPos = 255 - WheelPos;
53+
if(WheelPos < 85) {
54+
return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3);
55+
}
56+
if(WheelPos < 170) {
57+
WheelPos -= 85;
58+
return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3);
59+
}
60+
WheelPos -= 170;
61+
return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
62+
}
63+
64+
void setup() {
65+
Serial.begin(115200);
66+
strip.begin();
67+
}
68+
69+
void loop() {
70+
sensorValue = analogRead(sensorPin);
71+
insert(readings, 10, sensorValue, count);
72+
strip.setPixelColor(0, Wheel(average(readings, count) / 4));
73+
strip.show();
74+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# SPDX-FileCopyrightText: 2025 Tim Cocks for Adafruit Industries
2+
# SPDX-License-Identifier: MIT
3+
"""
4+
NeoPot NeoPixel Rainbow Demo
5+
"""
6+
7+
import board
8+
import analogio
9+
import neopixel
10+
import rainbowio
11+
12+
analog_pin = analogio.AnalogIn(board.A0)
13+
knob_pixel = neopixel.NeoPixel(board.D4, 1, brightness=0.75, auto_write=True)
14+
15+
analog_smoothing_buffer = []
16+
17+
18+
def map_range(x, in_min, in_max, out_min, out_max):
19+
return (x - in_min) / (in_max - in_min) * out_max - out_min
20+
21+
22+
def average(values):
23+
if not values:
24+
return 0
25+
26+
return sum(values) / len(values)
27+
28+
29+
while True:
30+
analog_smoothing_buffer.insert(0, analog_pin.value)
31+
if len(analog_smoothing_buffer) >= 10:
32+
analog_smoothing_buffer.pop(-1)
33+
34+
knob_pixel[0] = rainbowio.colorwheel(
35+
map_range(average(analog_smoothing_buffer), 0, 65525, 0, 255)
36+
)

0 commit comments

Comments
 (0)