It's a great idea to get your software all set up and loaded onto your board right away, to make testing your connections easier later on.

To get the code running you'll need:

  1. Arduino IDE (1.8 or newer)
  2. Adafruit Board support for QT Py
  3. Arduino libraries: FastLED, Adafruit_FreeTouch, Adafruit_NeoPixel

1. Arduino IDE

If you’re not using a recent version of the Arduino IDE, this would be a good time to upgrade.  If this is your first time using Arduino, head over to this guide to get it installed.  It's free and fairly simple to get set up.

2. Board Support

You'll need to tell the Arduino IDE which board you're using.  This takes just a few minutes of setup, and you'll only need to do it once. 

Here is a step-by-step tutorial for setting up QT Py

3. Libraries

All three libraries can be installed using the Arduino Library Manager — use SketchInclude LibraryManage Libraries… and search for all or part of the library’s name, then click “Install.”

Look for:

  • FastLED
  • Adafruit_FreeTouch
  • Adafruit_NeoPixel

Adafruit_NeoPixel isn’t absolutely required for this project, but it’s handy to have installed in case you have problems with FastLED. Troubleshooting the basics is a little easier with Adafruit_NeoPixel.

Upload the Code

Get the code by clicking "Download Project Zip" in the code listing below. Unzip the code into your Arduino project folder.

Plug your microcontroller into your computer with a USB cable.  In the Arduino IDE, go to File -> Open to open the code file. Go to Tools -> Boards and select the name of the board (Adafruit QT Py).  Then go to Tools -> Port and select the board there too.  (If it's not showing up there, be sure your microcontroller is plugged into your computer via USB).

// SPDX-FileCopyrightText: 2018 Erin St Blaine for Adafruit Industries
//
// SPDX-License-Identifier: MIT

// Code by Erin St. Blaine for Adafruit Industries
// NeoPixel Aquarium Tutorial: https://learn.adafruit.com/neopixel-aquarium-with-submersible-lights/
// For QT Py board

#include "Adafruit_FreeTouch.h"
#include "FastLED.h"

#define CAPTOUCH_PIN A2  //A2 capacitive touch pin
#define CAPTOUCH_PIN2 A3  //A3 capacitive touch pin
#define NEOPIXEL_PIN A1  //A1 neopixel pin
#define NUM_LEDS    164  //how many pixels total

#define LED_TYPE    WS2812
#define COLOR_ORDER GRB
CRGB leds[NUM_LEDS];   //LED array 

// These variables will affect the way the gradient animation looks.  Feel free to mess with them.
int SPEEDO = 10;          
int STEPS = 50;         
int HUE = 0;            
int SATURATION = 255;  
int COLORCHANGE = 50;        
int BRIGHTNESS = 200;     
int onBright = 200;

// Calibrating your capacitive touch sensitivity: Change this variable to something between your capacitive touch serial readouts for on and off
int touch = 500;    

long oldState = 0;
long offState = 0;

// set up capacitive touch button using the FreeTouch library
Adafruit_FreeTouch qt_1 = Adafruit_FreeTouch(CAPTOUCH_PIN, OVERSAMPLE_4, RESISTOR_50K, FREQ_MODE_NONE);
Adafruit_FreeTouch qt_2 = Adafruit_FreeTouch(CAPTOUCH_PIN2, OVERSAMPLE_4, RESISTOR_50K, FREQ_MODE_NONE);

TBlendType    currentBlending;
CRGBPalette16 currentPalette;


void setup() {
  Serial.begin(115200);

  if (! qt_1.begin())  
   Serial.println("Failed to begin qt on pin A2");
  if (! qt_2.begin())  
   Serial.println("Failed to begin qt on pin A3");
   FastLED.addLeds<WS2812, NEOPIXEL_PIN, COLOR_ORDER>(leds, NUM_LEDS);  // Set up neopixels with FastLED
   FastLED.setBrightness(BRIGHTNESS); // set global brightness
   FastLED.setMaxPowerInVoltsAndMilliamps(3,350);  //Constrain FastLED's power usage
}

void loop() {
  
  Serial.print(qt_1.measure());
  Serial.write(' ');
  checkpress();   //check to see if the button's been pressed
  delay(20);
}

void checkpress() {

// Get current button state.
 
    long newState =  qt_1.measure(); 
    long switchState = qt_2.measure(); 
    Serial.println(qt_1.measure());
    Serial.println(qt_2.measure());
   if (newState > touch && oldState < touch) {
    // Short delay to debounce button.
    delay(500);
    // Check if button is still low after debounce.
    long newState =  qt_1.measure(); 
    long switchState = qt_2.measure();
    }
    if (switchState > touch && offState < touch) {
    // Short delay to debounce button.
    delay(500);
    // Check if button is still low after debounce.
    long newState =  qt_1.measure(); 
    }

 
  if (newState > touch ) {  
     BRIGHTNESS = onBright;
     HUE=HUE+COLORCHANGE;  // change the hue by a specified amount each time the cap touch pad is activated
  if (HUE > 255){
    HUE=0;}
   Gradient();
    }
//  if (HUE==250) {
//    dark();
//  }
    else {
      Gradient();
      
    }
   if (switchState > touch) {
      if (BRIGHTNESS == onBright){
        dark();
        switchState = 0;
        BRIGHTNESS = 0;
      }
      else {
        BRIGHTNESS = onBright;
      }
   }

   
    
  // Set the last button state to the old state.
  oldState = newState;
  offState = switchState;

} 



// GRADIENT --------------------------------------------------------------
void Gradient()
{
  SetupGradientPalette();

  static uint8_t startIndex = 0;
  startIndex = startIndex - 1;  // motion speed

  FillLEDsFromPaletteColors( startIndex);
  FastLED.show();
  FastLED.delay(SPEEDO);
}

// adjust hue, saturation and brightness values here to make a pleasing gradient

void SetupGradientPalette()
{
  CRGB light = CHSV( HUE + 25, SATURATION - 20, BRIGHTNESS);
  CRGB lightmed = CHSV (HUE + 15, SATURATION - 10, BRIGHTNESS-50);
  CRGB medium = CHSV ( HUE + 10, SATURATION - 15, BRIGHTNESS);
  CRGB dark  = CHSV( HUE, SATURATION, BRIGHTNESS);
  CRGB black = CHSV (HUE, SATURATION, 0);
  
  currentPalette = CRGBPalette16( 
    black,  light,  light,  light,
    lightmed, lightmed, lightmed,  medium,
    medium,  medium,  medium,  dark,
    dark, dark, dark,  black );
}

void FillLEDsFromPaletteColors( uint8_t colorIndex)
{
  uint8_t brightness = BRIGHTNESS;
  
  for( int i = 0; i < NUM_LEDS; i++) {
    leds[i] = ColorFromPalette( currentPalette, colorIndex, brightness, currentBlending);
    colorIndex += STEPS;
  }
}

void dark()
{ 
  for(int i = 0; i < NUM_LEDS; i++) { 
  leds[i] = CRGB::Black; 
  FastLED.show();
  delay(20);
}
}

This guide was first published on Dec 01, 2020. It was last updated on Nov 18, 2020.

This page (Software) was last updated on Nov 29, 2020.

Text editor powered by tinymce.