/ Neopixel_Aquarium / Neopixel_Aquarium.ino
Neopixel_Aquarium.ino
1 // SPDX-FileCopyrightText: 2018 Erin St Blaine for Adafruit Industries 2 // 3 // SPDX-License-Identifier: MIT 4 5 // Code by Erin St. Blaine for Adafruit Industries 6 // NeoPixel Aquarium Tutorial: https://learn.adafruit.com/neopixel-aquarium-with-submersible-lights/ 7 // For QT Py board 8 9 #include "Adafruit_FreeTouch.h" 10 #include "FastLED.h" 11 12 #define CAPTOUCH_PIN A2 //A2 capacitive touch pin 13 #define CAPTOUCH_PIN2 A3 //A3 capacitive touch pin 14 #define NEOPIXEL_PIN A1 //A1 neopixel pin 15 #define NUM_LEDS 164 //how many pixels total 16 17 #define LED_TYPE WS2812 18 #define COLOR_ORDER GRB 19 CRGB leds[NUM_LEDS]; //LED array 20 21 // These variables will affect the way the gradient animation looks. Feel free to mess with them. 22 int SPEEDO = 10; 23 int STEPS = 50; 24 int HUE = 0; 25 int SATURATION = 255; 26 int COLORCHANGE = 50; 27 int BRIGHTNESS = 200; 28 int onBright = 200; 29 30 // Calibrating your capacitive touch sensitivity: Change this variable to something between your capacitive touch serial readouts for on and off 31 int touch = 500; 32 33 long oldState = 0; 34 long offState = 0; 35 36 // set up capacitive touch button using the FreeTouch library 37 Adafruit_FreeTouch qt_1 = Adafruit_FreeTouch(CAPTOUCH_PIN, OVERSAMPLE_4, RESISTOR_50K, FREQ_MODE_NONE); 38 Adafruit_FreeTouch qt_2 = Adafruit_FreeTouch(CAPTOUCH_PIN2, OVERSAMPLE_4, RESISTOR_50K, FREQ_MODE_NONE); 39 40 TBlendType currentBlending; 41 CRGBPalette16 currentPalette; 42 43 44 void setup() { 45 Serial.begin(115200); 46 47 if (! qt_1.begin()) 48 Serial.println("Failed to begin qt on pin A2"); 49 if (! qt_2.begin()) 50 Serial.println("Failed to begin qt on pin A3"); 51 FastLED.addLeds<WS2812, NEOPIXEL_PIN, COLOR_ORDER>(leds, NUM_LEDS); // Set up neopixels with FastLED 52 FastLED.setBrightness(BRIGHTNESS); // set global brightness 53 FastLED.setMaxPowerInVoltsAndMilliamps(3,350); //Constrain FastLED's power usage 54 } 55 56 void loop() { 57 58 Serial.print(qt_1.measure()); 59 Serial.write(' '); 60 checkpress(); //check to see if the button's been pressed 61 delay(20); 62 } 63 64 void checkpress() { 65 66 // Get current button state. 67 68 long newState = qt_1.measure(); 69 long switchState = qt_2.measure(); 70 Serial.println(qt_1.measure()); 71 Serial.println(qt_2.measure()); 72 if (newState > touch && oldState < touch) { 73 // Short delay to debounce button. 74 delay(500); 75 // Check if button is still low after debounce. 76 long newState = qt_1.measure(); 77 long switchState = qt_2.measure(); 78 } 79 if (switchState > touch && offState < touch) { 80 // Short delay to debounce button. 81 delay(500); 82 // Check if button is still low after debounce. 83 long newState = qt_1.measure(); 84 } 85 86 87 if (newState > touch ) { 88 BRIGHTNESS = onBright; 89 HUE=HUE+COLORCHANGE; // change the hue by a specified amount each time the cap touch pad is activated 90 if (HUE > 255){ 91 HUE=0;} 92 Gradient(); 93 } 94 // if (HUE==250) { 95 // dark(); 96 // } 97 else { 98 Gradient(); 99 100 } 101 if (switchState > touch) { 102 if (BRIGHTNESS == onBright){ 103 dark(); 104 switchState = 0; 105 BRIGHTNESS = 0; 106 } 107 else { 108 BRIGHTNESS = onBright; 109 } 110 } 111 112 113 114 // Set the last button state to the old state. 115 oldState = newState; 116 offState = switchState; 117 118 } 119 120 121 122 // GRADIENT -------------------------------------------------------------- 123 void Gradient() 124 { 125 SetupGradientPalette(); 126 127 static uint8_t startIndex = 0; 128 startIndex = startIndex - 1; // motion speed 129 130 FillLEDsFromPaletteColors( startIndex); 131 FastLED.show(); 132 FastLED.delay(SPEEDO); 133 } 134 135 // adjust hue, saturation and brightness values here to make a pleasing gradient 136 137 void SetupGradientPalette() 138 { 139 CRGB light = CHSV( HUE + 25, SATURATION - 20, BRIGHTNESS); 140 CRGB lightmed = CHSV (HUE + 15, SATURATION - 10, BRIGHTNESS-50); 141 CRGB medium = CHSV ( HUE + 10, SATURATION - 15, BRIGHTNESS); 142 CRGB dark = CHSV( HUE, SATURATION, BRIGHTNESS); 143 CRGB black = CHSV (HUE, SATURATION, 0); 144 145 currentPalette = CRGBPalette16( 146 black, light, light, light, 147 lightmed, lightmed, lightmed, medium, 148 medium, medium, medium, dark, 149 dark, dark, dark, black ); 150 } 151 152 void FillLEDsFromPaletteColors( uint8_t colorIndex) 153 { 154 uint8_t brightness = BRIGHTNESS; 155 156 for( int i = 0; i < NUM_LEDS; i++) { 157 leds[i] = ColorFromPalette( currentPalette, colorIndex, brightness, currentBlending); 158 colorIndex += STEPS; 159 } 160 } 161 162 void dark() 163 { 164 for(int i = 0; i < NUM_LEDS; i++) { 165 leds[i] = CRGB::Black; 166 FastLED.show(); 167 delay(20); 168 } 169 }