Advertisement
ncsonka

wav_trigger_trellis

Jun 18th, 2019
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <Wire.h>
  2. #include "Adafruit_Trellis.h"   //need adafruit trellis library
  3.  
  4.  
  5. String results[] = {"86420","86420","86420","86420",
  6.   "86420","86420","86420","86420",
  7.   "86420","86420","86420","86420",
  8.   "86420","86420","86420","86420",};
  9. //array of each step, keeps track of which drum tracks should play. For example, if the first digit is 8 it's off and 9 is on, sending a trigger out
  10. // on that specific step. even numbers = off, odd numbers = on.
  11.  
  12.  
  13. Adafruit_Trellis matrix0 = Adafruit_Trellis();
  14.  
  15. Adafruit_TrellisSet trellis =  Adafruit_TrellisSet(&matrix0);
  16.  
  17.  
  18. int BPM = 300;
  19. int channel;
  20. int currentchan= 0;
  21.  
  22. #define numKeys (16)
  23.  
  24. #define INTPIN A2
  25.  
  26. void setup() {
  27.   Serial.begin(9600);
  28.   pinMode(A1, INPUT);  // potentiometer for BPM control
  29.  
  30.   pinMode(INTPIN, INPUT);   // INT pin requires a pullup
  31.   digitalWrite(INTPIN, HIGH);
  32.   pinMode(12,OUTPUT);
  33.  
  34.   for(int j=2;j<7;j++){
  35.     pinMode(j, INPUT);   // Buttons
  36.     digitalWrite(j, HIGH);
  37.   }
  38.  
  39.   for(int j=7;j<12;j++){
  40.     pinMode(j, OUTPUT);   // Ouput LEDS, triggers Wav_Trigger with 5v pulse
  41.    
  42.   }
  43.  
  44.  
  45.  
  46.  
  47.  
  48.   trellis.begin(0x70);    // begin() with the addresses of each panel in order
  49.  
  50.  
  51.   // light up all the LEDs in order
  52.   for (uint8_t i=0; i<numKeys; i++) {
  53.     trellis.setLED(i);
  54.     trellis.writeDisplay();    
  55.     delay(50);
  56.   }
  57.   // then turn them off
  58.   for (uint8_t i=0; i<numKeys; i++) {
  59.     trellis.clrLED(i);
  60.     trellis.writeDisplay();    
  61.     delay(50);
  62.   }
  63.  
  64.  
  65.  
  66.  
  67. }
  68.  
  69.  
  70. void loop() {
  71.  
  72.  
  73.   for (uint8_t i=0; i<numKeys; i++) {
  74.     int BPM= analogRead(A1);
  75.     BPM =  map(BPM, 0, 1023, 400, 100);
  76.    // int BPMdisplay= (60000 /BPM);
  77.    
  78.    
  79.    
  80.     //Serial.println(BPMdisplay);
  81.  
  82.  
  83.  
  84.     for(int b=2;b<7;b++){
  85.       if (digitalRead(b) == LOW){   //change channels
  86.         channel = b - 1;
  87.  
  88.  
  89.         if ((digitalRead(2) == LOW) && (digitalRead(6)== LOW)){  //reset all paterns on each channel when these 2 buttons are held down together
  90.           for (int r=0;r<16;r++){
  91.             results[r]= "86420";
  92.             trellis.clrLED(r);
  93.             trellis.writeDisplay();
  94.           }              
  95.          
  96.           for (uint8_t i=0; i<numKeys; i++) {
  97.             trellis.setLED(i);
  98.             trellis.writeDisplay();    
  99.             delay(20);
  100.           }
  101.           // then turn them off
  102.           for (uint8_t i=0; i<numKeys; i++) {
  103.             trellis.clrLED(i);
  104.             trellis.writeDisplay();    
  105.             delay(20);
  106.           }
  107.          
  108.         }
  109.         break;
  110.       }
  111.     }
  112.    
  113.     if(currentchan != channel){   //clear current board LEDs so you can input steps on a different channel
  114.       for (uint8_t i=0; i<numKeys; i++) {
  115.        
  116.        
  117.         trellis.clrLED(i);
  118.         trellis.writeDisplay();  
  119.        
  120.       }
  121.       currentchan = channel;
  122.  
  123.       if (currentchan == 1){
  124.         for (uint8_t i=0; i<numKeys; i++) {
  125.          
  126.           if(results[i].indexOf('9') > -1){
  127.             trellis.setLED(i);
  128.             trellis.writeDisplay();
  129.           }
  130.          
  131.          
  132.         }
  133.        
  134.       }
  135.      
  136.       if (currentchan == 2){
  137.         for (uint8_t i=0; i<numKeys; i++) {
  138.          
  139.           if(results[i].indexOf('7') > -1){
  140.             trellis.setLED(i);
  141.             trellis.writeDisplay();
  142.           }
  143.          
  144.          
  145.         }
  146.        
  147.       }
  148.       if (currentchan == 3){
  149.         for (uint8_t i=0; i<numKeys; i++) {
  150.          
  151.           if(results[i].indexOf('5') > -1){
  152.             trellis.setLED(i);
  153.             trellis.writeDisplay();
  154.           }
  155.          
  156.          
  157.         }
  158.        
  159.       }
  160.      
  161.       if (currentchan == 4){
  162.         for (uint8_t i=0; i<numKeys; i++) {
  163.          
  164.           if(results[i].indexOf('3') > -1){
  165.             trellis.setLED(i);
  166.             trellis.writeDisplay();
  167.           }
  168.          
  169.          
  170.         }
  171.        
  172.       }
  173.       if (currentchan == 5){
  174.         for (uint8_t i=0; i<numKeys; i++) {
  175.          
  176.           if(results[i].indexOf('1') > -1){
  177.             trellis.setLED(i);
  178.             trellis.writeDisplay();
  179.           }
  180.          
  181.          
  182.         }
  183.        
  184.       }
  185.      
  186.     }
  187.  
  188.    
  189.     BlinkPattern(i,currentchan,BPM);  
  190.     if (trellis.readSwitches()) {  // If a button was just pressed or released...  
  191.       for (uint8_t i=0; i<numKeys; i++) {     // go through every button
  192.         if (trellis.justPressed(i)) {         // if it was pressed...
  193.  
  194.          
  195.           if (trellis.isLED(i)){ // Alternate the LED
  196.             trellis.clrLED(i);
  197.            
  198.             if (currentchan == 1){
  199.               if(results[i].indexOf('9') > -1){ //If Button is activated, array is changed
  200.                 results[i].replace('9','8');
  201.               }
  202.             }
  203.             if (currentchan == 2){
  204.               if(results[i].indexOf('7') > -1){ //If Button is activated, array is changed
  205.                 results[i].replace('7','6');
  206.               }
  207.             }
  208.            
  209.             if (currentchan == 3){
  210.               if(results[i].indexOf('5') > -1){ //If Button is activated, array is changed
  211.                 results[i].replace('5','4');
  212.               }
  213.             }
  214.             if (currentchan == 4){
  215.               if(results[i].indexOf('3') > -1){ //If Button is activated, array is changed
  216.                 results[i].replace('3','2');
  217.               }
  218.             }
  219.            
  220.             if (currentchan == 5){
  221.               if(results[i].indexOf('1') > -1){ //If Button is activated, array is changed
  222.                 results[i].replace('1','0');
  223.               }
  224.             }
  225.           }  
  226.           else{
  227.            
  228.             if (currentchan == 1){
  229.               trellis.setLED(i);              //Array value changed to default if button not activated
  230.               if(results[i].indexOf('8') > -1){
  231.                 results[i].replace('8','9');
  232.               }
  233.             }
  234.  
  235.             if (currentchan == 2){
  236.               trellis.setLED(i);              //Array value changed to default if button not activated
  237.               if(results[i].indexOf('6') > -1){
  238.                 results[i].replace('6','7');
  239.               }
  240.             }
  241.  
  242.             if (currentchan == 3){
  243.               trellis.setLED(i);              //Array value changed to default if button not activated
  244.               if(results[i].indexOf('4') > -1){
  245.                 results[i].replace('4','5');
  246.               }
  247.             }
  248.  
  249.             if (currentchan == 4){
  250.               trellis.setLED(i);              //Array value changed to default if button not activated
  251.               if(results[i].indexOf('2') > -1){
  252.                 results[i].replace('2','3');
  253.               }
  254.             }
  255.  
  256.  
  257.             if (currentchan == 5){
  258.               trellis.setLED(i);              //Array value changed to default if button not activated
  259.               if(results[i].indexOf('0') > -1){
  260.                 results[i].replace('0','1');
  261.               }
  262.             }
  263.            
  264.           }
  265.         }
  266.       }  
  267.       trellis.writeDisplay(); // tell the trellis to set the LEDs we requested
  268.     }
  269.   }
  270.  
  271. //  PrintArray();
  272. }
  273.  
  274. void BlinkPattern(uint8_t i, int channel, int BPM)
  275. {
  276.  
  277.   if (trellis.isLED(i)== 1){
  278.     trellis.clrLED(i);
  279.     trellis.writeDisplay();
  280.     int counter = Outputs(i);
  281.  delay((BPM/4)-counter);
  282.  
  283.  //   digitalWrite(pinON, LOW);
  284.  
  285.     trellis.setLED(i);
  286.     trellis.writeDisplay();
  287.    
  288.     delay((BPM *3)/4);
  289.  
  290.   }
  291.  
  292.  
  293.  
  294.   else{
  295.     int counter = Outputs(i);
  296.     trellis.setLED(i);
  297.     trellis.writeDisplay();    
  298.     delay((BPM/2)-counter);
  299.    
  300.     trellis.clrLED(i);
  301.     trellis.writeDisplay();
  302.     delay(BPM/2);
  303.     // digitalWrite(pinON, LOW);
  304.   }  
  305.  
  306.  
  307. }  
  308.  
  309. void PrintArray(){ // for testing purposes
  310.   Serial.println("Array");
  311.   for (int i =0; i< 4; i++) Serial.println(results[i]);
  312.   Serial.println(" ");
  313.   for (int i =4; i< 8; i++) Serial.println(results[i]);
  314.   Serial.println(" ");
  315.   for (int i =8; i< 12; i++) Serial.println(results[i]);
  316.   Serial.println(" ");
  317.   for (int i =12; i<16 ; i++) Serial.println(results[i]);
  318.   Serial.println(" ");
  319.  
  320. }
  321.  
  322. int Outputs(uint8_t i){  //checks array for each step and if the digit is odd, fires off a trigger pulse.
  323.   int counter = 0;
  324.   if(results[i].indexOf('9') > -1){  //Output
  325.     digitalWrite(7, HIGH);
  326.    
  327.     delay(10);
  328.  
  329.     digitalWrite(7, LOW);
  330.     counter = counter + 10;
  331.   }
  332.  
  333.   if(results[i].indexOf('7') > -1){  //Output
  334.     digitalWrite(8, HIGH);
  335.  
  336.    delay(10);
  337.      digitalWrite(8, LOW);
  338.     counter = counter + 10;
  339.   }
  340.  
  341.   if(results[i].indexOf('5') > -1){  //Output
  342.     digitalWrite(9, HIGH);
  343.  
  344.    
  345.    delay(10);
  346.     digitalWrite(9, LOW);
  347.    counter = counter + 10;
  348.  
  349.  
  350.   }
  351.   if(results[i].indexOf('3') > -1){  //Output
  352.     digitalWrite(10, HIGH);
  353.   delay(10);
  354.  
  355.      digitalWrite(10, LOW);
  356.     counter = counter + 10;
  357.   }
  358.  
  359.   if(results[i].indexOf('1') > -1){  //Output
  360.     digitalWrite(11, HIGH);
  361.   delay(10);
  362.      digitalWrite(11, LOW);
  363.    
  364.     counter = counter + 10;
  365.   }
  366.  
  367.  
  368.   return counter;
  369. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement