/**
* Maps potentiometer input to the onboard NeoPixel's hue.
* Turn the knob to sweep through the full rainbow!
*
* Requires the Adafruit NeoPixel library:
* Sketch -> Include Library -> Manage Libraries -> search "Adafruit NeoPixel"
*
* See: https://makeabilitylab.github.io/physcomp/esp32/pot-fade
*/
#include <Adafruit_NeoPixel.h>
// On the Wokwi generic ESP32-S3 DevKit, the NeoPixel is on GPIO 38
const int WOKWI_NEOPIXEL_PIN = 38;
Adafruit_NeoPixel _pixel(1, WOKWI_NEOPIXEL_PIN, NEO_GRB + NEO_KHZ800);
const int POT_INPUT_PIN = 4;
#if defined(ESP32)
const int MAX_ANALOG_VAL = 4095;
#else
const int MAX_ANALOG_VAL = 1023;
#endif
const uint32_t MAX_HUE = 65536; // Full color wheel in 16-bit hue
void setup() {
#if defined(NEOPIXEL_POWER)
pinMode(NEOPIXEL_POWER, OUTPUT);
digitalWrite(NEOPIXEL_POWER, HIGH);
#endif
_pixel.begin();
_pixel.setBrightness(30); // Keep it gentle on the eyes
_pixel.show();
Serial.begin(115200);
}
void loop() {
// Read the potentiometer (0-4095 on ESP32)
int potVal = analogRead(POT_INPUT_PIN);
// Map to the full hue range (0-65535)
uint32_t hue = map(potVal, 0, MAX_ANALOG_VAL, 0, MAX_HUE - 1);
// Apply the hue with full saturation and brightness, plus gamma correction
uint32_t color = _pixel.gamma32(_pixel.ColorHSV(hue, 255, 255));
_pixel.setPixelColor(0, color);
_pixel.show();
Serial.print("Pot:");
Serial.print(potVal);
Serial.print(", Hue:");
Serial.println(hue);
delay(20);
}