/ UnicornHorn_CapacitiveTouch / UnicornHorn_CapacitiveTouch.ino
UnicornHorn_CapacitiveTouch.ino
1 // SPDX-FileCopyrightText: 2018 Erin St. Blaine for Adafruit Industries 2 // 3 // SPDX-License-Identifier: MIT 4 5 #include "Adafruit_FreeTouch.h" 6 #include "FastLED.h" 7 8 #define CAPTOUCH_PIN A1 9 #define NEOPIXEL_PIN 1 10 #define LED_PIN 0 11 #define NUM_LEDS 12 12 13 #define LED_TYPE WS2812 14 #define COLOR_ORDER GRB 15 CRGB leds[NUM_LEDS]; 16 17 int BRIGHTNESS=150; 18 int touch = 500; // Change this variable to something between your capacitive touch serial readouts for on and off 19 20 long oldState = 0; 21 int gHue=0; 22 23 Adafruit_FreeTouch qt_1 = Adafruit_FreeTouch(CAPTOUCH_PIN, OVERSAMPLE_4, RESISTOR_50K, FREQ_MODE_NONE); 24 //Adafruit_FreeTouch qt_2 = Adafruit_FreeTouch(A2, OVERSAMPLE_4, RESISTOR_50K, FREQ_MODE_NONE); 25 26 27 28 void setup() { 29 Serial.begin(115200); 30 31 if (! qt_1.begin()) 32 Serial.println("Failed to begin qt on pin A1"); 33 34 pinMode(LED_PIN, OUTPUT); //initialize the LED pin 35 36 FastLED.addLeds<WS2812, NEOPIXEL_PIN, COLOR_ORDER>(leds, NUM_LEDS); // Set up neopixels with FastLED 37 FastLED.setBrightness(BRIGHTNESS); 38 FastLED.setMaxPowerInVoltsAndMilliamps(3,350); //Constrain FastLED's power usage 39 } 40 41 void loop() { 42 43 Serial.print(qt_1.measure()); 44 Serial.write(' '); 45 checkpress(); 46 delay(20); 47 } 48 49 void checkpress() { 50 51 // Get current button state. 52 53 long newState = qt_1.measure(); 54 Serial.println(qt_1.measure()); 55 if (newState > touch && oldState < touch) { 56 // Short delay to debounce button. 57 delay(20); 58 // Check if button is still low after debounce. 59 long newState = qt_1.measure(); } 60 61 62 if (newState > touch ) { 63 dark(); 64 digitalWrite(LED_PIN, HIGH); 65 delay(20); 66 } 67 68 else { 69 rainbow(); 70 digitalWrite(LED_PIN, LOW); 71 delay(20); 72 } 73 74 75 76 // Set the last button state to the old state. 77 oldState = newState; 78 79 // do some periodic updates 80 EVERY_N_MILLISECONDS( 20 ) { gHue++; } // slowly cycle the "base color" through the rainbow 81 } 82 83 void rainbow() 84 { 85 // FastLED's built-in rainbow generator 86 fill_rainbow( leds, NUM_LEDS, gHue, 7); 87 FastLED.show(); 88 delay(20); 89 90 } 91 92 void dark() 93 { 94 for(int i = 0; i < NUM_LEDS; i++) { 95 leds[i] = CRGB::Black; 96 FastLED.show(); 97 delay(20); 98 } 99 } 100