Difference between revisions of "IoT - Modular - Buttons.h"

From The TinkerNet Wiki
Jump to navigation Jump to search
Line 1: Line 1:
 +
//Use case code at end of file
 +
//Submitted by somebody that probably shouldn't be coding so fix it if it's borked...
 +
 +
 
<syntaxhighlight lang="cpp" line="1">
 
<syntaxhighlight lang="cpp" line="1">
  
Line 63: Line 67:
  
 
</syntaxhighlight>
 
</syntaxhighlight>
 +
 +
//***********************************************************************************************************************
 +
Code Suggestion:
 +
// I/O - Setup the I/O PINS HERE
 +
PushButton BTN1_TRIGGER = { 23 };// Creates PushButton instance for buttons.h class to work with
 +
PushButton BTN2_TRIGGER = { 14 }; //Etc...
 +
PushButton BTN3_TRIGGER = { 21 }; //Etc...
 +
PushButton BTN4_TRIGGER = { 19 }; //Etc...
 +
PushButton BTN5_TRIGGER = { 18 }; //Etc...
 +
PushButton BTN6_TRIGGER = { 17 }; //Etc...
 +
//These are initialized as part of the class and don't require initialization in 'void setup{}'
 +
 +
 +
 +
void loop{};
 +
uint16_t button_function = button_0;
 +
 +
if (BTN1_TRIGGER.isPressed()) {
 +
button_function = button_1;
 +
}
 +
else if (BTN2_TRIGGER.isPressed()) {
 +
button_function = button_2;
 +
}
 +
else if (BTN3_TRIGGER.isPressed()) {
 +
button_function = button_3;
 +
}
 +
else if (BTN4_TRIGGER.isPressed()) {
 +
button_function = button_4;
 +
}
 +
else if (BTN5_TRIGGER.isPressed()) {
 +
button_function = button_5;
 +
}
 +
else if (BTN6_TRIGGER.isPressed()) {
 +
button_function = button_6;
 +
}
 +
 +
switch (button_function) {
 +
default:
 +
case button_0:
 +
case button_1:
 +
                //Do something
 +
break;
 +
case button_2:
 +
                // Do something else
 +
break;
 +
case button_3:
 +
                // Doing something totally different
 +
break;
 +
case button_4:
 +
                // Wish I could do something
 +
break;
 +
case button_5:
 +
                // Stop doing that, please for the love of all that is holy!
 +
break;
 +
case button_6:
 +
                // Never mind... I will do this instead
 +
break;
 +
 +
}

Revision as of 01:26, 2 August 2021

//Use case code at end of file //Submitted by somebody that probably shouldn't be coding so fix it if it's borked...


 1 class PushButton {
 2 
 3 public:
 4 
 5 PushButton(uint8_t pin) // Constructor (executes when a PushButton object is created)
 6 
 7 pin(pin) { // remember the push button pin
 8 
 9 pinMode(pin, INPUT_PULLUP); // enable the internal pull-up resistor
10 
11 };
12 
13 bool isPressed() // read the button state check if the button has been pressed, debounce the button as well
14 
15 {
16 
17 bool pressed = false;
18 
19 bool state = digitalRead(pin);               // read the button's state
20 
21 int8_t stateChange = state - previousState;  // calculate the state change since last time
22 
23 if (stateChange == rising) { // If the button is pressed (went from high to low)
24 
25 if (millis() - previousBounceTime > debounceTime) { // check if the time since the last bounce is higher than the threshold
26 
27 pressed = true; // the button is pressed
28 
29 }
30 
31 }
32 
33 if (stateChange == falling) { // if the button is released or bounces
34 
35 previousBounceTime = millis(); // remember when this happened
36 
37 }
38 
39 previousState = state; // remember the current state
40 
41 return pressed; // return true if the button was pressed and didn't bounce
42 
43 };
44 
45 private:
46 
47 uint8_t pin;
48 
49 bool previousState = HIGH;
50 
51 unsigned long previousBounceTime = 0;
52 
53 const static unsigned long debounceTime = 1000;
54 
55 const static int8_t rising = HIGH - LOW;
56 
57 const static int8_t falling = LOW - HIGH;
58 
59 };

//*********************************************************************************************************************** Code Suggestion: // I/O - Setup the I/O PINS HERE PushButton BTN1_TRIGGER = { 23 };// Creates PushButton instance for buttons.h class to work with PushButton BTN2_TRIGGER = { 14 }; //Etc... PushButton BTN3_TRIGGER = { 21 }; //Etc... PushButton BTN4_TRIGGER = { 19 }; //Etc... PushButton BTN5_TRIGGER = { 18 }; //Etc... PushButton BTN6_TRIGGER = { 17 }; //Etc... //These are initialized as part of the class and don't require initialization in 'void setup{}'


void loop{}; uint16_t button_function = button_0;

if (BTN1_TRIGGER.isPressed()) { button_function = button_1; } else if (BTN2_TRIGGER.isPressed()) { button_function = button_2; } else if (BTN3_TRIGGER.isPressed()) { button_function = button_3; } else if (BTN4_TRIGGER.isPressed()) { button_function = button_4; } else if (BTN5_TRIGGER.isPressed()) { button_function = button_5; } else if (BTN6_TRIGGER.isPressed()) { button_function = button_6; }

switch (button_function) { default: case button_0: case button_1:

               //Do something

break; case button_2:

               // Do something else

break; case button_3:

               // Doing something totally different

break; case button_4:

               // Wish I could do something

break; case button_5:

               // Stop doing that, please for the love of all that is holy!

break; case button_6:

               // Never mind... I will do this instead

break;

}