Advertisement
weaknetlabs

blizzard box prototype v.02

Jul 17th, 2014
429
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.49 KB | None | 0 0
  1. #include <Tone.h>
  2. #include <Keypad.h>
  3.  
  4. /*
  5.   Blizzard Box v.02
  6.   (c) GNU 2014
  7.   Designed by Douglas Berdeaux
  8.   WeakNetLabs@Gmail.com
  9. */
  10.  
  11. // array of tone output pins:
  12. int bbt1[12] = {1300,700,700,900,700,900,1100,700,900,1100,1100,1500}; // blue box tone 1
  13. int bbt2[12] = {1500,900,1100,1100,1300,1300,1300,1500,1500,1500,1700,1700}; // bluebox tone 2
  14. int rbt[2] = {1700,2200}; // 66 ms on/off
  15. int delayBbMsLong = 100; // 100 ms
  16. int delayBbMsShort = 70; // shorter
  17. int delayRbMsQ = 33; // 33 ms for $0.25 ACTS
  18. int delayRbMsO = 66; // Other (dime/nickel)
  19. const byte rows = 4; //four rows
  20. const byte cols = 4; //three columns
  21. boolean recording = 0; // are we recording?
  22. boolean recorded = 0; // we have a stored number
  23. char recTones[20] = {'x','x','x','x','x','x','x','x','x','x',
  24.   'x','x','x','x','x','x','x','x','x','x'}; // don't dial x digits.
  25. char keys[rows][cols] = { // matrix layout used by the Keypad library
  26.   {'1','2','3','a'},
  27.   {'4','5','6','b'},
  28.   {'7','8','9','c'},
  29.   {'#','0','*','d'}
  30. };
  31. char heldButton; // which button was held.
  32.  
  33. byte rowPins[rows] = {5,4,3,2}; //connect to the row pinouts of the keypad
  34. byte colPins[cols] = {9,8,7,6}; //connect to the column pinouts of the keypad
  35. // global objects
  36. Tone freq[2];
  37. Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, rows, cols );
  38.  
  39.  
  40. void setup(void){
  41.   // Serial.begin(9600); // Debugging Serial connection for printing to screen
  42.   freq[0].begin(11); // Initialize our first tone generator
  43.   freq[1].begin(12); // Initialize our second tone generator
  44.   keypad.setHoldTime(2000); // hold for two seconds to change state to HOLD
  45.   pinMode(10, INPUT);
  46. }
  47.  
  48. void loop(){
  49.   int buttonState = digitalRead(10);
  50.   if(buttonState == HIGH){
  51.      playTone(2600,1000);
  52.   }
  53.   char button = keypad.getKey();
  54.   if(keypad.getState() == HOLD){
  55.     int intB = heldButton - 48; // get Blue Box element index  
  56.     if(intB==52){ // long press D
  57.       if(recording){
  58.         playTone(2200,66);
  59.         playTone(1500,500);
  60.         recording=0; // stop recording
  61.       }else{
  62.         playTone(1500,66);      
  63.         playTone(2200,500);
  64.         recording=1; // save the digit pressed
  65.         for(int i=0;i<=19;i++){ // reset array
  66.            recTones[i] = 'x';
  67.         }
  68.       }
  69.     }
  70.   }
  71.   if (button){
  72.     int intB = button - 48; // get Blue Box element index
  73.     heldButton = button; // store this for later (hack)
  74.     if((intB<10&&intB>-1)||(intB>48&&intB<52)||intB==-13||intB==-6){ // 0-9,A-C
  75.       playTone(intB,delayBbMsLong); // play it
  76.       if(((intB<10&&intB>-1)||(intB==-13||intB==-6))&&recording){
  77.         for(int i=0;i<=19;i++){ // record it into array
  78.           if(recTones[i]=='x'){ // overwriet the first found 'x' char
  79.             recTones[i]=button; // store integer values to pass to playTone();
  80.             break; // leave the rest of the 'x's alone.
  81.           }
  82.         }
  83.       }
  84.     }else if(intB==52){
  85.        for(int i=0;i<=19;i++){
  86.         if(recTones[i] == 'x'){
  87.           break;
  88.         }else{
  89.           playTone((recTones[i] - 48),70);
  90.         }
  91.        }
  92.     }
  93.   }
  94. }
  95. void playTone(int freqElement,int delayMs){ // pass frequency element from array
  96.   if(freqElement==-13){ freqElement=10; } // KP
  97.   if(freqElement==-6){ freqElement=11; }  // ST
  98.   if(freqElement>-1&&freqElement<13){
  99.     freq[0].play(bbt1[freqElement]);
  100.     freq[1].play(bbt2[freqElement]);
  101.     delay(delayMs); // play tone for delay milliseconds.
  102.     stopTone(); // stop the tone
  103.     if(heldButton == 'd'){ delay(delayMs); } // need to delay between playing too.
  104.   }else if(freqElement > 48 && freqElement < 52){ // 49,50,51 = A,B,C
  105.     switch(freqElement){ // These are for Red box tones
  106.       case (49):
  107.         redBox(4,delayRbMsQ);
  108.         break;
  109.       case (50):
  110.         redBox(1,delayRbMsO);
  111.         break;
  112.       case (51):
  113.         redBox(0,delayRbMsO);
  114.         break;
  115.     }  
  116.   }
  117.   else if(freqElement>100){
  118.      // a specific frequency
  119.      freq[0].play(freqElement);
  120.      digitalWrite(12,HIGH); // needed voltage on pin or volume lowers!
  121.      delay(delayMs);
  122.      freq[0].stop(); // done
  123.   }else{
  124.   // THAIR SHOUL BE NO ELSE!!
  125.   }
  126. }
  127.  
  128. void redBox(int iterations,int delayMs){ // play tones
  129.   for(int i=0;i<=iterations;i++){
  130.     freq[0].play(rbt[0]);
  131.     freq[1].play(rbt[1]);
  132.     delay(delayMs);
  133.     stopTone();
  134.     delay(delayMs);
  135.   }
  136. }
  137. void stopTone(){ // stop them if they are playing
  138.   if(freq[0].isPlaying()){
  139.     freq[0].stop();
  140.   }
  141.   if(freq[1].isPlaying()){
  142.     freq[1].stop();
  143.   }
  144.   return;
  145. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement