/ color_touch_pendant / color_touch_pendant.ino
color_touch_pendant.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  // Color Touch Pendant Tutorial: https://learn.adafruit.com/color-touch-pendant-necklace/introduction
  7  // Two neopixel rings connected on pin 1 will cycle through gradient colors when the pendant is touched.  For Gemma M0.
  8  
  9  #include "Adafruit_FreeTouch.h"
 10  #include "FastLED.h"
 11  
 12  #define CAPTOUCH_PIN 0  //capacitive touch pin
 13  #define NEOPIXEL_PIN 1  //neopixel ring pin
 14  #define NUM_LEDS    28  //how many pixels total
 15  
 16  #define LED_TYPE    WS2812
 17  #define COLOR_ORDER GRB
 18  CRGB leds[NUM_LEDS];   //LED array 
 19  
 20  // These variables will affect the way the gradient animation looks.  Feel free to mess with them.
 21  int SPEEDO = 10;          
 22  int STEPS = 50;         
 23  int HUE = 0;            
 24  int SATURATION = 255;  
 25  int COLORCHANGE = 50;        
 26  int BRIGHTNESS = 110;     
 27  
 28  // Calibrating your capacitive touch sensitivity: Change this variable to something between your capacitive touch serial readouts for on and off
 29  int touch = 650;    
 30  
 31  long oldState = 0;
 32  
 33  // set up capacitive touch button using the FreeTouch library
 34  Adafruit_FreeTouch qt_1 = Adafruit_FreeTouch(CAPTOUCH_PIN, OVERSAMPLE_4, RESISTOR_50K, FREQ_MODE_NONE);
 35  
 36  
 37  TBlendType    currentBlending;
 38  CRGBPalette16 currentPalette;
 39  
 40  
 41  void setup() {
 42    Serial.begin(115200);
 43  
 44    if (! qt_1.begin())  
 45     Serial.println("Failed to begin qt on pin A1");
 46     FastLED.addLeds<WS2812, NEOPIXEL_PIN, COLOR_ORDER>(leds, NUM_LEDS);  // Set up neopixels with FastLED
 47     FastLED.setBrightness(BRIGHTNESS); // set global brightness
 48     FastLED.setMaxPowerInVoltsAndMilliamps(3,350);  //Constrain FastLED's power usage
 49  }
 50  
 51  void loop() {
 52    
 53    Serial.print(qt_1.measure());
 54    Serial.write(' ');
 55    checkpress();   //check to see if the button's been pressed
 56    //delay(20);
 57  }
 58  
 59  void checkpress() {
 60  
 61  // Get current button state.
 62   
 63      long newState =  qt_1.measure();  
 64      Serial.println(qt_1.measure());
 65     if (newState > touch && oldState < touch) {
 66      // Short delay to debounce button.
 67      delay(500);
 68      // Check if button is still low after debounce.
 69      long newState =  qt_1.measure(); }
 70  
 71   
 72    if (newState > touch ) {  
 73       HUE=HUE+COLORCHANGE;  // change the hue by a specified amount each time the cap touch pad is activated
 74    if (HUE > 255){
 75      HUE=0;}
 76     Gradient();
 77      }
 78  //  if (HUE==250) {
 79  //    dark();
 80  //  }
 81      else {
 82        Gradient();
 83        
 84      }
 85  
 86     
 87      
 88    // Set the last button state to the old state.
 89    oldState = newState;
 90  
 91  } 
 92  
 93  
 94  
 95  // GRADIENT --------------------------------------------------------------
 96  void Gradient()
 97  {
 98    SetupGradientPalette();
 99  
100    static uint8_t startIndex = 0;
101    startIndex = startIndex - 1;  // motion speed
102  
103    FillLEDsFromPaletteColors( startIndex);
104    FastLED.show();
105    FastLED.delay(SPEEDO);
106  }
107  
108  // adjust hue, saturation and brightness values here to make a pleasing gradient
109  
110  void SetupGradientPalette()
111  {
112    CRGB light = CHSV( HUE + 25, SATURATION - 20, BRIGHTNESS);
113    CRGB lightmed = CHSV (HUE + 15, SATURATION - 10, BRIGHTNESS-50);
114    CRGB medium = CHSV ( HUE + 10, SATURATION - 15, BRIGHTNESS);
115    CRGB dark  = CHSV( HUE, SATURATION, BRIGHTNESS);
116    CRGB black = CHSV (HUE, SATURATION, 0);
117    
118    currentPalette = CRGBPalette16( 
119      black,  light,  light,  light,
120      lightmed, lightmed, lightmed,  medium,
121      medium,  medium,  medium,  dark,
122      dark, dark, dark,  black );
123  }
124  
125  void FillLEDsFromPaletteColors( uint8_t colorIndex)
126  {
127    uint8_t brightness = BRIGHTNESS;
128    
129    for( int i = 0; i < NUM_LEDS; i++) {
130      leds[i] = ColorFromPalette( currentPalette, colorIndex, brightness, currentBlending);
131      colorIndex += STEPS;
132    }
133  }
134  
135  void dark()
136  { 
137    for(int i = 0; i < NUM_LEDS; i++) { 
138    leds[i] = CRGB::Black; 
139    FastLED.show();
140    delay(20);
141  }
142  }
143