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,18 +22,20 @@
#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'.
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,112 +112,149 @@ 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 '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 'Button action' has recently occurred.
switch (blnDelayActions) switch (blnButtonActivty)
{ {
case true: case true:
// Pause // A 'Button Activty' HAS occurred.
delay(cintDebounceTime);
blnDelayActions = false; //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; break;
default: default:
// No Delay, or maybe repeat action(s) // A 'Button Activty' HAS NOT Occurred. No need to do anything - This will also retain the 'lastPressTime' time value.
delay(0);
break; break;
} }
// Check if ALL Keyboard Keys should now be released (Usually 'true', but perhaps 'false' if 'multi-button hold combinations' are required) //If LED is ON.... Check timer since last Button Activity.
switch (blnReleaseALLKeys) switch (ledOn)
{ {
case true: case true:
// Release ALL Keyboard Keys (this should almost always be the case) //If Max time has elapsed, then turn off LEDS
Keyboard.releaseAll(); if((millis() - lastPressTime) > cintLedOffTime)
blnReleaseALLKeys = false; {
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; break;
default: 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.) // LEDs are Off.... Do nothing
break; 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("Led Brightness = ");
// Serial.print(ledBrightness); // Serial.print(ledBrightness);
// Serial.print("\t Led State = "); // Serial.print("\t Led State = ");