Hardware components | ||||||
| × | 1 | ||||
| × | 1 | ||||
| × | 1 | ||||
| × | 1 | ||||
| × | 1 | ||||
| × | 1 | ||||
Software apps and online services | ||||||
|
Knock detector
C/C++This program detects knocks on piezo (pin 11) and turns on/off the LED strip via TIP102 on pin 3.
/* *************************************
Project name: LED strip control by knock
Author: Tekk (Peter Javorsky)
Date: 24/9/2015
Contact: tekk.sk (at) gmail.com
**************************************** */
int ledStripPin = 3; // this controls the TIP102 and turns on/off the LED strip
int knockSensor = 11; // this is where your piezo is connected, don't forget the 1 Megaohm resistor in parralell
int statePin = LOW;
int THRESHOLD = 27; // needs to be adjusted according to your piezo type!
void setup()
{
pinMode(ledStripPin, OUTPUT);
Serial.begin(9600);
delay(100); // wait for A/D converter and/or piezo to stabilize
}
void loop()
{
delay(1); // don't remove this delay - Sample size: 1ms
if (analogRead(knockSensor) >= THRESHOLD)
{
statePin = !statePin;
digitalWrite(ledStripPin, statePin);
Serial.println("Knock!");
delay(50); // wait a while for piezo to stabilize
}
}
/* *************************************
Project name: LED strip control by knock
Author: Tekk (Peter Javorsky)
Date: 24/9/2015
Contact: tekk.sk (at) gmail.com
**************************************** */
int ledStripPin = 3; // this controls the TIP102 and turns on/off the LED strip
int knockSensor = 11; // this is where your piezo is connected, don't forget the 1 Megaohm resistor in parralell
int statePin = LOW;
int THRESHOLD = 27; // needs to be adjusted according to your piezo type!
void setup()
{
pinMode(ledStripPin, OUTPUT);
Serial.begin(9600);
delay(100); // wait for A/D converter and/or piezo to stabilize
}
void loop()
{
delay(1); // don't remove this delay - Sample size: 1ms
if (analogRead(knockSensor) >= THRESHOLD)
{
if (!statePin)
{
unsigned long t = millis() + 350;
delay(50); // debounce only
while (millis() < t)
{
delay(1); // don't remove this delay - Sample size: 1ms
if (analogRead(knockSensor) >= THRESHOLD)
{
statePin = !statePin;
digitalWrite(ledStripPin, statePin);
Serial.print("Double ");
delay(50); // wait a while for piezo to stabilize
break;
}
}
}
else
{
statePin = !statePin;
}
digitalWrite(ledStripPin, statePin);
Serial.println("Knock!");
delay(50); // wait a while for piezo to stabilize
}
}
Comments