From 0880512e572e3d409a87c65b4f64d09c887445f8 Mon Sep 17 00:00:00 2001 From: firepixie Date: Tue, 16 Jan 2018 13:48:06 -0800 Subject: [PATCH] Unicorn Horn with Capacitive Touch Code upload --- .../UnicornHorn_CapacitiveTouch.ino | 96 +++++++++++++++++++ 1 file changed, 96 insertions(+) create mode 100644 UnicornHorn_CapacitiveTouch/UnicornHorn_CapacitiveTouch.ino diff --git a/UnicornHorn_CapacitiveTouch/UnicornHorn_CapacitiveTouch.ino b/UnicornHorn_CapacitiveTouch/UnicornHorn_CapacitiveTouch.ino new file mode 100644 index 00000000..e6befb15 --- /dev/null +++ b/UnicornHorn_CapacitiveTouch/UnicornHorn_CapacitiveTouch.ino @@ -0,0 +1,96 @@ +#include "Adafruit_FreeTouch.h" +#include "FastLED.h" + +#define CAPTOUCH_PIN A1 +#define NEOPIXEL_PIN 1 +#define LED_PIN 0 +#define NUM_LEDS 12 + +#define LED_TYPE WS2812 +#define COLOR_ORDER GRB +CRGB leds[NUM_LEDS]; + +int BRIGHTNESS=150; +int touch = 500; // Change this variable to something between your capacitive touch serial readouts for on and off + +long oldState = 0; +int gHue=0; + +Adafruit_FreeTouch qt_1 = Adafruit_FreeTouch(CAPTOUCH_PIN, OVERSAMPLE_4, RESISTOR_50K, FREQ_MODE_NONE); +//Adafruit_FreeTouch qt_2 = Adafruit_FreeTouch(A2, OVERSAMPLE_4, RESISTOR_50K, FREQ_MODE_NONE); + + + +void setup() { + Serial.begin(115200); + + if (! qt_1.begin()) + Serial.println("Failed to begin qt on pin A1"); + + pinMode(LED_PIN, OUTPUT); //initialize the LED pin + + FastLED.addLeds(leds, NUM_LEDS); // Set up neopixels with FastLED + FastLED.setBrightness(BRIGHTNESS); + FastLED.setMaxPowerInVoltsAndMilliamps(3,350); //Constrain FastLED's power usage +} + +void loop() { + + Serial.print(qt_1.measure()); + Serial.write(' '); + checkpress(); + delay(20); +} + +void checkpress() { + +// Get current button state. + + long newState = qt_1.measure(); + Serial.println(qt_1.measure()); + if (newState > touch && oldState < touch) { + // Short delay to debounce button. + delay(20); + // Check if button is still low after debounce. + long newState = qt_1.measure(); } + + + if (newState > touch ) { + dark(); + digitalWrite(LED_PIN, HIGH); + delay(20); + } + + else { + rainbow(); + digitalWrite(LED_PIN, LOW); + delay(20); + } + + + + // Set the last button state to the old state. + oldState = newState; + + // do some periodic updates + EVERY_N_MILLISECONDS( 20 ) { gHue++; } // slowly cycle the "base color" through the rainbow +} + +void rainbow() +{ + // FastLED's built-in rainbow generator + fill_rainbow( leds, NUM_LEDS, gHue, 7); + FastLED.show(); + delay(20); + +} + +void dark() +{ + for(int i = 0; i < NUM_LEDS; i++) { + leds[i] = CRGB::Black; + FastLED.show(); + delay(20); +} +} +