-
Notifications
You must be signed in to change notification settings - Fork 0
/
tinyButton.cpp
77 lines (50 loc) · 1.63 KB
/
tinyButton.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include "tinyButton.h"
void tinyButton::begin() {
// Serial.println("tinyButton Libary");
delay(1000);
}
uint8_t tinyButton::sampleADC() { // returns the true analog state of the ADC
int buttonSample = analogRead(buttonPin);
return buttonSample;
}
// define boundaries between analog reads
#define NULL_BOUNDARY 5
//#define BOUNDARY_0 0
#define BOUNDARY_1 50
#define BOUNDARY_2 80
#define BOUNDARY_3 100
#define BOUNDARY_4 1024
uint8_t tinyButton::deriveButton() { // Returns integers 1 - 4 depending on button pressed 0 = NULL or no button pressed
int buttonSample = sampleADC();
if (buttonSample <= NULL_BOUNDARY) {
return 0;
} else if (buttonSample > NULL_BOUNDARY && buttonSample <= BOUNDARY_1) {
return 1;
} else if (buttonSample > BOUNDARY_1 && buttonSample <= BOUNDARY_2) {
return 4;
} else if (buttonSample > BOUNDARY_2 && buttonSample <= BOUNDARY_3) {
return 2;
} else if (buttonSample > BOUNDARY_3) {
return 3;
} else {
return false;
}
}
uint8_t tinyButton::buttonArray() {
int buttonDetected = deriveButton();
// Set all variables to zero
uint8_t buttonBits[4] = {0, 0, 0, 0};
// Then turn it into a 1 if that button has been detected
if (buttonDetected > 0) {
buttonBits[buttonDetected - 1] = 1;
}
// for (int i = 0; i < 4; i++) {
// Serial.print(buttonBits[i]);
// }
// Serial.println("");
}
uint8_t tinyButton::tinyButtonLoop() {
int activeButton = tinyButton::deriveButton(); // Use this line to return ints 1 - 4 if button is pressed 0 button is unpressed
// tinyButton::buttonArray(); // Use this line to return bits
return activeButton;
}