|
| 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 | +} |
0 commit comments