From e25a54050a8c5a3dfc624506ac89b260757f6e2d Mon Sep 17 00:00:00 2001 From: Jim Martens Date: Wed, 22 Apr 2015 19:31:53 +0200 Subject: [PATCH] [ES-Blatt2] Generalized behaviour MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-óff-by: Jim Martens --- es/blatt2/uebung2-1/uebung2-1.ino | 53 +++++++++++++++---------------- 1 file changed, 26 insertions(+), 27 deletions(-) diff --git a/es/blatt2/uebung2-1/uebung2-1.ino b/es/blatt2/uebung2-1/uebung2-1.ino index a8f6361..0d9a676 100644 --- a/es/blatt2/uebung2-1/uebung2-1.ino +++ b/es/blatt2/uebung2-1/uebung2-1.ino @@ -12,16 +12,15 @@ int buttonTwoPin = 3; int compareValue = 60; int rc = 10499; -// these values are used by the TC0_Handler -// do not write into them within the loop/setup -// only the counter, buttonOnePressed and buttonTwoPressed should be used within the loop +// this variable is affected by the buttonOne and buttonTwo actions 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 timesPressedTwo = 0; -bool increased = false; -bool decreased = false; +bool buttonOneActionPerformed = false; +bool buttonTwoActionPerformed = false; /** * 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) { 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) { counter -= 1; } @@ -97,41 +96,41 @@ void TC0_Handler() // handles the plus button if (buttonPressedOne){ - ++timesPressedOne; + timesPressedOne += 1; } else { - if (increased) { - increased = false; + if (buttonOneActionPerformed) { + buttonOneActionPerformed = false; } timesPressedOne = 0 ; } // handles the minus button if (buttonPressedTwo){ - ++timesPressedTwo; + timesPressedTwo += 1; } else { - if (decreased) { - decreased = false; + if (buttonTwoActionPerformed) { + buttonTwoActionPerformed = false; } 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 (!increased) { - increment(); + if (!buttonOneActionPerformed) { + buttonOneAction(); } - increased = true; + buttonOneActionPerformed = true; 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 (!decreased) { - decrement(); + if (!buttonTwoActionPerformed) { + buttonTwoAction(); } - decreased = true; + buttonTwoActionPerformed = true; timesPressedTwo = 0; } }