Advertisement
craighissett

Footswitch Sketch

Aug 15th, 2014
506
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.85 KB | None | 0 0
  1. /*
  2. Keyboard Button test
  3. For the Arduino Leonardo and Micro.
  4. Sends a text string when a button is pressed.
  5.  
  6. The circuit:
  7. * 10-kiloOhm resistor attached from digital pins to ground
  8.  
  9. 6-way selector - use series of resistors to give different values to analog pin.
  10. A - Playback functions: Play/Pause/Stop, ff, rw, Beginning, End
  11. B - Audio Functions: Vol+, Vol-,
  12. C -
  13. D - Recording Functions: Arm record,
  14. E - Interface functions: Open Software, change presets etc
  15. F - PC Functions: Lock,Log in, Log out, Open Recording Software, Shutdown
  16.  
  17. Upon button press being detected the code should run the void based on the current selector switch setting.
  18.  
  19. */
  20.  
  21. //Create Array to store void names for each selector/button combo
  22. String ButtonMap[6][6]={
  23. {"1", "PlayPause", "FF", "RW", "VolUp", "VolDwn"},
  24. {"2", "NA", "NA", "NA", "NA", "NA"},
  25. {"3", "NA", "NA", "NA", "NA", "NA"},
  26. {"4", "NA", "NA", "NA", "NA", "NA"},
  27. {"5", "NA", "NA", "NA", "NA", "NA"},
  28. {"6", "LockPC", "NA", "NA", "NA", "NA"}};
  29.  
  30. //const int ButtonCount = 5;
  31. int ButtonPins[]={4, 5, 6, 7, 8};
  32. const int ButtonCount = sizeof(ButtonPins)/sizeof(int);
  33. int SelectorPin = A3;
  34.  
  35. int PreviousState[ButtonCount];
  36.  
  37. //define variables to hold Selecotr switch position, button pressed and the associated function to Run
  38. int SelectorPos;
  39. int CurrentPos;
  40. int ButtonPressed;
  41. String ToRun;
  42.  
  43. void setup() {
  44. // open the serial port and keyboard control:
  45. Serial.begin(9600);
  46. Keyboard.begin();
  47.  
  48. //Step through ButtonPin Array, assign each as INPUT with a PreviousState of LOW
  49. for (int ThisPin = 0; ThisPin < ButtonCount; ThisPin++)
  50. {pinMode(ButtonPins[ThisPin], INPUT);
  51. PreviousState[ThisPin] = LOW;}
  52.  
  53. CurrentPos=0;
  54.  
  55. MenuFlash();
  56. delay(3000);
  57. //SelectorScan();
  58.  
  59. }
  60.  
  61. void loop() {
  62. SelectorScan();
  63. ButtonScan();
  64. //delay(10);
  65. }
  66.  
  67. void SelectorScan()
  68. {/* 5v to GND with a 5 resistor ladder in between (2k resistor?). Each pin of selecotr connected at
  69. points (1 before first resistor then every intersection between each resistor.
  70. Use analog pin read to get value, and map to 1-6; SelectorPos will equal this output.*/
  71.  
  72. int Reading = analogRead(SelectorPin);
  73.  
  74.  
  75. if (Reading >= 301 && Reading <= 450)
  76. {SelectorPos = 6;}
  77. else if (Reading >= 0 && Reading <= 100)
  78. {SelectorPos = 5;}
  79. else if (Reading >= 101 && Reading <= 100)
  80. {SelectorPos = 4;}
  81. else if (Reading >= 900 && Reading <= 1050)
  82. {SelectorPos = 3;}
  83. else if (Reading >= 601 && Reading <= 750)
  84. {SelectorPos = 2;}
  85. else if (Reading >= 451 && Reading <= 600)
  86. {SelectorPos = 1;}
  87.  
  88.  
  89. //Check for change - if so refresh the screen menu
  90. if (SelectorPos != CurrentPos)
  91. {MenuFlash();}
  92.  
  93. //Then set current value
  94. CurrentPos = SelectorPos;
  95. }
  96.  
  97. void ButtonScan(){
  98. /*Use a For Loop to go through the button pin array checking all states - any changes then check SelectSwitch() and somehow
  99. fire the void associated in the ButtonMap array.
  100. In theory the array should be accessed such as ButtonMap[ButtonPressed, 1-5]][SelectorSwitchReturn, 1-6] */
  101. int State[ButtonCount];
  102.  
  103. for (int ThisPin = 0; ThisPin < ButtonCount; ThisPin++) {
  104. //Stick in the loop to read and compare each pin. Then fire a void to calculate and fire the correct void, maybe?
  105. State[ThisPin] = digitalRead(ButtonPins[ThisPin]);
  106. if ((State[ThisPin] != PreviousState[ThisPin]) && (State[ThisPin] == HIGH))
  107. { ButtonPressed = ThisPin +1;
  108. ToRun = (ButtonMap[SelectorPos-1][ButtonPressed]);
  109. ProcessPress();
  110. PreviousState[ThisPin]=State[ThisPin];
  111. }
  112. }
  113. }
  114. void ProcessPress()
  115. { /*Takes the values from ReadSelector void (SelectorPos, -1) and ReadButtons (ButtonPressed) and finds what void should be triggered from the array
  116. EG */
  117.  
  118. if (ToRun == "PlayPause")
  119. //{Keyboard.println("Play/Pause keypress triggered.");}
  120. {Serial.println(analogRead(SelectorPin));
  121. Serial.println("Play/Pause keypress triggered.");}
  122.  
  123. else if (ToRun == "FF")
  124. //{Keyboard.println("Fast forward keypress triggered.");}
  125. {Serial.println(analogRead(SelectorPin));
  126. Serial.println("FF keypress triggered.");}
  127.  
  128. else if (ToRun =="RW")
  129. //{Keyboard.println("Rewind keypress triggered.");}
  130. {Serial.println(analogRead(SelectorPin));
  131. Serial.println("RW keypress triggered.");}
  132.  
  133. else if (ToRun == "VolUp")
  134. {Serial.println(analogRead(SelectorPin));
  135. Serial.println("Volume Up triggered.");}
  136.  
  137. else if (ToRun == "VolDwn")
  138. {Serial.println(analogRead(SelectorPin));
  139. Serial.println("Volume down triggered.");}
  140.  
  141. else if (ToRun == "LockPC")
  142. {Serial.println("Logging out...");
  143. delay(2000);
  144.  
  145. Keyboard.press(131);
  146. Keyboard.print("l");
  147. Keyboard.releaseAll();}
  148. else if (ToRun == "NA")
  149. {Serial.println("NA - No function assigned");}
  150.  
  151. else
  152. {Serial.println(ToRun && "- no such function defined");}
  153.  
  154. }
  155.  
  156. void MenuFlash()
  157. {Serial.println("Screen update to follow...");
  158.  
  159. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement