Hi,
I've been playing about today and managed to get the potentiometer to adjust the threshold value and I've changed the wiring of it slightly. It now uses different pins as can be seen from the code below:
/*
Knock Sensor
This sketch reads a piezo element to detect a knocking sound.
It reads an analog pin and compares the result to a set threshold.
If the result is greater than the threshold, it writes "knock" to the serial
port, and toggles the LED on pin 13.
The circuit:
- positive connection of the piezo attached to analog in 0
- negative connection of the piezo attached to ground
- 1 megohm resistor attached from analog in 0 to ground
created 25 Mar 2007
by David Cuartielles <http://www.0j0.org>
modified 30 Aug 2011
by Tom Igoe
This example code is in the public domain.
https://www.arduino.cc/en/Tutorial/BuiltInExamples/Knock
*/
#include <Adafruit_NeoPixel.h>
#define PIXEL_PIN 6 // Digital IO pin connected to the NeoPixels.
#define PIXEL_COUNT 14 // Number of NeoPixels
// Declare our NeoPixel strip object:
Adafruit_NeoPixel strip(PIXEL_COUNT, PIXEL_PIN, NEO_GRB + NEO_KHZ800);
// Argument 1 = Number of pixels in NeoPixel strip
// Argument 2 = Arduino pin number (most are valid)
// Argument 3 = Pixel type flags, add together as needed:
// NEO_KHZ800 800 KHz bitstream (most NeoPixel products w/WS2812 LEDs)
// NEO_KHZ400 400 KHz (classic 'v1' (not v2) FLORA pixels, WS2811 drivers)
// NEO_GRB Pixels are wired for GRB bitstream (most NeoPixel products)
// NEO_RGB Pixels are wired for RGB bitstream (v1 FLORA pixels, not v2)
// NEO_RGBW Pixels are wired for RGBW bitstream (NeoPixel RGBW products)
// these constants won't change:
// const int ledPin = 13; // LED connected to digital pin 13
const int knockSensor = A5; // the piezo is connected to analog pin 0
const int threshold = A0; // 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 thresholdReading = map(threshold, 0, 1023, 1, 255);
void setup() {
pinMode(PIXEL_PIN, OUTPUT); // declare the ledPin as as OUTPUT
Serial.begin(9600); // use the serial port
strip.begin(); // Initialize NeoPixel strip object (REQUIRED)
strip.show(); // Initialize all pixels to 'off'
}
void loop() {
// read the sensor and store it in the variable sensorReading:
sensorReading = analogRead(knockSensor);
thresholdReading = analogRead(threshold);
Serial.println(sensorReading);
Serial.println(thresholdReading);
// if the sensor reading is greater than the threshold:
if (sensorReading >= thresholdReading) {
colorWipe(strip.Color(255, 0, 0), 50); // Red
// toggle the status of the ledPin:
ledState = !ledState;
// update the LED pin itself:
digitalWrite(PIXEL_PIN, ledState);
// send the string "Knock!" back to the computer, followed by newline
Serial.println("Knock!");
//colorWipe();
}
delay(100); // delay to avoid overloading the serial port buffer
colorWipe(strip.Color( 0, 0, 0), 50); // Black/off
}
// Fill strip pixels one after another with a color. Strip is NOT cleared
// first; anything there will be covered pixel by pixel. Pass in color
// (as a single 'packed' 32-bit value, which you can get by calling
// strip.Color(red, green, blue) as shown in the loop() function above),
// and a delay time (in milliseconds) between pixels.
void colorWipe(uint32_t color, int wait) {
for(int i=0; i<strip.numPixels(); i++) { // For each pixel in strip...
strip.setPixelColor(i, color); // Set pixel's color (in RAM)
strip.show(); // Update strip to match
delay(wait); // Pause for a moment
}
}
The code is based on the knock example code included with Arduino IDE. I have modified it with neopixel code from buttonCycler example included with the adafruit neopixel library. I am running everything from the USB providing 5v as the moment but my project is going to be using a 3.7v Li-ion cell with a TP4056 and a MT3068 Boost module to boost the voltage to 5v and this will power the Nano and the neopixels.
The problem I've got now is that the code is running but the piezo disc isn't really sensitive to touch. In order to get anything out of it at all i'm having to really tap on it to get any sort of reading out of it. I'm currently using 27mm discs. Would increasing the size of the disc help with this? I have noted that without the 1M resistor the value of the piezo disc increases over time, so I'm guessing it acts like a capacitor and when I tap on it that value decreases and starts to build up again. I have tried with different value resistors but to no effect really.
The idea of this is that the piezo disc will sit on the underneath of the lid of a 3d printed enclosure and the neopixels would light up if it is touched at all. This is why it needs to be sensitive to the slightest touch.
Hope this helps.