SHARE
TWEET

KnockBox

tuixte Jul 2nd, 2013 1,050 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /************************************/
  2. /************ KNOCK BOX *************/
  3. /*** A project by Michele Marolla ***/
  4. /************************************/
  5.  
  6. #include <Servo.h>
  7.  
  8. /* Pins */
  9. const int closedLED_p = 5;
  10. const int openLED_p = 6;
  11. const int inputLED_p = 7;
  12. const int rhythmBTN_p = 8;
  13. const int closeBTN_p = 9;
  14. const int playBTN_p = 10;
  15. const int piezo_p = A0;
  16. const int servo_p = 11;
  17.  
  18.  
  19. /* Rhythm Management */
  20. const int RHYTHM_SIZE = 10;
  21. const int TOLERANCE = 150;
  22. const int TRESHOLD = 20;
  23. class Rhythm{
  24.   private:
  25.     int rhythm[RHYTHM_SIZE];
  26.  
  27.   public:
  28.     Rhythm();
  29.     void getRhythm(int *rhythm_a);
  30.     void setRhythm(const int rhythm_a[RHYTHM_SIZE]);
  31.     int getActualSize() const;
  32.     int getActualSize(const int rhythm_a[RHYTHM_SIZE]) const;
  33.     void play() const;
  34.     void play(const int rhythm_a[RHYTHM_SIZE]) const;
  35.     int listenForKnock() const;
  36.     void listenForRhythm(int *rhythm_a);
  37.     boolean checkRhythm();
  38.    
  39. };
  40.  
  41. Rhythm::Rhythm(){
  42.   int temp[RHYTHM_SIZE] = {500, 500, 100, -1, -1, -1, -1, -1, -1, -1};
  43.   this->setRhythm(temp);
  44. }
  45.  
  46. void Rhythm::getRhythm(int *rhythm_a){
  47.   for(int i=0; i<RHYTHM_SIZE; i++)
  48.     rhythm_a[i] = this->rhythm[i];
  49. }
  50.  
  51. void Rhythm::setRhythm(const int rhythm_a[RHYTHM_SIZE]){
  52.   for(int i=0; i<RHYTHM_SIZE; i++)
  53.     this->rhythm[i] = rhythm_a[i];
  54. }
  55.  
  56. int Rhythm::getActualSize() const{
  57.   return this->getActualSize(this->rhythm);
  58. }
  59.  
  60. int Rhythm::getActualSize(const int rhythm_a[RHYTHM_SIZE]) const{
  61.   int count = 0;
  62.   for(int i=0; i<RHYTHM_SIZE; i++){
  63.     if(rhythm_a[i] == -1)
  64.       break;
  65.     count++;
  66.   }
  67.   return count;
  68. }
  69.  
  70. void Rhythm::play() const{
  71.   this->play(this->rhythm);
  72. }
  73.  
  74. void Rhythm::play(const int rhythm_a[RHYTHM_SIZE]) const{
  75.   pinMode(piezo_p, OUTPUT);
  76.  
  77.   // First knock
  78.   tone(piezo_p, 2000); delay(100); noTone(piezo_p);
  79.  
  80.   // Other knocks
  81.   for(int i=0; i<this->getActualSize(rhythm_a); i++){
  82.     delay(rhythm_a[i]);
  83.     tone(piezo_p, 2000); delay(100); noTone(piezo_p);
  84.   }
  85. }
  86.  
  87. int Rhythm::listenForKnock() const{
  88.   Serial.println("LISTEN FOR KNOCK");
  89.  
  90.   pinMode(piezo_p, INPUT);
  91.   unsigned long time = millis();
  92.   unsigned long temp;
  93.   while(true){
  94.     temp = millis() - time;
  95.     if(temp >= 3000)
  96.       return -1;
  97.     if((analogRead(piezo_p) >= TRESHOLD) && (temp >= 100)){
  98.       digitalWrite(inputLED_p, HIGH);
  99.       delay(20);
  100.       digitalWrite(inputLED_p, LOW);
  101.       return (int)temp;
  102.     }
  103.     delay(50);
  104.   }
  105. }
  106.  
  107. void Rhythm::listenForRhythm(int *rhythm_a){
  108.   int time;
  109.   this->listenForKnock(); // First knock
  110.   int i;
  111.   for(i=0; i<RHYTHM_SIZE; i++){
  112.     time = this->listenForKnock();
  113.     if(time == -1)
  114.       break;
  115.     rhythm_a[i] = time;
  116.   }
  117.   for(; i<RHYTHM_SIZE; i++)
  118.     rhythm_a[i] = -1;
  119. }
  120.  
  121. boolean Rhythm::checkRhythm(){
  122.   Serial.println("CHECK RHYTHM");
  123.  
  124.   int time;
  125.  
  126.   // Listen and check
  127.   int temp = this->listenForKnock();
  128.   while(temp == -1)
  129.     temp = this->listenForKnock(); // First knock
  130.    
  131.   for(int i=0; i<this->getActualSize(); i++){
  132.     time = this->listenForKnock();
  133.     if((time == -1) || (time > 3000))
  134.       return false;
  135.     if((time < this->rhythm[i]-TOLERANCE)
  136.       || (time > this->rhythm[i]+TOLERANCE))
  137.       return false;
  138.   }
  139.   return true;
  140. }
  141.  
  142. /* Box Management */
  143. class Box{
  144.   private:
  145.     boolean locked;
  146.     Servo servo;
  147.    
  148.   public:
  149.     Box();
  150.     boolean isLocked() const;
  151.     void openBox();
  152.     void closeBox();
  153.     void recordNewRhythm(Rhythm *rhythm);
  154. };
  155.  
  156. Box::Box(){
  157.   this->locked = true;
  158.   this->servo.attach(servo_p);
  159.   this->servo.write(90);
  160.   digitalWrite(openLED_p, LOW);
  161.   digitalWrite(inputLED_p, LOW);
  162.   digitalWrite(closedLED_p, HIGH);
  163. }
  164.  
  165. boolean Box::isLocked() const{
  166.   return this->locked;
  167. }
  168.  
  169. void Box::openBox(){
  170.   this->locked = false;
  171.   this->servo.write(0);
  172.   digitalWrite(openLED_p, HIGH);
  173.   digitalWrite(inputLED_p, LOW);
  174.   digitalWrite(closedLED_p, LOW);
  175. }
  176.  
  177. void Box::closeBox(){
  178.   this->locked = true;
  179.   this->servo.write(90);
  180.   digitalWrite(openLED_p, LOW);
  181.   digitalWrite(inputLED_p, LOW);
  182.   digitalWrite(closedLED_p, HIGH);
  183. }
  184.  
  185. void Box::recordNewRhythm(Rhythm *rhythm){
  186.   digitalWrite(openLED_p, HIGH);
  187.   digitalWrite(closedLED_p, HIGH);
  188.  
  189.   int newRhythm[RHYTHM_SIZE];
  190.   rhythm->listenForRhythm(&newRhythm[0]);
  191.   rhythm->play(newRhythm);
  192.   while(true){
  193.     if(digitalRead(closeBTN_p) == HIGH){
  194.       digitalWrite(closedLED_p, LOW);
  195.       delay(100);
  196.       return;
  197.     }else if(digitalRead(rhythmBTN_p) == HIGH){
  198.       rhythm->setRhythm(newRhythm);
  199.       this->closeBox();
  200.       return;
  201.     }
  202.     delay(100);
  203.   }
  204. }
  205.  
  206. /* Setup */
  207. Box *box;
  208. Rhythm *rhythm;
  209.  
  210. void setup(){
  211.   Serial.begin(9600);
  212.  
  213.   pinMode(closedLED_p, OUTPUT);
  214.   pinMode(openLED_p, OUTPUT);
  215.   pinMode(inputLED_p, OUTPUT);
  216.   pinMode(rhythmBTN_p, INPUT);
  217.   pinMode(closeBTN_p, INPUT);
  218.   pinMode(piezo_p, INPUT);
  219.   pinMode(playBTN_p, INPUT);
  220.  
  221.   box = new Box();
  222.   rhythm = new Rhythm();
  223. }
  224.  
  225. /* Loop */
  226. void loop(){
  227.   Serial.print("Box is locked? ");
  228.   Serial.println(box->isLocked());
  229.  
  230.   if(box->isLocked()){
  231.     if(rhythm->checkRhythm()){
  232.       box->openBox();
  233.     }else{
  234.       tone(piezo_p, 500);
  235.       delay(200);
  236.       noTone(piezo_p);
  237.     }
  238.   }else{
  239.     if(digitalRead(closeBTN_p) == HIGH)
  240.       box->closeBox();
  241.     else if(digitalRead(rhythmBTN_p) == HIGH)
  242.       box->recordNewRhythm(rhythm);
  243.     else if(digitalRead(playBTN_p) == HIGH)
  244.       rhythm->play();
  245.   }
  246.   delay(100);
  247. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top