Upload the test code below to your Teensy.  While it's plugged into your computer, open your Serial monitor.  You'll see a list of numbers scrolling down the screen.

With one finger on your switch lock, touch each charm and watch the numbers change.  Get a sense of what the numbers are doing.  You'll need a rough idea of the highs and lows you get when touching your charms to make the code work right.

//test code for Alohamora bottle

#include <CapacitiveSensor.h>
#include <FastLED.h>

#define DATA_PIN    23
#define LED_TYPE    WS2812
#define COLOR_ORDER GRB
#define NUM_LEDS    5        // Change this to reflect the number of LEDs you have

int HUE = 0;            
int SATURATION = 255;          
int BRIGHTNESS = 255;       

CRGB leds[NUM_LEDS];

CRGBPalette16 gCurrentPalette;
CRGBPalette16 gTargetPalette;


/*
 * CapitiveSense Library Demo Sketch
 * Paul Badger 2008
 * Uses a high value resistor e.g. 10M between send pin and receive pin
 * Resistor effects sensitivity, experiment with values, 50K - 50M. Larger resistor values yield larger sensor values.
 * Receive pin is the sensor pin - try different amounts of foil/metal on this pin
 * Modified by Becky Stern 2013 to change the color of one RGB Neo Pixel based on touch input
 */


CapacitiveSensor   cs_4_1 = CapacitiveSensor(4,1);        // 10M resistor between pins 4 & 2, pin 2 is sensor pin, add a wire and or foil if desired
CapacitiveSensor   cs_4_2 = CapacitiveSensor(4,2);        // 10M resistor between pins 4 & 2, pin 2 is sensor pin, add a wire and or foil if desired
CapacitiveSensor   cs_4_3 = CapacitiveSensor(4,3);        // 10M resistor between pins 4 & 2, pin 2 is sensor pin, add a wire and or foil if desired


void setup()                    
{
  delay(3000); // 3 second delay for recovery
  
  // tell FastLED about the LED strip configuration
  FastLED.addLeds<LED_TYPE,DATA_PIN,COLOR_ORDER>(leds, NUM_LEDS)
    .setCorrection(TypicalLEDStrip)
    .setDither(BRIGHTNESS < 255);

  // set master brightness control
  FastLED.setBrightness(BRIGHTNESS);
  Serial.begin(9600);

}
void loop(){
 
    long start = millis();
    long total1 =  cs_4_1.capacitiveSensor(30);
    long total2 =  cs_4_2.capacitiveSensor(30);
    long total3 =  cs_4_3.capacitiveSensor(30);

    
if (total1 > 3000){
 // digitalWrite(11, HIGH);
   BRIGHTNESS=255; HUE=0; Solid();


  } else if (total2 > 3000){
 // digitalWrite(13, HIGH);
   BRIGHTNESS=255; HUE=200; Solid();


} else if (total3 > 3000){
 // digitalWrite(11, HIGH);
   BRIGHTNESS=255; HUE=160; Solid();


} else { 
   BRIGHTNESS=100; HUE=55; Solid();
}

  Serial.print(millis() - start);        // check on performance in milliseconds
    Serial.print("\t");                    // tab character for debug windown spacing

    Serial.println(total1);                  // print sensor output 1
    Serial.print("\t");
     Serial.println(total2);                  // print sensor output 2
    Serial.print("\t");
    Serial.println(total3);                // print sensor output 3 

  delay(10);                             // arbitrary delay to limit data to serial port  
}



void Solid()
{
   fill_solid(leds, NUM_LEDS, CHSV(HUE, SATURATION, BRIGHTNESS)); 
   FastLED.show(); 
   delay(20); 
  
}

In my serial monitor, I see the numbers jumping from the 20s-30s to the 10,000s when I touch each charm.  I chose a value of 3000 for the sensor cutoff, which works really well with my charms.

Find the total1, total2 and total3 variables in the loop() portion of the test code and play with the cutoff numbers to reflect what your charms are doing.  When you've got it right, the bottle will change colors when you touch each charm.

Once you have this working reliably while plugged into your computer, unplug your arduino and try it with just the battery plugged in.  To make it work reliably, keep one finger on your on/off switch lock while you touch the charms.

Science Moment!

Capacitive touch sensors work great when they're plugged in to your computer or a power grid of some kind.  They need the tasty ground connection that comes with plugged-in power.  

Running capacitive touch sensors from a battery means no strong ground connection.. like, to the ground beneath your feet.  Capacitive touch sensors really want that connection.   

When you touch the metal of your on/off switch lock with your finger, your body and your feet on the ground will provide that needed ground connection.  Then the rest of the circuit is happy as a clam and will proceed to function properly.  

Okay!  Once you have your charms all working beautifully, it's time to assemble the bottle and make everything pretty.

This guide was first published on Apr 18, 2016. It was last updated on Apr 18, 2016.

This page (Sensor Calibration) was last updated on Mar 30, 2016.

Text editor powered by tinymce.