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.
/////////////////////////////////////////////////////////////////////////////// // 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.
Page last edited February 08, 2017
Text editor powered by tinymce.