/ Make_It_Twist_Potentiometer / potentiometer-neopixels / potentiometer-neopixels.ino
potentiometer-neopixels.ino
 1  // SPDX-FileCopyrightText: 2018 Anne Barela for Adafruit Industries
 2  //
 3  // SPDX-License-Identifier: MIT
 4  
 5  // Read analog potentiometer on Circuit Playground Express or other board with changes
 6  // Anne Barela for Adafruit Industries 9/2018 based on
 7  // NeoPixel Ring simple sketch (c) 2013 Shae Erisson
 8  // released under the GPLv3 license to match the rest of the AdaFruit NeoPixel library
 9  
10  #include <Adafruit_NeoPixel.h>
11  #ifdef __AVR__
12    #include <avr/power.h>
13  #endif
14  
15  // Which pin on the microcontroller board is connected to the NeoPixels?
16  #define PIN             8  // For Circuit Playground Express
17  
18  // How many NeoPixels are attached to the board?
19  #define NUMPIXELS      10
20  
21  // When we setup the NeoPixel library, we tell it how many pixels, and which pin to use to send signals.
22  // Note that for older NeoPixel strips you might need to change the third parameter--see the strandtest
23  // example for more information on possible values.
24  Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);
25  
26  int delayval = 500; // delay for half a second
27  
28  void setup() {
29    Serial.begin(9600);
30    pixels.begin(); // This initializes the NeoPixel library.
31  }
32  
33  void loop() {
34     int i;              // loop variable
35     int value;          // analog read of potentiometer
36     int display_value;  // number of NeoPixels to display out of NUMPIXELS
37  
38     // Read PIN value and scale from 0 to NUMPIXELS -1
39     value = analogRead(A1);
40     Serial.print(value);
41     Serial.print(", ");
42     display_value = int(value  * NUMPIXELS / 1023);
43     Serial.println(display_value);
44  
45     // For a set of NeoPixels the first NeoPixel is 0, second is 1, all the way up to the count of pixels minus one
46  
47     for(i=0; i<display_value; i++){
48  
49        // pixels.Color takes RGB values, from 0,0,0 up to 255,255,255
50        pixels.setPixelColor(i, 0, 050, 0); // Moderately bright green color
51     }
52     for(i=display_value; i<NUMPIXELS; i++) {
53        pixels.setPixelColor(i, 0, 0, 0);    // turn off all pixels after value displayed
54     }
55  
56     pixels.show(); // This sends the updated pixel color to the hardware.
57  
58     delay(delayval); // Delay for a period of time (in milliseconds).
59  }