help with cap touch and knock sensor

I am currently trying to combine a knock sensor via piezo and a knock sensor sketch with a capacitive touch sensor via a stripped wire and a sketch that uses the CapSense library. I have been able to get each sketch to work separately but am having problems getting them to work together.

Here is the code:

This sketch combines the Knock Sensor sketch on
along with a capacitive sense sketch.

created Nov. 18 2010
by Tiffany Pippin


// these constants won’t change:
const int ledPin = 13; // led connected to digital pin 13
const int knockSensor = 0; // the piezo is connected to analog pin 0
const int threshold = 50; // threshold value to decide when the detected sound is a knock or not

// these variables will change:
int sensorReading = 0; // variable to store the value read from the sensor pin
int ledState = LOW; // variable used to store the last LED status, to toggle the light

int i;

unsigned int x, y;

float accum, fout, fval = .2; // these are variables for a simple low-pass (smoothing) filter - fval of 1 = no filter - .001 = max filter

//## LED

int ledCount = 8;
int ledPins = {

2, 3, 4, 5, 6, 7}; // an array of pin numbers to which LEDs are attached

void setup() {
pinMode(ledPin, OUTPUT);

DDRB=B101; // DDR is the pin direction register - governs inputs and outputs- 1’s are outputs

// Arduino pin 8 output, pin 9 input, pin 10 output for “guard pin”

// preceding line is equivalent to three lines below

// pinMode(8, OUTPUT); // output pin

// pinMode(9, INPUT); // input pin

// pinMode(10, OUTPUT); // guard pin

digitalWrite(10, LOW); //could also be HIGH - don’t use this pin for changing output though

// loop over the pin array and set them all to output:

for (int thisLed = 0; thisLed < ledCount; thisLed++) {

pinMode(ledPins[thisLed], OUTPUT);


void loop() {
// read the sensor and store it in the variable sensorReading:
sensorReading = analogRead(knockSensor);

// if the sensor reading is greater than the threshold:
if (sensorReading >= threshold) {
// toggle the status of the ledPin:
ledState = !ledState;
// update the LED pin itself:
digitalWrite(ledPin, ledState);
// send the string “Knock!” back to the computer, followed by newline

y = 0; // clear out variables

x = 0;

for (i=0; i < 4 ; i++ ){ // do it four times to build up an average - not really neccessary but takes out some jitter

// LOW-to-HIGH transition

PORTB = PORTB | 1; // Same as line below - shows programmer chops but doesn’t really buy any more speed

// digitalWrite(8, HIGH);

// output pin is PortB0 (Arduino 8), sensor pin is PortB1 (Arduinio 9)

while ((PINB & B10) != B10 ) { // while the sense pin is not high

// while (digitalRead(9) != 1) // same as above port manipulation above - only 20 times slower!




// HIGH-to-LOW transition

PORTB = PORTB & 0xFE; // Same as line below - these shows programmer chops but doesn’t really buy any more speed

//digitalWrite(8, LOW);

while((PINB & B10) != 0 ){ // while pin is not low – same as below only 20 times faster

// while(digitalRead(9) != 0 ) // same as above port manipulation - only 20 times slower!



fout = (fval * (float)x) + ((1-fval) * accum); // Easy smoothing filter “fval” determines amount of new data in fout
accum = fout;

int mappedFout;
mappedFout = map(fout, 1100, 1400, 0, ledCount);

//Serial.print((long)x, DEC); // raw data - Low to High
// Serial.print( " ");
// Serial.print((long)y, DEC); // raw data - High to Low
Serial.print( " ");
Serial.println( (long)fout, DEC); // Smoothed Low to High

// map the result to a range from 0 to the number of LEDs:
int ledLevel = mappedFout;

// loop over the LED array:

for (int thisLed = 0; thisLed < ledCount; thisLed++) {
// if the array element’s index is less than ledLevel,
// turn the pin for this element on:
if (thisLed < ledLevel) {
digitalWrite(ledPins[thisLed], HIGH);
// turn off all pins higher than the ledLevel:
else {
digitalWrite(ledPins[thisLed], LOW);

int piezoFreq = map(fout, 1100, 1400, 0, 2500);


delay(100); // delay to avoid overloading the serial port buffer

I’m new to this and not that great with coding yet, but when I use the two separately it works. This particular code will upload and when I play it says complete, no errors. So can anyone shed some light on what I maybe doing wrong?

I have 6 leds attached to digital pins 2-7 for capacitive touch, and this part works fine with this sketch. It’s actually just the knock sensor I can’t get to work.

I have A0 going to 1M resistor and red piezo wire. I have the ground on Analog going to the black wire on piezo and other side of resistor.


Does this code look any better in your IDE than it does posted here?

Curly braces should be lined up, and code in blocks should be indented.

if(someValue == someOtherValue)
   // Do something
   // Do something

Curly braces should be used to enclose blocks of code, not randomly placed. If there is not if, while, for, or switch statement, there is (generally) no need to enclose a random chunk of code in curly braces.

Comments should explain what is happening, not state the obvious.

   // toggle the status of the ledPin:
   ledState = !ledState;  
   // update the LED pin itself:        
   digitalWrite(ledPin, ledState);

delay(100);  // delay to avoid overloading the serial port buffer

Those comments are not terribly useful.

If you have a problem with code, post all the code, but only the stuff that illustrates the problem. Commented out code should be DELETED.

when I play it says complete

Play what?

It's actually just the knock sensor I can't get to work.

Remove all the stuff not related to the knock sensor. Does it resume working?

If so, start adding stuff (that you understand) back in, and see if/when it quits working.

Okay, I will try this. I actually started with a CapSense code that had a buzzer included so there may be some stuff that doesn't belong in the code. I will go back and start with the knock sensor code and then add in the CapSense code without buzzer included and see what happens.

The two codes work separately fine, just not as I combined them. So I know that my board hookup is right, I'm just not good at the code language. Thanks for your help!

Also if anyone has link to someone else who has combined the two as two separate inputs I would love to see it. I couldn't find anything on my own on the internet.