-
Notifications
You must be signed in to change notification settings - Fork 0
/
V1.4.ino
388 lines (348 loc) · 10.4 KB
/
V1.4.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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
// ***********************************************************
// ** 7/1/2021 The Pet Feeder **
// ** ^^^^^^^^^^^^^^^^ **
// ** by Ran Malach **
// ** **
// ** Control a DC motor to drive an AUGER Doser. **
// ** Two buttons L(eft) & R(ight). **
// ** Limit Feeds to twice a day + a bonus feed. **
// ** State machine implementation. **
// ***********************************************************
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include "Clock.h"
#define LCD_ROW_LENGTH 16
#define DEBOUNCE 200
#define CLOCK_DISPLAY_LENGTH 13
#define DELAY_FOR_BACKLIGHT_IN_SECONDS 25
#define NUM_OPTIONS_MAIN_MENU 4
#define MAX_FEED_IN_SECONDS 20
#define AUGER 13
#define L_BUTTON 8
#define R_BUTTON 7
#define LED1 10
#define LED2 11
#define LED3 12
enum fsm {IDLE, MENU, SET_TIME, SET_FEED_FACTOR, HIST};
fsm state = IDLE;
// Setting the length of feeds
uint8_t feedingFactor = 1;
// This array holds a week of feeding history.
// Every feed will be written here.
char feedingHistory[7][LCD_ROW_LENGTH * 2] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
// For the two buttons:
// - state is digitally read, normally HIGH
// - previous is the last clock button state,
// its purpose is for ignoring the button while its pressed and wait for release
// - on is the buttom line...
bool Lon = LOW, Ron = LOW, Lstate = HIGH, Rstate = HIGH;
bool Lprevious = HIGH, Rprevious = HIGH;
// For simplyfing the menu
bool firstRun = true;
// For scrolling menus
uint8_t optionSelect = 0;
// Counting the period of pressing buttons.
// Its making the buttons pressing more stable & it will help to set the clock.
unsigned long Lt = 0, Rt = 0;
// t is for counting the auger operating duration
// dt is for counting a second == 1000 * millis()
unsigned long t = 0, dt = 0;
// Counting the seconds of backlight for shutdown..
unsigned int backLightOnSecCnt = 0;
// String for the display in IDLE state
char clockStr[CLOCK_DISPLAY_LENGTH];
// Checkout my Clock.h implemantation
Clock clk;
LiquidCrystal_I2C lcd(0x27, LCD_ROW_LENGTH, 2); // set the LCD address to 0x27 for a 16 chars and 2 line display
void setup()
{
//Serial.begin(9600);
pinMode(AUGER, OUTPUT);
pinMode(LED1, OUTPUT);
pinMode(LED2, OUTPUT);
pinMode(LED3, OUTPUT);
pinMode(L_BUTTON, INPUT);
pinMode(R_BUTTON, INPUT);
lcd.init();
lcd.backlight();
lcd.setCursor(1,0);
lcd.print("The Pet Feeder");
lcd.setCursor(2,1);
lcd.print("By Ran Malach");
delay(2000);
lcd.clear();
}
void loop()
{
if(millis() - dt > 999)
// Count a second.
{
dt = millis();
// Pass a sec in clock
clk.tic();
if(!clk.getFeedCnt())
// the feed counter zeroes at midnight, so...
// turn off the lights, g'night...
{
digitalWrite(LED1, LOW);
digitalWrite(LED2, LOW);
digitalWrite(LED3, LOW);
}
if(state == IDLE)
// Show the clock each sec in IDLE state
{
lcd.setCursor(2, 1);
clk.clock2str(clockStr);
lcd.print(clockStr);
}
}
if(clk.getElapsedSecs() - backLightOnSecCnt > DELAY_FOR_BACKLIGHT_IN_SECONDS)
// Turn off the backlight after several seconds of no activity
// Each press on one of the two buttons resets the backLightOnSecCnt variable
{
backLightOnSecCnt = clk.getElapsedSecs();
lcd.clear();
lcd.noBacklight();
optionSelect = 0;
state = IDLE;
}
// ------------------------------------------------------
// Read the button's state (check the func. down below) |
buttonsRead(); // |
// ------------------------------------------------------
// ************************ FSM ********************************
switch(state)
{
case IDLE: // ********** fsm - IDLE *******************************
if(millis() - t > 1000 * (unsigned long)(feedingFactor / (clk.getFeedCnt() != 3 ? 1 : 2)))
// Stop feeding.
// The ternary expression is for the Bonus feed. [ ^ ^ ^ ^ ]
// If that Hungry little one wants an extra meal, give it half a dose.
{
digitalWrite(AUGER, LOW);
}
else
// Disable pressing 'feed' while feeding
{
Lon = LOW;
}
if(Lon)
// Left button is pressed
{
if(clk.getFeedCnt() < 3)
// Check if the feeding counter enables feeding
{
// Feed
digitalWrite(AUGER, HIGH);
// Turn on the appropriate led
digitalWrite(LED1 + clk.getFeedCnt(), HIGH);
// Advance feed Counter
clk.setFeedCnt();
// Write this feed to the feeding HISTORY's array
clk.clock2strHist(feedingHistory[clk.getDay()]);
lcd.print("Feed");
t = millis();
lcd.clear();
}
}
if(Ron)
// Right button is pressed
{
state = MENU;
menuDisp();
}
break; // end case IDLE
case MENU: // ********** fsm - MENU *******************************
if(Lon)
{
switch(optionSelect)
{
case 0:
state = HIST;
firstRun = true;
break;
case 1:
state = SET_TIME;
optionSelect = 0;
firstRun = true;
lcd.clear();
break;
case 2:
state = SET_FEED_FACTOR;
feedDisp();
optionSelect = 0;
break;
case 3:
state = IDLE;
lcd.clear();
optionSelect = 0;
break;
default:
break;
}
}
if(Ron)
{
optionSelect++;
if(optionSelect == NUM_OPTIONS_MAIN_MENU)
optionSelect = 0;
menuDisp();
}
break; // end case MENU
case SET_FEED_FACTOR: // ********** fsm - SET_FEED_FACTOR ************************
if(Lon)
{
state = IDLE;
lcd.clear();
}
if(Ron)
{
feedingFactor++;
if(feedingFactor > MAX_FEED_IN_SECONDS)
feedingFactor = 1;
feedDisp();
}
break;
case SET_TIME: // ************* fsm - SET_TIME *************************
setClock();
break;
case HIST: // ******************* fsm - HIST *********************
if(firstRun)
{
lcd.clear();
// Use optionSelect as the day to show..
optionSelect = clk.getDay();
lcd.print(feedingHistory[optionSelect]);
lcd.setCursor(0, 1);
// Well, the feedingHistory array is of size : [7][two times row length]
lcd.print(&feedingHistory[optionSelect][LCD_ROW_LENGTH]);
firstRun = false;
}
if(Ron)
{
optionSelect++;
if(optionSelect == 7)
optionSelect = 0;
lcd.clear();
lcd.print(feedingHistory[optionSelect]);
lcd.setCursor(0, 1);
lcd.print(&feedingHistory[optionSelect][LCD_ROW_LENGTH]);
}
if(Lon)
{
optionSelect = 0;
state = IDLE;
lcd.clear();
}
break;
default: // ************* DEFAULT *************************
break;
} //************************** end fsm - switch state *******************************
}
void buttonsRead()
{
Lstate = digitalRead(L_BUTTON);
Rstate = digitalRead(R_BUTTON);
if(!Lstate && Lprevious && millis() - Lt > DEBOUNCE)
{
Lon = HIGH;
Lt = millis();
backLightOnSecCnt = clk.getElapsedSecs();
lcd.backlight();
}
else
Lon = LOW;
if(!Rstate && (state == SET_TIME ? 1 : Rprevious) && millis() - Rt > DEBOUNCE)
{
Ron = HIGH;
Rt = millis();
backLightOnSecCnt = clk.getElapsedSecs();
lcd.backlight();
}
else
Ron = LOW;
Lprevious = Lstate;
Rprevious = Rstate;
}
void menuDisp()
{
uint8_t op[] = {11, 0, 6, 11};
lcd.clear();
lcd.print("MENU:");
lcd.setCursor(12, 0);
lcd.print("hist");
lcd.setCursor(0, 1);
lcd.print(" clock feed back");
// Use that ternary to switch between rows
lcd.setCursor(op[optionSelect], optionSelect == 0 ? 0 : 1);
lcd.print('*');
}
void feedDisp()
{
lcd.clear();
lcd.print("Feeding Duration");
lcd.setCursor(7, 1);
lcd.print(feedingFactor);
}
void setClock()
{
char dayStr[4];
if(Lon)
{
optionSelect++;
firstRun = true;
lcd.clear();
}
switch(optionSelect)
{
case 0:
if(Ron)
{
clk.setMin(clk.getMin() + 1);
lcd.clear();
firstRun = true;
}
if(firstRun)
{
lcd.print("Minutes: ");
lcd.print(clk.getMin() / 10);
lcd.print(clk.getMin() % 10);
firstRun = false;
}
break;
case 1:
if(Ron)
{
clk.setHour(clk.getHour() + 1);
lcd.clear();
firstRun = true;
}
if(firstRun)
{
lcd.print("Hours: ");
lcd.print(clk.getHour() / 10);
lcd.print(clk.getHour() % 10);
firstRun = false;
}
break;
case 2:
if(Ron)
{
clk.setDay((weekDays)((clk.getDay() + 1) % 7));
lcd.clear();
firstRun = true;
}
if(firstRun)
{
lcd.print("Day: ");
clk.getDayStr(dayStr);
lcd.print(dayStr);
firstRun = false;
}
break;
default:
optionSelect = 0;
state = IDLE;
break;
}
}