Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- This code contains the follow functions:
- - void setup(): Sets pins 4, 5, 6, 7 to input with pull-up resistors enabled and begins Keyboard functionality
- - void loop(): Main loop - reads pin voltages and sends out corresponding keystrokes via USB
- */
- /*
- Pinout:
- - "HIGH" voltage button contacts - pins 4, 5, 6, 7, 8
- - "GND" voltage button contacts - GND pin
- */
- /*
- August 17, 2018
- This is an example of one way to de-bounce buttons in code.
- This sketch doesn't really do anything, it only shows how to de-bounce buttons in code.
- */
- #include <Keyboard.h>
- int upleftStatus = 1;
- int upleftStatusPrev = 1;
- int downleftStatus = 1;
- int downleftStatusPrev = 1;
- int centerStatus = 1;
- int centerStatusPrev = 1;
- int uprightStatus = 1;
- int uprightStatusPrev = 1;
- int downrightStatus = 1;
- int downrightStatusPrev = 1;
- int buttonPin4 = 4; // These are defining the pins for the button connections.
- int buttonPin5 = 5;
- int buttonPin6 = 6;
- int buttonPin7 = 7;
- int buttonPin8 = 8;
- // All five buttons share the variables below.
- bool buttonPressed = false; // This is the flag variable that must be false for button input to be accepted.
- // When a button is pushed, the above value is set to [true] and a start time [ millis() ] is saved in button_press_start_time.
- unsigned long button_press_start_time = 0; // This stores the system time that the button was pushed.
- unsigned long button_press_current_time = 0; // This stores the current time, to compare to the value above.
- int button_debounce_time = 60; // This is the length of time (in milliseconds) to de-bounce the button inputs.
- // @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
- // Function prototypes:
- // These aren't really needed in this instance (with a one-file sketch in the Arduino IDE) but some compilers do require them.
- void buttonPressDelay();
- void doButton4stuff();
- void doButton5stuff();
- void doButton6stuff();
- void doButton7stuff();
- void doButton8stuff();
- // @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
- void setup() {
- // All that is done here is setting the input pin states.
- pinMode(buttonPin4, INPUT_PULLUP);
- pinMode(buttonPin5, INPUT_PULLUP);
- pinMode(buttonPin6, INPUT_PULLUP);
- pinMode(buttonPin7, INPUT_PULLUP);
- pinMode(buttonPin8, INPUT_PULLUP);
- Keyboard.begin();
- }
- // @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
- void loop() { // begin of main sketch loop
- /*
- Whatever other code you want can go here, outside of the button-handling code.
- */
- upleftStatus = digitalRead(4);
- downleftStatus = digitalRead(5);
- centerStatus = digitalRead(6);
- uprightStatus = digitalRead(7);
- downrightStatus = digitalRead(8);
- //UPLEFT ARROW PRESSED
- if (upleftStatus != upleftStatusPrev && upleftStatus == LOW)
- {
- Keyboard.press('r');
- upleftStatusPrev = upleftStatus;
- }
- //UPLEFT ARROW RELEASED
- if (upleftStatus != upleftStatusPrev && upleftStatus == HIGH)
- {
- Keyboard.release('r');
- upleftStatusPrev = upleftStatus;
- }
- //DOWNLEFT ARROW PRESSED
- if (downleftStatus != downleftStatusPrev && downleftStatus == LOW)
- {
- Keyboard.press('v');
- downleftStatusPrev = downleftStatus;
- }
- //DOWNLEFT ARROW RELEASED
- if (downleftStatus != downleftStatusPrev && downleftStatus == HIGH)
- {
- Keyboard.release('v');
- downleftStatusPrev = downleftStatus;
- }
- //CENTER ARROW PRESSED
- if (centerStatus != centerStatusPrev && centerStatus == LOW)
- {
- Keyboard.press('g');
- centerStatusPrev = centerStatus;
- }
- //CENTER ARROW RELEASED
- if (centerStatus != centerStatusPrev && centerStatus == HIGH)
- {
- Keyboard.release('g');
- centerStatusPrev = centerStatus;
- }
- //UPRIGHT ARROW PRESSED
- if (uprightStatus != uprightStatusPrev && uprightStatus == LOW)
- {
- Keyboard.press('y');
- uprightStatusPrev = uprightStatus;
- }
- //UPRIGHT ARROW RELEASED
- if (uprightStatus != uprightStatusPrev && uprightStatus == HIGH)
- {
- Keyboard.release('y');
- uprightStatusPrev = uprightStatus;
- }
- //DOWNRIGHT ARROW PRESSED
- if (downrightStatus != downrightStatusPrev && downrightStatus == LOW)
- {
- Keyboard.press('n');
- downrightStatusPrev = downrightStatus;
- }
- //DOWNRIGHT ARROW RELEASED
- if (downrightStatus != downrightStatusPrev && downrightStatus == HIGH)
- {
- Keyboard.release('n');
- downrightStatusPrev = downrightStatus;
- }
- if (buttonPressed == false) {
- // The 'if' conditional statement above only allows checking for button presses if the boolean indicator is set to [false].
- // The boolean indicator will only be false if there have been no button presses since the button_delay_time has elapsed from the previous button press.
- // Below is reading the #4 button pin.
- // Since the pins were set to input_pullup, these should be checked for the low state.
- if (digitalRead(buttonPin4) == LOW) {
- buttonPressDelay(); // The buttonPressDelay() function disables further button input, so that is called first.
- doButton4stuff(); // Whatever this button press does, is in this function.
- }
- // Below is reading the #5 button pin.
- if (digitalRead(buttonPin5) == LOW) {
- buttonPressDelay(); // The buttonPressDelay() function disables further button input, so that is called first.
- doButton5stuff(); // Whatever this button press does, is in this function.
- }
- // Below is reading the #6 button pin.
- if (digitalRead(buttonPin6) == LOW) {
- buttonPressDelay(); // The buttonPressDelay() function disables further button input, so that is called first.
- doButton6stuff(); // Whatever this button press does, is in this function.
- }
- // Below is reading the #7 button pin.
- if (digitalRead(buttonPin7) == LOW) {
- buttonPressDelay(); // The buttonPressDelay() function disables further button input, so that is called first.
- doButton7stuff(); // Whatever this button press does, is in this function.
- }
- // Below is reading the #8 button pin.
- if (digitalRead(buttonPin8) == LOW) {
- buttonPressDelay(); // The buttonPressDelay() function disables further button input, so that is called first.
- doButton8stuff(); // Whatever this button press does, is in this function.
- }
- } // end of ---> if (buttonPressed == false)
- else { // ---> if (buttonPressed == true)
- // The code below re-sets the buttonPressed variable to false, but only after the amount of milliseconds in {button_delay_time} have passed.
- // Other parts of the sketch can run during this wait time, but no more button inputs will be accepted until this time period expires.
- button_press_current_time = millis();
- if (button_press_current_time >= button_press_start_time) {
- if (button_press_current_time >= (button_press_start_time + button_debounce_time)) {
- buttonPressed = false;
- }
- }
- else { // if (button_press_current_time < button_press_start_time)
- // This test checks for the condition that happens when the system timer has rolled over.
- button_press_start_time = millis();
- }
- }
- /*
- Whatever other code you want can go here, outside of the button-handling code.
- */
- } // This is the end of the main sketch loop.
- // @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
- // The function below is what gets called whenever any button is pushed. The delay time is the same for all buttons.
- void buttonPressDelay() {
- button_press_start_time = millis();
- buttonPressed = true;
- }
- // @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
- void doButton4stuff() {
- //UPLEFT ARROW PRESSED
- if (upleftStatus != upleftStatusPrev && upleftStatus == LOW)
- {
- Keyboard.press('r');
- upleftStatusPrev = upleftStatus;
- }
- //UPLEFT ARROW RELEASED
- if (upleftStatus != upleftStatusPrev && upleftStatus == HIGH)
- {
- Keyboard.release('r');
- upleftStatusPrev = upleftStatus;
- }// Whatever button 4 should do, goes in here
- }
- // @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
- void doButton5stuff() {
- //DOWNLEFT ARROW PRESSED
- if (downleftStatus != downleftStatusPrev && downleftStatus == LOW)
- {
- Keyboard.press('v');
- downleftStatusPrev = downleftStatus;
- }
- //DOWNLEFT ARROW RELEASED
- if (downleftStatus != downleftStatusPrev && downleftStatus == HIGH)
- {
- Keyboard.release('v');
- downleftStatusPrev = downleftStatus;
- }
- // Whatever button 5 should do, goes in here
- }
- // @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
- void doButton6stuff() {
- //CENTER ARROW PRESSED
- if (centerStatus != centerStatusPrev && centerStatus == LOW)
- {
- Keyboard.press('g');
- centerStatusPrev = centerStatus;
- }
- //CENTER ARROW RELEASED
- if (centerStatus != centerStatusPrev && centerStatus == HIGH)
- {
- Keyboard.release('g');
- centerStatusPrev = centerStatus;
- }
- // Whatever button 6 should do, goes in here
- }
- //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
- void doButton7stuff() {
- //UPRIGHT ARROW PRESSED
- if (uprightStatus != uprightStatusPrev && uprightStatus == LOW)
- {
- Keyboard.press('y');
- uprightStatusPrev = uprightStatus;
- }
- //UPRIGHT ARROW RELEASED
- if (uprightStatus != uprightStatusPrev && uprightStatus == HIGH)
- {
- Keyboard.release('y');
- uprightStatusPrev = uprightStatus;
- }
- // Whatever button 7 should do, goes in here
- }
- // @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
- void doButton8stuff() {
- //DOWNRIGHT ARROW PRESSED
- if (downrightStatus != downrightStatusPrev && downrightStatus == LOW)
- {
- Keyboard.press('n');
- downrightStatusPrev = downrightStatus;
- }
- //DOWNRIGHT ARROW RELEASED
- if (downrightStatus != downrightStatusPrev && downrightStatus == HIGH)
- {
- Keyboard.release('n');
- downrightStatusPrev = downrightStatus;
- }
- // Whatever button 8 should do, goes in here
- }
- // ~~~~~~~~ the end ~~~~~~~~~~
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement