IoT - Modular - Buttons.h

From The TinkerNet Wiki
Revision as of 00:28, 2 August 2021 by Grant (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

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 };
 1 /***********************************************************************************************************************
 2 Code Suggestion:
 3 // I/O - Setup the I/O PINS HERE
 4 	PushButton BTN1_TRIGGER	= { 23 };// Creates PushButton instance for buttons.h class to work with
 5 	PushButton BTN2_TRIGGER = { 14 }; //Etc...
 6 	PushButton BTN3_TRIGGER	= { 21 }; //Etc...
 7 	PushButton BTN4_TRIGGER	= { 19 }; //Etc...
 8 	PushButton BTN5_TRIGGER	= { 18 }; //Etc...
 9 	PushButton BTN6_TRIGGER	= { 17 }; //Etc...
10 //These are initialized as part of the class and don't require initialization in 'void setup{}'
11 
12 
13 
14 void loop{};
15 	uint16_t button_function = button_0;
16 
17 	if (BTN1_TRIGGER.isPressed()) {
18 		button_function = button_1;
19 	} 
20 	else if (BTN2_TRIGGER.isPressed()) {
21 		button_function = button_2;
22 	}
23 	else if (BTN3_TRIGGER.isPressed()) {
24 		button_function = button_3;
25 	}
26 	else if (BTN4_TRIGGER.isPressed()) {
27 		button_function = button_4;
28 	}
29 	else if (BTN5_TRIGGER.isPressed()) {
30 		button_function = button_5;
31 	}
32 	else if (BTN6_TRIGGER.isPressed()) {
33 		button_function = button_6;
34 	}
35 
36 switch (button_function) {
37 		default:
38 		case button_0:
39 		case button_1: 
40                 //Do something
41 		break;
42 		case button_2:
43                 // Do something else
44 		break;
45 		case button_3:
46                 // Doing something totally different
47 		break;
48 		case button_4:
49                 // Wish I could do something
50 		break;
51 		case button_5: 
52                 // Stop doing that, please for the love of all that is holy!
53 		break;
54 		case button_6: 
55                 // Never mind... I will do this instead
56 		break;
57 	
58 	}
59 */