This repository has been archived by the owner on Oct 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Button.ino
211 lines (191 loc) · 5.32 KB
/
Button.ino
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
void setupButtons() {
pinMode(CATEGORY_BUTTON_PIN, INPUT_PULLUP);
resetButtons();
pinMode(WPS_LED_PIN, OUTPUT);
}
void resetButtons() {
storageCategory = 0;
cookedCategory = 2;
foodCategory = 4;
//////Serial.println("RESETTING BUTTONS");
analogWrite(RR, 0xd5);
analogWrite(GG, 0xb6);
analogWrite(BB, 0x4d);
analogWrite(R, 0x3e);
analogWrite(G, 0xb4);
analogWrite(B, 0xaf);
analogWrite(RRR, 0x3e);
analogWrite(GGG, 0xb4);
analogWrite(BBB, 0xaf);
}
void checkButtons() {
short buttonPress = analogRead(CATEGORY_BUTTON_PIN);
if (buttonPress < 42) {
foodCategory = 4;
analogWrite(RRR, 0x3e);
analogWrite(GGG, 0xb4);
analogWrite(BBB, 0xaf);
//////Serial.println("Other");
}
else if (buttonPress < 95) {
foodCategory = 3;
analogWrite(RRR, 0xf2);
analogWrite(GGG, 0x1d);
analogWrite(BBB, 0xff);
//////Serial.println("Dairy S");
}
else if (buttonPress < 144) {
foodCategory = 2;
analogWrite(RRR, 0xff);
analogWrite(GGG, 0x44);
analogWrite(BBB, 0x33);
////Serial.println("Dairy L");
}
else if (buttonPress < 188) {
foodCategory = 1;
analogWrite(RRR, 0x6d);
analogWrite(GGG, 0xff);
analogWrite(BBB, 0x6f);
////Serial.println("Meat");
}
else if (buttonPress < 226) {
foodCategory = 0;
analogWrite(RRR, 0xff);
analogWrite(GGG, 0xdd);
analogWrite(BBB, 0x12);
////Serial.println("Produce");
}
else if (buttonPress < 262) {
storageCategory = 0;
analogWrite(R, 0x3e);
analogWrite(G, 0xb4);
analogWrite(B, 0xaf);
////Serial.println("Fridge");
}
else if (buttonPress < 295) {
storageCategory = 1;
analogWrite(R, 0x92);
analogWrite(G, 0x6d);
analogWrite(B, 0xaf);
////Serial.println("Freezer");
}
else if (buttonPress < 320) {
cookedCategory = 0;
analogWrite(RR, 0xee);
analogWrite(GG, 0x88);
analogWrite(BB, 0x73);
////Serial.println("Raw");
}
else if (buttonPress < 350) {
cookedCategory = 1;
analogWrite(RR, 0x6d);
analogWrite(GG, 0xc1);
analogWrite(BB, 0x6f);
////Serial.println("Cooked");
}
else if (buttonPress < 375) {
cookedCategory = 2;
analogWrite(RR, 0xd5);
analogWrite(GG, 0xb6);
analogWrite(BB, 0x4d);
////Serial.println("Misc");
}
///////////////////////////////////////////////////////
// WPS CHECK
ms = millis(); //record the current time
myWPSButton.read(); //Read the button
switch (STATE) {
//This state watches for short and long presses, switches the LED for
//short presses, and moves to the TO_BLINK state for long presses.
case ONOFF:
if (myWPSButton.wasReleased()) {
switchLED();
scannerState = !scannerState;
}
else if (myWPSButton.pressedFor(LONG_PRESS))
STATE = TO_BLINK;
else if (ledState == true) {
if (ms - myWPSButton.lastChange() > 10000) {
digitalWrite(WPS_LED_PIN, LOW);
ledState = false;
scannerState = ADD;
}
else if (scannerState == ADD) {
digitalWrite(WPS_LED_PIN, LOW);
ledState = false;
}
}
break;
case TO_BLINK:
if (myWPSButton.wasReleased())
STATE = BLINK;
else
fastBlink();
break;
case BLINK:
if (myWPSButton.pressedFor(LONG_PRESS)) {
STATE = TO_ONOFF;
digitalWrite(WPS_LED_PIN, LOW);
ledState = false;
}
else
fastBlink();
break;
case TO_ONOFF:
if (myWPSButton.wasReleased())
STATE = ONOFF;
break;
}
/////////////////////////////////////////////
btnUP.read(); //read the buttons
btnDN.read();
if (page != lastPage) { //print the count if it has changed
constr();
lastPage = page;
////Serial.print("BUTTON INPUT DETECTED! NEW PAGE VALUE: "); ////Serial.println(page, DEC);
}
switch (DISPLAY_STATE) {
case WAIT: //wait for a button event
if (btnUP.wasPressed())
DISPLAY_STATE = INCR;
else if (btnDN.wasPressed())
DISPLAY_STATE = DECR;
else if (btnUP.wasReleased()) //reset the long press interval
rpt = REPEAT_FIRST;
else if (btnDN.wasReleased())
rpt = REPEAT_FIRST;
else if (btnUP.pressedFor(rpt)) { //check for long press
rpt += REPEAT_INCR; //increment the long press interval
DISPLAY_STATE = INCR;
}
else if (btnDN.pressedFor(rpt)) {
rpt += REPEAT_INCR;
DISPLAY_STATE = DECR;
}
break;
case DECR: //increment the counter
page = min(page++, (int)((activeTags + (10 - 1)) / 10) - 1); //but not more than the specified maximum
DISPLAY_STATE = WAIT;
break;
case INCR: //decrement the counter
page = max(page--, MIN_COUNT); //but not less than the specified minimum
DISPLAY_STATE = WAIT;
break;
}
}
void switchLED()
{
msLast = ms; //record the last switch time
ledState = !ledState;
digitalWrite(WPS_LED_PIN, ledState);
}
void fastBlink()
{
if (ms - myWPSButton.lastChange() > 60000) {
digitalWrite(WPS_LED_PIN, LOW);
ledState = false;
STATE = ONOFF;
}
else if (ms - msLast >= BLINK_INTERVAL)
switchLED();
}