/ NeoPixie_Dust_Bag / NeoPixie_Dust_Bag.ino
NeoPixie_Dust_Bag.ino
1 // SPDX-FileCopyrightText: 2017 John Edgar Park for Adafruit Industries 2 // 3 // SPDX-License-Identifier: MIT 4 5 // NeoPixie Dust Bag by John Edgar Park jpixl.net 6 // 7 // No fairy costume is complete without a glowing pixie dust bag. 8 // This one uses a touch sensor to cycle through colors on the beautifully twinkling NeoPixel rings, 9 // controlled by the tiny Adafruit GEMMA microcontroller. 10 // 11 // Build instructions: learn.adafruit.com/neopixel-pixie-dust-bag/overview 12 // 13 // Some code based upon Adafruit GEMMA earring code and Adafruit NeoPixel buttoncycler code 14 15 #include <Adafruit_NeoPixel.h> //Include the NeoPixel library 16 17 #define NEO_PIN 0 // DIGITAL IO pin for NeoPixel OUTPUT from GEMMA 18 #define TOUCH_PIN 2 // DIGITAL IO pin for momentary touch sensor INPUT to GEMMA 19 #define PIXEL_COUNT 30 // Number of NeoPixels connected to GEMMA 20 #define DELAY_MILLIS 10 // delay between blinks, smaller numbers are faster 21 #define DELAY_MULT 8 // Randomization multiplier on the delay speed of the effect 22 #define BRIGHT 100 // Brightness of the pixels, max is 255 23 24 // Parameter 1 = number of pixels in strip 25 // Parameter 2 = pin number on Arduino (most are valid) 26 // Parameter 3 = pixel type flags, add together as needed: 27 // NEO_RGB Pixels are wired for RGB bitstream (v1 FLORA pixels, not v2) 28 // NEO_GRB Pixels are wired for GRB bitstream, correct for neopixel stick (most NeoPixel products) 29 // NEO_KHZ400 400 KHz bitstream (e.g. FLORA pixels) 30 // NEO_KHZ800 800 KHz bitstream (e.g. High Density LED strip), correct for neopixel stick 31 Adafruit_NeoPixel pixels = Adafruit_NeoPixel(PIXEL_COUNT, NEO_PIN, NEO_GRB + NEO_KHZ800); 32 33 bool oldState = HIGH; //sets the initial variable for counting touch sensor button pushes 34 int showColor = 0; //color mode for cycling 35 36 void setup() { 37 pinMode(TOUCH_PIN, INPUT); //Initialize touch sensor pin as input using external pull-up resistor 38 pixels.begin(); 39 pixels.setBrightness(BRIGHT); 40 pixels.show(); //Set all pixels to "off" 41 } 42 43 44 void loop() { 45 int RColor = 100; //color (0-255) values to be set by cylcing touch switch, initially GOLD 46 int GColor = 0 ; 47 int BColor = 0 ; 48 49 if (showColor==0) {//Garden PINK 50 RColor = 242; 51 GColor = 90; 52 BColor = 255; 53 } 54 if (showColor==1) {//Pixie GOLD 55 RColor = 255; 56 GColor = 222; 57 BColor = 30; 58 } 59 if (showColor==2) {//Alchemy BLUE 60 RColor = 50; 61 GColor = 255; 62 BColor = 255; 63 } 64 if (showColor==3) {//Animal ORANGE 65 RColor = 255; 66 GColor = 100; 67 BColor = 0; 68 } 69 if (showColor==4) {//Tinker GREEN 70 RColor = 0; 71 GColor = 255; 72 BColor = 40; 73 } 74 75 //sparkling 76 int p = random(PIXEL_COUNT); //select a random pixel 77 pixels.setPixelColor(p,RColor,GColor,BColor); //color value comes from cycling state of momentary switch 78 pixels.show(); 79 delay(DELAY_MILLIS * random(DELAY_MULT) ); //delay value randomized to up to DELAY_MULT times longer 80 pixels.setPixelColor(p, RColor/10, GColor/10, BColor/10); //set to a dimmed version of the state color 81 pixels.show(); 82 pixels.setPixelColor(p+1, RColor/15, GColor/15, BColor/15); //set a neighbor pixel to an even dimmer value 83 pixels.show(); 84 85 //button check to cycle through color value sets 86 bool newState = digitalRead(TOUCH_PIN); //Get the current button state 87 // Check if state changed from high to low (button press). 88 if (newState == LOW && oldState == HIGH) { 89 // Short delay to debounce button. 90 delay(20); 91 // Check if button is still low after debounce. 92 newState = digitalRead(TOUCH_PIN); 93 if (newState == LOW) { 94 showColor++; 95 if (showColor > 4) 96 showColor=0; 97 } 98 } 99 // Set the last button state to the old state. 100 oldState = newState; 101 102 }