From 86c884be4abc255c1c532ecc765d76d9666a28e2 Mon Sep 17 00:00:00 2001 From: asb Date: Thu, 15 Sep 2022 21:07:10 +1000 Subject: [PATCH] V2 --- 4ButtonKeypad/4ButtonKeypad.ino | 185 +++++++++++++++++++------------- 1 file changed, 113 insertions(+), 72 deletions(-) diff --git a/4ButtonKeypad/4ButtonKeypad.ino b/4ButtonKeypad/4ButtonKeypad.ino index 744f61e..f10c347 100644 --- a/4ButtonKeypad/4ButtonKeypad.ino +++ b/4ButtonKeypad/4ButtonKeypad.ino @@ -22,18 +22,20 @@ #define BUTTON3 A1 #define BUTTON4 A0 -const int cintLedOffTime = 60000; //LED 'OFF' time (milliseconds) is how long LED will remain on for after a button was pressed (For reference: 60,000 = 1 minute) -const int cintDebounceTime = 200; //DebounceTime (milliseconds) is used with the 'Delay' function to pause code after a button press. This helps to slow 'extra' commands from - //a Button like 'PASTING repeated data'). Increase the 'time' if wishing to 'hold' an action from happening (or repeating) for longer. - //This is currently only used when 'blnDelayActions' becomes 'true'. If it is 'false', then the delay is 'zero'. +const int cintLedOffTime = 1000; //LED 'OFF' time (milliseconds) is how long LED will remain on for after a button was pressed (For reference: 60,000 = 1 minute) +const int cintDebounceTime = 200; //DebounceTime (milliseconds) is used with the 'Delay' function to pause code after a button press. This helps to slow 'extra' commands from + //a Button like 'PASTING repeated data'). Increase the 'time' if wishing to 'hold' an action from happening (or repeating) for longer. + //This is currently only used when 'blnDelayActions' becomes 'true'. If it is 'false', then the delay is 'zero'. unsigned long ledTickTime = 0; unsigned long lastPressTime = 0; uint8_t ledBrightness = 100; -bool ledOn = false; -bool blnDelayActions = false; //Used to SLOW 'button actions' from repeating (ie. it is a simple debounce to retard 'extra' or 'unwanted' actions from Buttons). -bool blnReleaseALLKeys = false; //Used to control when 'ALL Keyboard Keys should be released'.... OR NOT, where (in some cases) 'Multi-button combinations are required' (eg. 'Hold-Key' actions like Ctr+Alt+Del). - //If 'blnReleaseALLKeys' is set to false (in the running code), then you must also implicitly state what keys should be released (and when). +bool ledOn = true; +bool blnDelayActions = false; //Used to SLOW 'button actions' from repeating (ie. it is a simple debounce to retard 'extra' or 'unwanted' actions from Buttons). +bool blnReleaseALLKeys = false; //Used to control when 'ALL Keyboard Keys should be released'.... OR NOT, where (in some cases) 'Multi-button combinations are required' (eg. 'Hold-Key' actions like Ctr+Alt+Del). + //If 'blnReleaseALLKeys' is set to false (in the running code), then you must also implicitly state what keys should be released (and when). +bool blnButtonActivty = false; //Used to flag when a Button action has actually occured. This is used primarily so it can correctly turn off LEDs after the specified activity time (ie. indirectly works with 'cintLedOffTime') + //Using the EasyButton library we create 4 buttons, with each button being connected to a GPIO pin as defined above, and having a 35ms debounce time. From memory, these do not have internal pullup //resistors, and are not inverted. Hence the two falses. @@ -92,15 +94,17 @@ void loop() { // PASTE COMMAND (Ctrl+V) Keyboard.press(KEY_LEFT_CTRL); Keyboard.write('v'); - lastPressTime = millis(); blnDelayActions = true; + blnButtonActivty = true; + //lastPressTime = millis(); } else if(button1.wasReleased()) { - lastPressTime = millis(); + //lastPressTime = millis(); blnDelayActions = false; blnReleaseALLKeys = true; + blnButtonActivty = true; } if(button2.isPressed()) @@ -108,115 +112,152 @@ void loop() { // COPY COMMAND (Ctrl+C) Keyboard.press(KEY_LEFT_CTRL); Keyboard.write('c'); - lastPressTime = millis(); blnDelayActions = false; + blnButtonActivty = true; + //lastPressTime = millis(); } else if(button2.wasReleased()) { - lastPressTime = millis(); + //lastPressTime = millis(); blnDelayActions = false; blnReleaseALLKeys = true; + blnButtonActivty = true; } if(button3.isPressed()) { //CUSTOM TEXT (Message) - Keyboard.println("Hi there"); - Keyboard.println(); - Keyboard.println("Just confirming that your item is in the mail... See pics."); - Keyboard.println(); - Keyboard.println("Tracking is available via Australia Post: TMP40083004070052454xxxxx"); - Keyboard.println("Tracking progress applies on business days as it processes through Australia Post facilities.."); - Keyboard.println(); - Keyboard.print("Thank-you and All the best ;)"); - lastPressTime = millis(); + //Keyboard.println("Hi"); + //Keyboard.println(); + Keyboard.println("Just confirming that your item is in the mail (and may have already been delivered)... See pics."); + //Keyboard.println(); + //Keyboard.println("Tracking is available via Australia Post: TMP40083004070052454xxxxx"); + //Keyboard.println("Tracking progress applies on business days as it processes through Australia Post facilities.."); + //Keyboard.println(); + //Keyboard.print("Thank-you and All the best ;)"); + + //lastPressTime = millis(); blnDelayActions = true; + blnButtonActivty = true; } else if(button3.wasReleased()) { - lastPressTime = millis(); + //lastPressTime = millis(); blnDelayActions = false; blnReleaseALLKeys = true; + blnButtonActivty = true; } if(button4.isPressed()) { - // 'CTRL' KEY (Only) - Keyboard.press(KEY_LEFT_CTRL); - lastPressTime = millis(); + // 'SHIFT' KEY (Only) + Keyboard.press(KEY_LEFT_SHIFT); blnDelayActions = false; + //lastPressTime = millis(); + blnButtonActivty = true; } else if(button4.wasReleased()) { - lastPressTime = millis(); + //lastPressTime = millis(); blnDelayActions = false; blnReleaseALLKeys = false; //When FALSE - you must implicidly release Keys (below) - Keyboard.release(KEY_LEFT_CTRL); + Keyboard.release(KEY_LEFT_SHIFT); + blnButtonActivty = true; } - // Check if a 'Pause' is required while performing 'Button' action(s): This stops (or rather 'SLOWS') 'repeated button actions' if a button is 'held down too long' (eg. helps stop extra 'PASTE data' commands) - switch (blnDelayActions) + // Check if a 'Button action' has recently occurred. + switch (blnButtonActivty) { case true: - // Pause - delay(cintDebounceTime); - blnDelayActions = false; + // A 'Button Activty' HAS occurred. + + //Reset the 'lastPressTime' timer variable to current time(r). + lastPressTime = millis(); + + // Check if a 'Pause' is required while performing 'Button' action(s): This stops (or rather 'SLOWS') 'repeated button actions' if a button is 'held down too long' (eg. helps stop extra 'PASTE data' commands) + switch (blnDelayActions) + { + case true: + // Pause + delay(cintDebounceTime); + blnDelayActions = false; + break; + default: + // No Delay, or maybe repeat action(s) + delay(0); + break; + } + + // Check if ALL Keyboard Keys should now be released (Usually 'true', but perhaps 'false' if 'multi-button hold combinations' are required) + switch (blnReleaseALLKeys) + { + case true: + // Release ALL Keyboard Keys (this should almost always be the case). Then reset 'ReleaseAllKeys' flag. + Keyboard.releaseAll(); + blnReleaseALLKeys = false; + break; + default: + // Do NOT Release Keys (IMPORTANT: If here not by default, then you should have already implicidly set what Keys have been (or are to be) released.) + break; + } + + //Reset the Button Activity Flag + blnButtonActivty = false; + break; - default: - // No Delay, or maybe repeat action(s) - delay(0); + default: + // A 'Button Activty' HAS NOT Occurred. No need to do anything - This will also retain the 'lastPressTime' time value. break; } - // Check if ALL Keyboard Keys should now be released (Usually 'true', but perhaps 'false' if 'multi-button hold combinations' are required) - switch (blnReleaseALLKeys) + //If LED is ON.... Check timer since last Button Activity. + switch (ledOn) { case true: - // Release ALL Keyboard Keys (this should almost always be the case) - Keyboard.releaseAll(); - blnReleaseALLKeys = false; + //If Max time has elapsed, then turn off LEDS + if((millis() - lastPressTime) > cintLedOffTime) + { + ledOn = false; + //lastPressTime = 0; + } + else + { + ledOn = true; + } + + //If certain time has elapsed, then adjust LEDs brightness + if ((millis() - ledTickTime) > 10) + { + ledTickTime = millis(); + if(!ledOn && (ledBrightness > 0)) + { + ledBrightness=ledBrightness-1; + } + + else if(ledOn && (ledBrightness < 100)) + { + ledBrightness=ledBrightness+10; + } + + analogWrite(LED1, ledBrightness); + analogWrite(LED2, ledBrightness); + analogWrite(LED3, ledBrightness); + analogWrite(LED4, ledBrightness); + } + //} break; - default: - // Do NOT Release Keys (IMPORTANT: If here not by default, then you should have already implicidly set what Keys have been (or are to be) released.) + default: + // LEDs are Off.... Do nothing break; } - if((millis() - lastPressTime) > cintLedOffTime) - { - ledOn = false; - } - else - { - ledOn = true; - } - - - if ((millis() - ledTickTime) > 10) - { - ledTickTime = millis(); - if(!ledOn && (ledBrightness > 0)) - { - ledBrightness=ledBrightness-1; - } - - else if(ledOn && (ledBrightness < 100)) - { - ledBrightness=ledBrightness+10; - } - - analogWrite(LED1, ledBrightness); - analogWrite(LED2, ledBrightness); - analogWrite(LED3, ledBrightness); - analogWrite(LED4, ledBrightness); - } - // Serial.print("Led Brightness = "); // Serial.print(ledBrightness); // Serial.print("\t Led State = "); // Serial.println(ledOn); -} \ No newline at end of file +}