Stap 4: Schaal aan meerdere knoppen
De bedrading is vrij veel het zelfde als met een enkele knop. De monitorsignaalkabel aan te sluiten zal worden aangesloten rechtstreeks op de pin en alle de gnd zal worden verbonden door een interne weerstand.
Voor de code, zul je alle onze variabelen als matrices definiëren en maken een "voor" loops en controleert u de status van elke pin:
#include "Keyboard.h"const int buttonPin[] = {2, 3, 4, 5}; int pinCount = 4; int buttonState[] = {0, 0, 0, 0}; int prevButtonState[] = {HIGH, HIGH, HIGH, HIGH}; long lastDebounceTime[] = {0, 0, 0, 0}; long debounceDelay = 50; void setup() { for (int thisPin = pinCount - 1; thisPin >= 0; thisPin--) { pinMode(buttonPin[thisPin], INPUT); digitalWrite(buttonPin[thisPin], HIGH); } Keyboard.begin(); } // Output actions. Probably the only part that you need to change int outputAction(int currentButton) { if (currentButton == 1) { Keyboard.press(ctrlKey); Keyboard.press('c'); delay(100); Keyboard.releaseAll(); } if (currentButton + 1 == 2) { Keyboard.press(ctrlKey); Keyboard.press('v'); delay(100); Keyboard.releaseAll(); } } void loop() { for (int thisPin = pinCount - 1; thisPin >= 0; thisPin--) { buttonState[thisPin] = digitalRead(buttonPin[thisPin]); if ((buttonState[thisPin] != prevButtonState[thisPin]) && (buttonState[thisPin] == HIGH)) { if ((millis() - lastDebounceTime[thisPin]) > debounceDelay) { outputAction(thisPin); lastDebounceTime[thisPin] = millis(); } } prevButtonState[thisPin] = buttonState[thisPin]; } }
Voor de verbinding van de bedrading te blijven vasthouden, zelfs wanneer u nodig hebt om het te openen, kunt u overwegen tin solderen van de draden naar de knoppen. Hier vindt u gemakkelijk tutorials over dit op YouTube.