Advertisement
weaknetlabs

blizzard box prototype v.02

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