retrokits

Buttons on the RK-002

May 28th, 2022 (edited)
146
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.  * Button input test on the RK002
  3.  * You can use the outer pins of the orange RK002 DIN5 for output or input, in this example we scan for a button press on one of the pins and insert a sequencer start/stop command into the midi stream
  4.  *
  5.  * It is an illustration of what is possible. We have an RK-202 adapter to add buttons to the RK002 DIN5 plug which exposes it for button connections.
  6.  *
  7.  * This simple example uses it to insert sequencer start stop commands
  8.  * ...but you could also add patch changes + / - or e.g. even initiate sysex dumps on a single button press.
  9.  *
  10.  *
  11.  * This example in action with the RK-202 buttonboard: https://www.instagram.com/p/CeEXg5cNAHt/
  12.  *
  13.  * button1 (black): tempo start / stop
  14.  * button2 (red)  : tap tempo (for internal tempo generator only)
  15.  *
  16.  */
  17.  
  18. // ******************************************
  19. // NOTE: Activate GPIO functions on hardware layout
  20. // ******************************************
  21.  
  22. #define BUTTON1 13
  23. #define BUTTON2 6
  24.  
  25. // WAITTIME is xx times 10mS so 16 = 160mS debounce time, if too short/long, you can change it here
  26. #define WAITTIME 16
  27.  
  28. // heartbeattmr holds a counter for waittime
  29. byte heartbeattmr = 0;
  30.  
  31. bool buttons[2] = {false,false} ; // state variables for button lock during debouncing
  32.  
  33. bool clock_run=false;
  34.  
  35. void onButton1(){
  36.     if(buttons[0]==false){
  37.       if(clock_run){ // is running so stop
  38.         clock_run=false;
  39.         RK002_sendStop();
  40.       }else{
  41.         RK002_sendStart();
  42.         clock_run=true;
  43.       }
  44.       buttons[0]=true;
  45.       heartbeattmr=WAITTIME;
  46.     }
  47. }
  48.  
  49. void onButton2(){
  50.     if(buttons[1]==false){
  51.       RK002_clockTap(); // tap tempo
  52.       buttons[1]=true;
  53.       heartbeattmr=WAITTIME;
  54.     }
  55. }
  56.  
  57. bool RK002_onClock(){
  58.   return true;
  59. }
  60. bool RK002_onStart(){
  61.   clock_run=true;
  62.   return true;
  63. }
  64. bool RK002_onStop(){
  65.   clock_run=false;
  66.   return true;
  67. }
  68. bool RK002_onContinue(){
  69.   clock_run=!clock_run;
  70.   return true;
  71. }
  72. void setup()
  73. {
  74.  
  75.   /*        0  GPIO 6 (run)
  76.    *     0 5V
  77.    *    0 GND  [=    orange connector, pin side view
  78.    *     0 DATA
  79.    *        0  GPIO 13 (clk)
  80.    */
  81.  
  82.  
  83.   pinMode(BUTTON1,INPUT_PULLUP); // button 1 input
  84.   attachInterrupt(digitalPinToInterrupt(BUTTON1),onButton1,FALLING);
  85.  
  86.   pinMode(BUTTON2,INPUT_PULLUP); // button 2 input
  87.   attachInterrupt(digitalPinToInterrupt(BUTTON2),onButton2,FALLING);
  88.  
  89.   RK002_clockSetTempo(1280); // no external tempo? set to 128BPM
  90.   RK002_clockSetMode(0); // use external clock if available
  91. }
  92.  
  93. void RK002_onHeartBeat() // we use heartbeat for button debouncing
  94. {
  95.   // is timer running (button scanned) ?
  96.   if (heartbeattmr > 0)
  97.   {
  98.     heartbeattmr--;
  99.     if (heartbeattmr == 0)
  100.     {
  101.       //Timeout occured, enable reading of buttons again (WAITTIME x 10mS)
  102.       if(buttons[0]==true) buttons[0]=false;
  103.       if(buttons[1]==true) buttons[1]=false;
  104.     }
  105.   }
  106. }
  107.  
  108. void loop()
  109. {
  110. }
  111.  
RAW Paste Data Copied