This commit is contained in:
asb 2022-09-15 21:07:10 +10:00 committed by Zach S-B
parent c93a221401
commit 86c884be4a

View File

@ -22,7 +22,7 @@
#define BUTTON3 A1 #define BUTTON3 A1
#define BUTTON4 A0 #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 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 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. //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'. //This is currently only used when 'blnDelayActions' becomes 'true'. If it is 'false', then the delay is 'zero'.
@ -30,10 +30,12 @@ const int cintDebounceTime = 200; //DebounceTime (milliseconds) is used with t
unsigned long ledTickTime = 0; unsigned long ledTickTime = 0;
unsigned long lastPressTime = 0; unsigned long lastPressTime = 0;
uint8_t ledBrightness = 100; uint8_t ledBrightness = 100;
bool ledOn = false; 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 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). 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). //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 //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. //resistors, and are not inverted. Hence the two falses.
@ -92,15 +94,17 @@ void loop() {
// PASTE COMMAND (Ctrl+V) // PASTE COMMAND (Ctrl+V)
Keyboard.press(KEY_LEFT_CTRL); Keyboard.press(KEY_LEFT_CTRL);
Keyboard.write('v'); Keyboard.write('v');
lastPressTime = millis();
blnDelayActions = true; blnDelayActions = true;
blnButtonActivty = true;
//lastPressTime = millis();
} }
else if(button1.wasReleased()) else if(button1.wasReleased())
{ {
lastPressTime = millis(); //lastPressTime = millis();
blnDelayActions = false; blnDelayActions = false;
blnReleaseALLKeys = true; blnReleaseALLKeys = true;
blnButtonActivty = true;
} }
if(button2.isPressed()) if(button2.isPressed())
@ -108,56 +112,72 @@ void loop() {
// COPY COMMAND (Ctrl+C) // COPY COMMAND (Ctrl+C)
Keyboard.press(KEY_LEFT_CTRL); Keyboard.press(KEY_LEFT_CTRL);
Keyboard.write('c'); Keyboard.write('c');
lastPressTime = millis();
blnDelayActions = false; blnDelayActions = false;
blnButtonActivty = true;
//lastPressTime = millis();
} }
else if(button2.wasReleased()) else if(button2.wasReleased())
{ {
lastPressTime = millis(); //lastPressTime = millis();
blnDelayActions = false; blnDelayActions = false;
blnReleaseALLKeys = true; blnReleaseALLKeys = true;
blnButtonActivty = true;
} }
if(button3.isPressed()) if(button3.isPressed())
{ {
//CUSTOM TEXT (Message) //CUSTOM TEXT (Message)
Keyboard.println("Hi there"); //Keyboard.println("Hi");
Keyboard.println(); //Keyboard.println();
Keyboard.println("Just confirming that your item is in the mail... See pics."); Keyboard.println("Just confirming that your item is in the mail (and may have already been delivered)... See pics.");
Keyboard.println(); //Keyboard.println();
Keyboard.println("Tracking is available via Australia Post: TMP40083004070052454xxxxx"); //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("Tracking progress applies on business days as it processes through Australia Post facilities..");
Keyboard.println(); //Keyboard.println();
Keyboard.print("Thank-you and All the best ;)"); //Keyboard.print("Thank-you and All the best ;)");
lastPressTime = millis();
//lastPressTime = millis();
blnDelayActions = true; blnDelayActions = true;
blnButtonActivty = true;
} }
else if(button3.wasReleased()) else if(button3.wasReleased())
{ {
lastPressTime = millis(); //lastPressTime = millis();
blnDelayActions = false; blnDelayActions = false;
blnReleaseALLKeys = true; blnReleaseALLKeys = true;
blnButtonActivty = true;
} }
if(button4.isPressed()) if(button4.isPressed())
{ {
// 'CTRL' KEY (Only) // 'SHIFT' KEY (Only)
Keyboard.press(KEY_LEFT_CTRL); Keyboard.press(KEY_LEFT_SHIFT);
lastPressTime = millis();
blnDelayActions = false; blnDelayActions = false;
//lastPressTime = millis();
blnButtonActivty = true;
} }
else if(button4.wasReleased()) else if(button4.wasReleased())
{ {
lastPressTime = millis(); //lastPressTime = millis();
blnDelayActions = false; blnDelayActions = false;
blnReleaseALLKeys = false; //When FALSE - you must implicidly release Keys (below) 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 'Button action' has recently occurred.
switch (blnButtonActivty)
{
case true:
// 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) // 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) switch (blnDelayActions)
{ {
@ -176,7 +196,7 @@ void loop() {
switch (blnReleaseALLKeys) switch (blnReleaseALLKeys)
{ {
case true: case true:
// Release ALL Keyboard Keys (this should almost always be the case) // Release ALL Keyboard Keys (this should almost always be the case). Then reset 'ReleaseAllKeys' flag.
Keyboard.releaseAll(); Keyboard.releaseAll();
blnReleaseALLKeys = false; blnReleaseALLKeys = false;
break; break;
@ -185,16 +205,31 @@ void loop() {
break; break;
} }
//Reset the Button Activity Flag
blnButtonActivty = false;
break;
default:
// A 'Button Activty' HAS NOT Occurred. No need to do anything - This will also retain the 'lastPressTime' time value.
break;
}
//If LED is ON.... Check timer since last Button Activity.
switch (ledOn)
{
case true:
//If Max time has elapsed, then turn off LEDS
if((millis() - lastPressTime) > cintLedOffTime) if((millis() - lastPressTime) > cintLedOffTime)
{ {
ledOn = false; ledOn = false;
//lastPressTime = 0;
} }
else else
{ {
ledOn = true; ledOn = true;
} }
//If certain time has elapsed, then adjust LEDs brightness
if ((millis() - ledTickTime) > 10) if ((millis() - ledTickTime) > 10)
{ {
ledTickTime = millis(); ledTickTime = millis();
@ -213,6 +248,12 @@ void loop() {
analogWrite(LED3, ledBrightness); analogWrite(LED3, ledBrightness);
analogWrite(LED4, ledBrightness); analogWrite(LED4, ledBrightness);
} }
//}
break;
default:
// LEDs are Off.... Do nothing
break;
}
// Serial.print("Led Brightness = "); // Serial.print("Led Brightness = ");
// Serial.print(ledBrightness); // Serial.print(ledBrightness);