Fix indentation.

This commit is contained in:
Henrik Ekblad
2014-08-17 14:45:17 +02:00
parent 0d00e22498
commit ec53d08e57
@@ -1,4 +1,4 @@
/* Secret Knock Sensor
/* Secret Knock Sensor
See original instructions here (note: The MySensors adopted code might differ in wiring. The instructions below is correct):
https://learn.adafruit.com/secret-knock-activated-drawer-lock/
@@ -22,43 +22,43 @@
Connect radio according as usual(you can skip IRQ pin)
http://www.mysensors.org/build/connect_radio
*/
*/
#include <MySensor.h>
#include <SPI.h>
#include <MySensor.h>
#include <SPI.h>
#define CHILD_ID 99 // Id of the sensor child
#define CHILD_ID 99 // Id of the sensor child
const byte eepromValid = 121; // If the first byte in eeprom is this then the data is valid.
const byte eepromValid = 121; // If the first byte in eeprom is this then the data is valid.
/*Pin definitions*/
const int programButton = 0; // (Digital 0) Record A New Knock button.
const int ledPin = 1; // (Digital 1) The LED pin (if any)
const int knockSensor = 5; // (Digital 5) for using the microphone digital output (tune knob to register knock)
const int audioOut = 2; // (Digital 2) for using the peizo as an output device. (Thing that goes beep.)
const int lockPin = 4; // (Digital 4) The pin that activates the relay/solenoid lock.
/*Pin definitions*/
const int programButton = 0; // (Digital 0) Record A New Knock button.
const int ledPin = 1; // (Digital 1) The LED pin (if any)
const int knockSensor = 5; // (Digital 5) for using the microphone digital output (tune knob to register knock)
const int audioOut = 2; // (Digital 2) for using the peizo as an output device. (Thing that goes beep.)
const int lockPin = 4; // (Digital 4) The pin that activates the relay/solenoid lock.
/*Tuning constants. Changing the values below changes the behavior of the device.*/
int threshold = 3; // Minimum signal from the piezo to register as a knock. Higher = less sensitive. Typical values 1 - 10
const int rejectValue = 25; // If an individual knock is off by this percentage of a knock we don't unlock. Typical values 10-30
const int averageRejectValue = 15; // If the average timing of all the knocks is off by this percent we don't unlock. Typical values 5-20
const int knockFadeTime = 150; // Milliseconds we allow a knock to fade before we listen for another one. (Debounce timer.)
const int lockOperateTime = 2500; // Milliseconds that we operate the lock solenoid latch before releasing it.
const int maximumKnocks = 20; // Maximum number of knocks to listen for.
const int knockComplete = 1200; // Longest time to wait for a knock before we assume that it's finished. (milliseconds)
/*Tuning constants. Changing the values below changes the behavior of the device.*/
int threshold = 3; // Minimum signal from the piezo to register as a knock. Higher = less sensitive. Typical values 1 - 10
const int rejectValue = 25; // If an individual knock is off by this percentage of a knock we don't unlock. Typical values 10-30
const int averageRejectValue = 15; // If the average timing of all the knocks is off by this percent we don't unlock. Typical values 5-20
const int knockFadeTime = 150; // Milliseconds we allow a knock to fade before we listen for another one. (Debounce timer.)
const int lockOperateTime = 2500; // Milliseconds that we operate the lock solenoid latch before releasing it.
const int maximumKnocks = 20; // Maximum number of knocks to listen for.
const int knockComplete = 1200; // Longest time to wait for a knock before we assume that it's finished. (milliseconds)
byte secretCode[maximumKnocks] = {50, 25, 25, 50, 100, 50, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; // Initial setup: "Shave and a Hair Cut, two bits."
int knockReadings[maximumKnocks]; // When someone knocks this array fills with the delays between knocks.
int knockSensorValue = 0; // Last reading of the knock sensor.
boolean programModeActive = false; // True if we're trying to program a new knock.
byte secretCode[maximumKnocks] = {50, 25, 25, 50, 100, 50, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; // Initial setup: "Shave and a Hair Cut, two bits."
int knockReadings[maximumKnocks]; // When someone knocks this array fills with the delays between knocks.
int knockSensorValue = 0; // Last reading of the knock sensor.
boolean programModeActive = false; // True if we're trying to program a new knock.
bool lockStatus;
MySensor gw;
MyMessage lockMsg(CHILD_ID, V_LOCK_STATUS);
bool lockStatus;
MySensor gw;
MyMessage lockMsg(CHILD_ID, V_LOCK_STATUS);
void setup() {
void setup() {
pinMode(ledPin, OUTPUT);
pinMode(knockSensor, INPUT);
@@ -80,9 +80,9 @@
setLockState(lockStatus, true); // Now set the last known state and send it to controller
delay(500); // This delay is here because the solenoid lock returning to place can otherwise trigger and inadvertent knock.
}
}
void loop() {
void loop() {
gw.process(); // Process incomming messages
// Listen for any knock at all.
knockSensorValue = digitalRead(knockSensor);
@@ -124,10 +124,10 @@
listenToSecretKnock(); // We have our first knock. Go and see what other knocks are in store...
}
}
}
// Records the timing of knocks.
void listenToSecretKnock(){
// Records the timing of knocks.
void listenToSecretKnock(){
int i = 0;
// First reset the listening array.
for (i=0; i < maximumKnocks; i++){
@@ -189,11 +189,11 @@
} else { // If we're in programming mode we still validate the lock because it makes some numbers we need, we just don't do anything with the return.
validateKnock();
}
}
}
// Unlocks the door.
void setLockState(bool state, bool send){
// Unlocks the door.
void setLockState(bool state, bool send){
if (state)
Serial.println("open lock");
else
@@ -206,11 +206,11 @@
gw.saveState(0,state);
lockStatus = state;
delay(500); // This delay is here because releasing the latch can cause a vibration that will be sensed as a knock.
}
}
// Checks to see if our knock matches the secret.
// Returns true if it's a good knock, false if it's not.
boolean validateKnock(){
// Checks to see if our knock matches the secret.
// Returns true if it's a good knock, false if it's not.
boolean validateKnock(){
int i = 0;
int currentKnockCount = 0;
@@ -266,11 +266,11 @@
}
return true;
}
}
// reads the secret knock from EEPROM. (if any.)
void readSecretKnock(){
// reads the secret knock from EEPROM. (if any.)
void readSecretKnock(){
byte reading;
int i;
reading = gw.loadState(1);
@@ -279,20 +279,20 @@
secretCode[i] = gw.loadState(i+2);
}
}
}
}
//saves a new pattern too eeprom
void saveSecretKnock(){
//saves a new pattern too eeprom
void saveSecretKnock(){
gw.saveState(1, 0); // clear out the signature. That way we know if we didn't finish the write successfully.
for (int i=0; i < maximumKnocks; i++){
gw.saveState(i+2, secretCode[i]);
}
gw.saveState(1, eepromValid); // all good. Write the signature so we'll know it's all good.
}
}
// Plays back the pattern of the knock in blinks and beeps
void playbackKnock(int maxKnockInterval){
// Plays back the pattern of the knock in blinks and beeps
void playbackKnock(int maxKnockInterval){
digitalWrite(ledPin, LOW);
delay(1000);
digitalWrite(ledPin, HIGH);
@@ -307,22 +307,22 @@
}
}
digitalWrite(ledPin, LOW);
}
}
// Deals with the knock delay thingy.
void knockDelay(){
// Deals with the knock delay thingy.
void knockDelay(){
int itterations = (knockFadeTime / 20); // Wait for the peak to dissipate before listening to next one.
for (int i=0; i < itterations; i++){
delay(10);
analogRead(knockSensor); // This is done in an attempt to defuse the analog sensor's capacitor that will give false readings on high impedance sensors.
delay(10);
}
}
}
// Plays a non-musical tone on the piezo.
// playTime = milliseconds to play the tone
// delayTime = time in microseconds between ticks. (smaller=higher pitch tone.)
void chirp(int playTime, int delayTime){
// Plays a non-musical tone on the piezo.
// playTime = milliseconds to play the tone
// delayTime = time in microseconds between ticks. (smaller=higher pitch tone.)
void chirp(int playTime, int delayTime){
long loopTime = (playTime * 1000L) / delayTime;
pinMode(audioOut, OUTPUT);
for(int i=0; i < loopTime; i++){
@@ -331,11 +331,11 @@
digitalWrite(audioOut, LOW);
}
pinMode(audioOut, INPUT);
}
}
void incomingMessage(const MyMessage &message) {
void incomingMessage(const MyMessage &message) {
// We only expect one type of message from controller. But we better check anyway.
if (message.type==V_LOCK_STATUS) {
// Change relay state
@@ -345,5 +345,5 @@
Serial.print("Incoming lock status:");
Serial.println(message.getBool());
}
}
}