Components and supplies
Stereo Jack Socket, 6.3mm
Arduino Micro
Project description
Code
USB Foot switch
c_cpp
1#include <Keyboard.h> 2//###################################################################################### 3 4//###################################################################################### 5//User Settings: 6//Character(s) sent to USB when the "Tip" Button is pressed shortly 7char Char_Tip[] ={' '};//start/stop command for many Apps. Hydrogen, Audacity, Transcibe!, YouTube, etc. 8//Character(s) sent to USB when the "Tip" Button is pressed for at least LongPressTime ms 9char Char_Tip_Long[] = {KEY_LEFT_ALT, KEY_TAB};//Switching to previous App 10 11//Character(s) sent to USB when the "Ring" Button is pressed shortly 12const char Char_Ring[] ={','};//tapping the BPM for Hydrogen 13//Character(s) sent to USB when the "Ring" Button is pressed for at least LongPressTime ms 14const char Char_Ring_Long[] ={KEY_LEFT_ALT, KEY_TAB};//Switching to previous App 15 16int LongPressTime=700; //milliseconds 17int delayafterrelease=200;//milliseconds 18int CMode=0; //Cable mode, Mono- or Stereo cable 19//###################################################################################### 20//Cablemode 0: Autodetect 21// For Mono or Stereo cable Ring contact is always "connected" to ground (Boss FS-5U) 22// If AutoDetectStereo is 1: At Powerup for three seconds the signal of Ring_Tip 23// is evaluated. If it is LOW all the time (contact closed) a mono setup is assumed. 24// This is not hot-plugable. If cables are changed please reset by reconnecting usb. 25//Cablemode 1: Mono or Stereo Cable, One Button, Tip contact only 26//Cablemode 2: 2x mono to stereo, Two Buttons or Boss FS-6 27 28//Button Commandes: KEY_RETURN, KEY_ESC, KEY_F1.., KEY_BACKSPACE, KEY_LEFT_CTRL... 29//https://www.arduino.cc/en/Reference/KeyboardModifiers 30//https://www.electronicsweekly.com/blogs/engineer-in-wonderland/arduino-micro-direct-access-board-leds-2017-08/ 31//---------------------------------------------------------------------------------------------------------- 32const int RXLED = 17; // The RX LED has a defined Arduino Micro pin 33const int TXLED = 30; // The TX LED has a defined Arduino Micro pin 34const int Pin_Ring = 2; 35const int Pin_Tip = 7 ; 36int Stat_Ring=0; 37int Stat_Tip=0; 38int RingPressedTime=0; 39int TipPressedTime=0; 40unsigned long int presstime; 41int lenRingCmd,lenRingLongCmd; 42int lenTipCmd,lenTipLongCmd; 43//---------------------------------------------------------------------------------------------------------- 44void setup() { 45 //turn off all LED 46 pinMode(LED_BUILTIN, OUTPUT); 47 pinMode(RXLED, OUTPUT); // Set RX LED as an output 48 pinMode(TXLED, OUTPUT); // Set TX LED as an output 49 digitalWrite(LED_BUILTIN, HIGH); // turn the LED off by making the voltage LOW 50 digitalWrite(RXLED, HIGH); // set the LED off 51 digitalWrite(TXLED, HIGH); // set the LED off 52 // put your setup code here, to run once: 53 pinMode(Pin_Ring, INPUT_PULLUP); 54 pinMode(Pin_Tip, INPUT_PULLUP); 55 digitalWrite(Pin_Ring, HIGH); 56 digitalWrite(Pin_Tip, HIGH); 57 58 lenRingCmd =sizeof(Char_Ring)/sizeof(Char_Ring[0]); //number of keystrokes to be transmitted 59 lenRingLongCmd =sizeof(Char_Ring_Long)/sizeof(Char_Ring_Long[0]); 60 lenTipCmd =sizeof(Char_Tip)/sizeof(Char_Tip[0]); 61 lenTipLongCmd =sizeof(Char_Tip_Long)/sizeof(Char_Tip_Long[0]); 62 63 int locount=0; // number of events with Pin_Ring==low (contact closed) 64 int hicount=1;// number of events with Pin_Ring==high (contact open), starts with 1 to avoid zero division 65 if (CMode==0){ 66 for (int i=0; i<300; i++){ // testing for 3 seconds, counting open and close events 67 if (LOW==digitalRead(Pin_Ring)) 68 locount+=1; 69 else 70 hicount+=1; 71 delay(10); 72 } 73 if (locount/hicount < 0.01)//if highcount (open) is much higher than locount (closed) the Ring/Ground contact 74 //is not permamently closed. A stereo cable is used. Seems to be more stable than testing 75 //for a single open event. 76 CMode=2;//stereo cable 77 else 78 CMode=1;//mono cable 79 } 80 Keyboard.releaseAll();//you never know ;-) 81} 82//---------------------------------------------------------------------------------------------------------- 83void loop() { 84 // put your main code here, to run repeatedly: 85 int pressed=0; 86 Stat_Ring=0; 87 Stat_Tip=0; 88 89//might be a good idea to use interrupts some day 90 for (int i=0; i < 5; i++){ 91 delay(10); 92 if (LOW==digitalRead(Pin_Ring)){ 93 Stat_Ring=1; // This one is always 1 with a mono cable. With stereo cable 1 means button is pressed 94 } 95 if (LOW==digitalRead(Pin_Tip)){ 96 Stat_Tip=1;// 1 means button pressed with any cable 97 } 98 } 99 100//Selecting and sending the keyboard commands: 101 if (CMode==2){ //Ring contact is used. 2xmono->stereo or stereo->stereo configuration 102 if (Stat_Ring==1){ 103 presstime=millis();//remember time of pressing event (ring contact) 104 while (LOW==digitalRead(Pin_Ring)) 105 delay(10); // wait for button being released. delay could be removed 106 if (millis()-presstime > LongPressTime)//if button has been pressed longer than LongPressTime use long command 107 for (int r=0; r<lenRingLongCmd; r++)//send all keyboard commands in the list 108 Keyboard.press(Char_Ring_Long[r]); 109 else 110 for (int r=0;r<lenRingCmd;r++) //otherwise use short command 111 Keyboard.press(Char_Ring[r]); 112 pressed=1;//button has been pressed 113 } 114 } 115 116 if (Stat_Tip==1){//same as Ring Contact, for any CMode 117 presstime=millis(); 118 while (LOW==digitalRead(Pin_Tip)) 119 delay(10); 120 if (millis()-presstime > LongPressTime) 121 for (int t=0; t<lenTipLongCmd; t++) 122 Keyboard.press(Char_Tip_Long[t]); 123 else 124 for (int t=0; t<lenTipCmd; t++) 125 Keyboard.press(Char_Tip[t]); 126 pressed=1; 127 } 128 129 if (pressed==1){ 130 Keyboard.releaseAll(); 131 delay(delayafterrelease);//make sure that no button press event is detectd during button release 132 pressed=0; 133 digitalWrite(LED_BUILTIN, HIGH); // turn the LED off by making the voltage LOW 134//One could use these two LEDs for indicating CableMode 135 digitalWrite(RXLED, HIGH); // set the LED off 136 digitalWrite(TXLED, HIGH); // set the LED off 137 } 138 } 139//---------------------------------------------------------------------------------------------------------- 140//---------------------------------------------------------------------------------------------------------- 141 142 143
Comments
Only logged in users can leave comments
jetrock
0 Followers
•0 Projects
Table of contents
Intro
1
0