NeoPixel Fun

Now let's take the analog value and do something fun with it. How about turning on the NeoPixels to represent knob position? Like all NeoPixels off is one direction, and all NeoPixels on is the other direction.

We can do this pretty easy using the map function. We know the range of values we expect from the analog input, 0-1023, and we just want to map that to how many NeoPixels to light up, 0-10. So, it's just a matter of coding it up.

And here it is.

Download: file
///////////////////////////////////////////////////////////////////////////////
// Circuit Playground Analog In - NeoPixel Fun
//
// Author: Carter Nelson
// MIT License (https://opensource.org/licenses/MIT)

#include <Adafruit_CircuitPlayground.h>

uint16_t value;
uint8_t pixels;

///////////////////////////////////////////////////////////////////////////////
void setup() {
  Serial.begin(9600);
  CircuitPlayground.begin();
}

///////////////////////////////////////////////////////////////////////////////
void loop() {
  value = analogRead(10);
  pixels = map(value, 0, 1023, 0, 10);

  CircuitPlayground.clearPixels();
  for (int p=0; p<pixels; p++) {
    CircuitPlayground.setPixelColor(p, 0xFF00FF);
  }
  delay(100);
}

With this code loaded and running on the Circuit Playground, try turning the knob. You should see the NeoPixels light up in accordance with the knob position.

This guide was first published on Feb 11, 2017. It was last updated on Feb 11, 2017.
This page (NeoPixel Fun) was last updated on Jul 11, 2020.