retrokits

RK002 ARP w GPIO Button activation

Jun 16th, 2022 (edited)
1,053
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* DUY Code: ARP Arpeggio example */
  2.  
  3. /*
  4.  * Example script of RK002 arpeggiator.
  5.  * ENABLE GPIO ON CONFIG ---->
  6.  *
  7.  * PARAMETERS (accessible via DUY Portal or SysEx without compiling)
  8.  * MIDICHN    : working input MIDI channel  (1..16)
  9.  * MODE       : 0=off, 1=UP, 2=DOWN, 3=ALTERNATE, 4=RANDOM, 5=AS_PLAYED
  10.  * OCTAVE_MIN : range min 0-4
  11.  * OCTAVE_MAX : range max 0-4
  12.  * CLOCKLENGTH: ARP clock length 1-48
  13.  * GATE       : gate length: 1=1/4, 2=2/4, 3=3/4, 4=1, 5=5/4 etc.
  14.  * TRANSPOSE  : ARP transpose 0-36
  15.  */
  16.  
  17. #define BUTTON1 13 // black button
  18. #define BUTTON2 6  // red button
  19. #define WAITTIME 16
  20. byte heartbeattmr = 0;
  21. bool buttons[2] = {false,false} ; // state variables for button lock during debouncing
  22.  
  23. RK002_DECLARE_PARAM(MIDICHN,1,1,16,1);
  24. RK002_DECLARE_PARAM(MODE,0,0,5,1);
  25. RK002_DECLARE_PARAM(OCTAVE_MIN,0,-4,0,0);
  26. RK002_DECLARE_PARAM(OCTAVE_MAX,0,0,4,0);
  27. RK002_DECLARE_PARAM(CLOCKLENGTH,0,1,48,2);
  28. RK002_DECLARE_PARAM(GATELENGTH,0,1,16,1);
  29. RK002_DECLARE_PARAM(GATE,0,1,16,2);
  30. RK002_DECLARE_PARAM(TRANSPOSE,0,0,36,0);
  31.  
  32. RKArp arp;
  33. byte midichn = 0;
  34. byte octmax=1;
  35. byte octmin=1;
  36. byte clockdiv=1;
  37. byte gatelen=1;
  38. byte arpmode=1;
  39. byte holdmode=0;
  40.  
  41. void updateParams()
  42. {
  43.   midichn = RK002_paramGet(MIDICHN) - 1;
  44.   arp.setMode(RK002_paramGet(MODE));
  45.   /*
  46.     ARPMODE_OFF,
  47.     ARPMODE_UP,
  48.     ARPMODE_DOWN,
  49.     ARPMODE_ALTERNATE,
  50.     ARPMODE_RANDOM,
  51.     ARPMODE_AS_PLAYED,
  52.   */
  53.   arp.setOctaveMin(RK002_paramGet(OCTAVE_MIN));
  54.   arp.setOctaveMax(RK002_paramGet(OCTAVE_MAX));
  55.   arp.setClockLength(RK002_paramGet(CLOCKLENGTH));
  56.   arp.setGateCode(RK002_paramGet(GATELENGTH));
  57.   arp.setTranspose(RK002_paramGet(TRANSPOSE));
  58. }
  59.  
  60. void RK002_onParamChange(unsigned param_nr, int param_val)
  61. {
  62.   updateParams();
  63. }
  64.  
  65. bool RK002_onNoteOff(byte chn, byte note, byte velocity)
  66. {
  67.   bool thru = true;
  68.   if (chn == midichn)
  69.   {
  70.     arp.inputNote(note,0); // velo 0 = note off
  71.     thru=false;
  72.   }
  73.   return thru;
  74. }
  75.  
  76. bool RK002_onNoteOn(byte chn, byte note, byte velocity)
  77. {
  78.   bool thru = true;
  79.   if (chn == midichn)
  80.   {
  81.     if(buttons[1]==true){
  82.       thru=false;
  83.       byte cmd=note % 12;
  84.       if(cmd==0){
  85.         octmin++;
  86.         octmin=octmin%4;
  87.         arp.setOctaveMin(-1*octmin);
  88.         RK002_paramSet(OCTAVE_MIN,-1*octmin);
  89.       }
  90.       if(cmd==1){
  91.         octmax++;
  92.         octmax=octmax%4;
  93.         arp.setOctaveMax(octmax);
  94.         RK002_paramSet(OCTAVE_MAX,octmax);
  95.       }
  96.       if(cmd==2){
  97.         clockdiv++;
  98.         clockdiv=clockdiv%5;
  99.         clockdiv++;
  100.         arp.setClockLength(clockdiv);
  101.         RK002_paramSet(CLOCKLENGTH,clockdiv);
  102.       }
  103.       if(cmd==3){
  104.         arpmode++;
  105.         arpmode=arpmode%5;
  106.         arpmode++;
  107.         arp.setMode(arpmode);
  108.         RK002_paramSet(MODE,arpmode);
  109.       }
  110.       if(cmd==4){
  111.         gatelen++;
  112.         gatelen=gatelen%15;
  113.         gatelen++;
  114.         arp.setGateCode(gatelen);
  115.         RK002_paramSet(GATELENGTH,gatelen);
  116.       }
  117.       if(cmd==5){
  118.         holdmode++;
  119.         holdmode=holdmode%2;
  120.         arp.setHold(holdmode==1);
  121.       }
  122.     }else{
  123.         arp.inputNote(note,velocity);
  124.         thru=false;
  125.     }
  126.   }
  127.   return thru;
  128. }
  129.  
  130.  
  131. bool RK002_onClock()
  132. {
  133.   arp.inputClock();
  134.   return true;
  135. }
  136.  
  137. void onArpOutput(void *userarg, byte key, byte vel)
  138. {
  139.   if(!vel){
  140.     RK002_sendNoteOff(midichn,key,100);
  141.   }else{
  142.     RK002_sendNoteOn(midichn,key,vel);
  143.   }
  144. }
  145.  
  146. void RK002_onHeartBeat() // we use heartbeat for button scanning with debouncing
  147. {
  148.   // is timer running (button scanned) ?
  149.   if (heartbeattmr > 0)
  150.   {
  151.     heartbeattmr--;
  152.     if (heartbeattmr == 0)
  153.       {
  154.         if(!digitalRead(BUTTON1)){
  155.           arp.setMode(0);
  156.           buttons[0]=true;
  157.         }else{
  158.           buttons[0]=false;
  159.         }
  160.         if(!digitalRead(BUTTON2)){
  161.           arp.setMode(RK002_paramGet(MODE));
  162.           buttons[1]=true;
  163.         }else{
  164.           buttons[1]=false;
  165.         }
  166.         heartbeattmr=WAITTIME;
  167.       }
  168.            //end hbtimertimeout
  169.     }
  170.            // end hbtimer
  171. }
  172.  
  173. void setup()
  174. {
  175.   RK002_clockSetMode(0);// 0=auto clock, 1=only internal, 2=only external
  176.   updateParams();
  177.  
  178.   pinMode(BUTTON1,INPUT_PULLUP); // button 1 black input
  179.   pinMode(BUTTON2,INPUT_PULLUP); // button 2 red input
  180.   heartbeattmr=WAITTIME;
  181.   arp.setOutputHandler(onArpOutput,0);
  182. }
  183.  
  184. void loop()
  185. {
  186. }
Add Comment
Please, Sign In to add comment