#include <Adafruit_NeoPixel.h>
#define LED_PIN 9
#define NUM_LED 16
int sensor = A1;
Adafruit_NeoPixel ring(NUM_LED, LED_PIN, NEO_GRB + NEO_KHZ800);
int pos = 0;
unsigned long previousPosMillis = 0;
int posTimer;
int trailFade;
bool refresh = false;
int smoothing = 0;
unsigned long previousSmoothMillis = 0;
int smoothTimer = 10;
void setup() {
ring.begin();
ring.setBrightness(255);
Serial.begin(9600);
pinMode(sensor, INPUT);
}
void loop() {
if (millis() - previousSmoothMillis >= smoothTimer) {
previousSmoothMillis = millis();
Serial.println(analogRead(sensor));
if (analogRead(sensor) < 512) {
smoothing++;
posTimer *= 1.1;
} else {
smoothing--;
posTimer *= 0.5;
}
smoothing = constrain(smoothing, 3, 60);
posTimer = constrain(posTimer, 40, 100);
}
trailFade = smoothing;
if (pos >= NUM_LED) {
pos = 0;
}
if (refresh) {
refresh = false;
for (int i = 0; i < NUM_LED; i++) {
if (i != pos) {
uint32_t Color = ring.getPixelColor(i);
ring.setPixelColor(i, max(Red(Color) - (trailFade * 0.66), 0), max(Green(Color) - trailFade, 0), max(Blue(Color) - trailFade, 0));
} else {
ring.setPixelColor(i, 255, 200, 0);
}
}
ring.show();
}
if (millis() - previousPosMillis >= posTimer) {
previousPosMillis = millis();
refresh = true;
pos++;
}
}
uint8_t Red(uint32_t color) {
return (color >> 16) & 0xFF;
}
// Returns the Green component of a 32-bit color
uint8_t Green(uint32_t color) {
return (color >> 8) & 0xFF;
}
// Returns the Blue component of a 32-bit color
uint8_t Blue(uint32_t color) {
return color & 0xFF;
}
/*
{
"version": 1,
"author": "Anonymous maker",
"editor": "wokwi",
"parts": [
{ "type": "wokwi-arduino-uno", "id": "uno", "top": 0, "left": 0, "attrs": {} },
{
"type": "wokwi-led-ring",
"id": "ring1",
"top": -191.36,
"left": 87.59,
"attrs": { "pixels": "16" }
},
{
"type": "wokwi-pushbutton",
"id": "btn1",
"top": 275,
"left": 249.6,
"attrs": { "color": "green" }
},
{
"type": "wokwi-resistor",
"id": "r1",
"top": 320.75,
"left": 38.4,
"attrs": { "value": "1000" }
}
],
"connections": [
[ "ring1:GND", "uno:GND.1", "black", [ "v0" ] ],
[ "ring1:VCC", "uno:5V", "red", [ "v0" ] ],
[ "uno:9", "ring1:DIN", "green", [ "v0" ] ],
[ "btn1:1.l", "uno:A1", "green", [ "h0" ] ],
[ "uno:5V", "btn1:2.l", "red", [ "v0" ] ],
[ "btn1:1.l", "r1:2", "green", [ "h0" ] ],
[ "r1:1", "uno:GND.2", "green", [ "v0" ] ]
],
"dependencies": {}
}
*/