mirror of
https://github.com/2martens/uni.git
synced 2026-05-06 11:26:25 +02:00
[ES-Blatt2] Generalized behaviour
Signed-óff-by: Jim Martens <github@2martens.de>
This commit is contained in:
@ -12,16 +12,15 @@ int buttonTwoPin = 3;
|
|||||||
int compareValue = 60;
|
int compareValue = 60;
|
||||||
int rc = 10499;
|
int rc = 10499;
|
||||||
|
|
||||||
// these values are used by the TC0_Handler
|
// this variable is affected by the buttonOne and buttonTwo actions
|
||||||
// do not write into them within the loop/setup
|
|
||||||
// only the counter, buttonOnePressed and buttonTwoPressed should be used within the loop
|
|
||||||
int volatile counter = 0;
|
int volatile counter = 0;
|
||||||
bool volatile buttonOnePressed = false;
|
|
||||||
bool volatile buttonTwoPressed = false;
|
// these values are used by the TC0_Handler
|
||||||
|
// do not use them at all
|
||||||
int timesPressedOne = 0;
|
int timesPressedOne = 0;
|
||||||
int timesPressedTwo = 0;
|
int timesPressedTwo = 0;
|
||||||
bool increased = false;
|
bool buttonOneActionPerformed = false;
|
||||||
bool decreased = false;
|
bool buttonTwoActionPerformed = false;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Setup function for initial setup code
|
* Setup function for initial setup code
|
||||||
@ -63,22 +62,22 @@ void loop() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Increases the counter by 1.
|
* Performs the action for button one.
|
||||||
*
|
*
|
||||||
* The highest possible value of the counter is 255.
|
* Has to be changed for the specific use case.
|
||||||
*/
|
*/
|
||||||
void increment() {
|
void buttonOneAction() {
|
||||||
if (counter < 255) {
|
if (counter < 255) {
|
||||||
counter += 1;
|
counter += 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Decreases the counter by 1.
|
* Performs the action for button two.
|
||||||
*
|
*
|
||||||
* The lowest possible value of the counter is 0.
|
* Has to be changed for the specific use case.
|
||||||
*/
|
*/
|
||||||
void decrement() {
|
void buttonTwoAction() {
|
||||||
if (counter > 0) {
|
if (counter > 0) {
|
||||||
counter -= 1;
|
counter -= 1;
|
||||||
}
|
}
|
||||||
@ -97,41 +96,41 @@ void TC0_Handler()
|
|||||||
|
|
||||||
// handles the plus button
|
// handles the plus button
|
||||||
if (buttonPressedOne){
|
if (buttonPressedOne){
|
||||||
++timesPressedOne;
|
timesPressedOne += 1;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
if (increased) {
|
if (buttonOneActionPerformed) {
|
||||||
increased = false;
|
buttonOneActionPerformed = false;
|
||||||
}
|
}
|
||||||
timesPressedOne = 0 ;
|
timesPressedOne = 0 ;
|
||||||
}
|
}
|
||||||
|
|
||||||
// handles the minus button
|
// handles the minus button
|
||||||
if (buttonPressedTwo){
|
if (buttonPressedTwo){
|
||||||
++timesPressedTwo;
|
timesPressedTwo += 1;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
if (decreased) {
|
if (buttonTwoActionPerformed) {
|
||||||
decreased = false;
|
buttonTwoActionPerformed = false;
|
||||||
}
|
}
|
||||||
timesPressedTwo = 0 ;
|
timesPressedTwo = 0 ;
|
||||||
}
|
}
|
||||||
|
|
||||||
// increases counter if button has been pressed long enough
|
// performs button one action if button has been pressed long enough
|
||||||
if (timesPressedOne >= compareValue) {
|
if (timesPressedOne >= compareValue) {
|
||||||
if (!increased) {
|
if (!buttonOneActionPerformed) {
|
||||||
increment();
|
buttonOneAction();
|
||||||
}
|
}
|
||||||
increased = true;
|
buttonOneActionPerformed = true;
|
||||||
timesPressedOne = 0;
|
timesPressedOne = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
// decreases counter if button has been pressed long enough
|
// performs button two action if button has been pressed long enough
|
||||||
if (timesPressedTwo >= compareValue) {
|
if (timesPressedTwo >= compareValue) {
|
||||||
if (!decreased) {
|
if (!buttonTwoActionPerformed) {
|
||||||
decrement();
|
buttonTwoAction();
|
||||||
}
|
}
|
||||||
decreased = true;
|
buttonTwoActionPerformed = true;
|
||||||
timesPressedTwo = 0;
|
timesPressedTwo = 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user