// put your main // import the library
#include <Servo.h>
#include <MsTimer2.h>
// create an instance of the servo library
Servo myServo,myServo2;
unsigned long ShowTime;
const int piezo = A0; // pin the piezo is attached to
const int piezo2 = A1; // pin the piezo is attached to
const int piezo3 = A2; // pin the piezo is attached to Lock
const int switchPin = 2; // pin the switch is attached to
const int yellowLed = 3; // pin the yellow LED is attached to
const int greenLed = 4; // pin the green LED is attached to
const int redLed = 5; // pin the red LED is attached to// variable for the piezo value
const int Led = 12;
const int Speaker = 13;
int knockVal=0,knockVal2=0,knockVal3=0;
// variable for the switch value
int switchVal;
// variables for the high and low limits of the knock value
const int quietKnock = 5;
const int loudKnock = 120;
// variable to indicate if locked or not
boolean locked = false;
boolean lockedknob = false;
// how many valid knocks you've received
int numberOfKnocks = 0;
//========================================================
//===============================================================
void setup(){
// attach the servo to pin 9
myServo.attach(9);
myServo2.attach(10);
// make the LED pins outputs
pinMode(yellowLed, OUTPUT);
pinMode(redLed, OUTPUT);
pinMode(greenLed, OUTPUT);
pinMode(greenLed, OUTPUT);
pinMode(Led, OUTPUT);
pinMode(Speaker, OUTPUT);
// set the switch pin as an input
pinMode(switchPin, INPUT);
pinMode(piezo, INPUT);
pinMode(piezo2, INPUT);
pinMode(piezo3, INPUT);
// start serial communication for debugging
Serial.begin(9600);
// turn the green LED on
digitalWrite(greenLed, HIGH);
// move the servo to the unlocked position
// myServo.write(180);
// print status to the serial monitor
// Serial.println("the door is opened!");
//MsTimer2::set(200, flash); // 500ms period
// MsTimer2::start();
}
void loop()
{
// delay(100);
knockVal=0;
knockVal2=0;
knockVal3=0;
for(int i=0; i<30;i++)
{
knockVal = knockVal+ analogRead(piezo);
delay(1);
knockVal2 = knockVal2+ analogRead(piezo2);
delay(1);
knockVal3 = knockVal3+ analogRead(piezo3);
delay(1);
}
if((( knockVal > loudKnock)||(knockVal2 > loudKnock))&&( lockedknob == false)){
if(locked == false)
locked=true;
else
locked = false;
digitalWrite(yellowLed, HIGH);
delay(500);
}
if(( knockVal3 > loudKnock)&&(locked==true)){
if(lockedknob == false)
lockedknob= true;
else
lockedknob = false;
digitalWrite(yellowLed, HIGH);
delay(500);
}
if( ( knockVal > loudKnock)||(knockVal2 > loudKnock))
if( locked == true){
// change the status LEDs
// digitalWrite(greenLed,LOW);
// digitalWrite(redLed,HIGH);
// move the servo to the locked position
Serial.print("piezo value befor servo is :");
Serial.println(knockVal);
Serial.print("piezo2 value befor servo is :");
Serial.println(knockVal2);
myServo.write(100);
delay(3500);
// myServo.write(100);
// delay(5500);
// print out status
Serial.println("the box is closed now!");
// wait for the servo to move into position
delay (200);
Serial.println("Ready for new command......");
digitalWrite(yellowLed, LOW);
delay(100);
// knockVal=2;
// knockVal2=2;
}
if(( knockVal3 > loudKnock)&&( lockedknob == false)){
digitalWrite(Led,LOW);
digitalWrite(yellowLed, HIGH);
digitalWrite(Speaker,LOW);
delay(500);
// change the status LEDs
// move the servo to the locked position
Serial.print("piezo value befor servo is :");
Serial.println(knockVal);
Serial.print("piezo3 value befor servo is :");
Serial.println(knockVal3);
myServo2.write(100);
delay(1500);
// myServo.write(100);
// delay(2500);
// print out status
Serial.println("knob is unlocked !");
// wait for the servo to move into position
delay (200);
Serial.println("Ready for new command......");
// knockVal=2;
// knockVal2=2;
digitalWrite(greenLed,HIGH);
digitalWrite(redLed,LOW);
digitalWrite(yellowLed, LOW);
digitalWrite(Led,LOW);
digitalWrite(Speaker,LOW);
delay(100);
}
if(( knockVal3 > loudKnock)&&( lockedknob == true))
{
// change the status LEDs
digitalWrite(greenLed,LOW);
digitalWrite(redLed,HIGH);
digitalWrite(Led,HIGH);
// move the servo to the locked position
Serial.print("piezo value befor servo is :");
Serial.println(knockVal);
Serial.print("piezo3 value befor servo is :");
Serial.println(knockVal3);
myServo2.write(10);
delay(1500);
// myServo.write(100);
// delay(2500);
// print out status
Serial.println("knob is locked !");
// wait for the servo to move into position
delay (200);
Serial.println("Ready for new command......");
digitalWrite(yellowLed, LOW);
// knockVal=2;
// knockVal2=2;
}
// if the box is locked
if( ( knockVal > loudKnock)||(knockVal2 > loudKnock))
if(locked == false){
// move the servo to the unlocked position
//<<==========================================================>>
Serial.print("piezo value befor servo is :");
Serial.println(knockVal);
Serial.print("piezo2 value befor servo is :");
Serial.println(knockVal2);
myServo.write(10);
// wait for it to move
delay(3500);
// myServo.write(10);
//delay(1500);
// change status LEDs
// digitalWrite(greenLed,HIGH);
// digitalWrite(redLed,LOW);
Serial.println("the box is opened now!");
delay(200);
Serial.println("Ready for new command......");
digitalWrite(yellowLed, LOW);
}
}
|