forked from mbroek/MBSE-ArdRims
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuttons.h
165 lines (143 loc) · 3.69 KB
/
buttons.h
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#ifndef BUTTONS_H
#define BUTTONS_H
byte btn_Press (byte Button_press, int msTime) {
if (digitalRead(Button_press) == 0) {
delay (msTime);
if (digitalRead(Button_press) == 0) {
while (digitalRead(Button_press) == 0) {
}
return 1;
}
}
return 0;
}
byte ReadButton(byte& Direction, unsigned long& Timer ) {
boolean f_btnUp, f_btnDn;
if (digitalRead (ButtonUpPin) == 0) { // Up button is pressed
if (Direction != DirectionUp)
Timer = millis(); // Register time of new direction
f_btnUp = true;
Direction = DirectionUp; // Register new direction
delay(35);
} else
f_btnUp = false;
if (digitalRead (ButtonDownPin) == 0) { // Down button is pressed
if (Direction != DirectionDown)
Timer = millis(); // Register time of new direction
f_btnDn = true;
Direction = DirectionDown; // Register new direction
delay(35);
} else
f_btnDn = false;
if (f_btnUp == false && f_btnDn == false)
Direction = DirectionNone; // No Up or Down direction.
}
int Set(int& Set, int Up, int Low, int Step, long Timer, byte Direction) {
int step_size;
int ButtonPressed;
if (Set > Up)
Set = Up;
if (Set < Low)
Set = Low;
delay(50);
if (Direction == DirectionUp) {
ButtonPressed = digitalRead(ButtonUpPin);
}
if (Direction == DirectionDown) {
ButtonPressed = digitalRead(ButtonDownPin);
}
if (ButtonPressed == 0 && Direction != DirectionNone) {
if (((millis() - Timer) / 1000) >= 4)
step_size = (Step * 20);
else {
if (((millis() - Timer) / 1000) >= 2)
step_size = (Step * 5);
else
step_size = Step;
}
if (Direction == DirectionUp) {
if (Set + step_size > Up)
Set = Up;
else
Set += step_size;
} else if (Direction == DirectionDown) {
if (Set - step_size < Low)
Set = Low;
else
Set -= step_size;
}
}
}
float Set(float& Set, float Up, float Low, float Step, long Timer, byte Direction) {
float step_size;
int ButtonPressed;
if (Set > Up)
Set = Up;
if (Set < Low)
Set = Low;
delay(50);
if (Direction == DirectionUp) {
ButtonPressed = digitalRead(ButtonUpPin);
}
if (Direction == DirectionDown) {
ButtonPressed = digitalRead(ButtonDownPin);
}
if (ButtonPressed == 0 && Direction != DirectionNone) {
if (((millis() - Timer) / 1000) >= 4)
step_size = (Step * 20.0);
else {
if (((millis() - Timer) / 1000) >= 2)
step_size = (Step * 5.0);
else
step_size = Step;
}
if (Direction == DirectionUp) {
if (Set + step_size > Up)
Set = Up;
else
Set += step_size;
} else if (Direction == DirectionDown) {
if (Set - step_size < Low)
Set = Low;
else
Set -= step_size;
}
}
}
byte Set(byte& Set, byte Up, byte Low, byte Step, long Timer, byte Direction) {
int step_size;
int ButtonPressed;
if (Set > Up)
Set = Up;
if (Set < Low)
Set = Low;
delay(50);
if (Direction == DirectionUp) {
ButtonPressed = digitalRead(ButtonUpPin);
}
if (Direction == DirectionDown) {
ButtonPressed = digitalRead(ButtonDownPin);
}
if (ButtonPressed == 0 && Direction != DirectionNone) {
if (((millis() - Timer) / 1000) >= 4)
step_size = (Step * 20);
else {
if (((millis() - Timer) / 1000) >= 2)
step_size = (Step * 5);
else
step_size = Step;
}
if (Direction == DirectionUp) {
if (Set + step_size > Up)
Set = Up;
else
Set += step_size;
} else if (Direction == DirectionDown) {
if (Set - step_size < Low)
Set = Low;
else
Set -= step_size;
}
}
}
#endif