-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMAIN.ino
1456 lines (1249 loc) · 39.3 KB
/
MAIN.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
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*###########################################################################
################### INCLUDES AND DEFINES ####################################*/
#include <Wire.h> // Include Wire library for I2C communication
#define TIMEOUT_COUNT 1000 // Set timeout constant to be used for returning to the main screen
// SCREEN
#include <LiquidCrystal_I2C.h> // Include LiquidCrystal_I2C library for controlling LCD display
//RTC
#include <uRTCLib.h> // Include uRTCLib library for managing real-time clock
// SOM
#include <SoftwareSerial.h> // Include SoftwareSerial library to create serial port on digital pins
#include "RedMP3.h" // Include the RedMP3 library for MP3 functionality
#define ARDUINO_RX 7 // Define digital pin 7 as RX for MP3 communication
#define ARDUINO_TX 8 // Define digital pin 8 as TX for MP3 communication
// TEMPHUMIDITY
#include <dht.h> // Include the dht library for controlling DHT22 sensor
#define dataPin 9 // Define digital pin 9 for data transmission from DHT22 sensor
/*################# END INCLUDES AND DEFINES ################################
#############################################################################*/
/*###########################################################################
################### VARIABLES OR FIXED ######################################*/
// RTC
uRTCLib rtc(0x68); // Instantiate uRTCLib object for RTC communication (0x68 is the default)
char auxtime[6][3]; // Create a 2D character array to hold time data
// LCD
LiquidCrystal_I2C lcd(0x27, 16, 4); //Instantiate LiquidCrystal_I2C object to interface with the LCD (0x27 is the default)
// ENCODER
int switchPin = 13; // Define digital pin 13 as switch pin (encoder button)
int click = HIGH; // Initialize the click variable as HIGH
int pinA = 12; // Define pinA for Rotary encoder (input S1)
int pinB = 11; // Define pinB for Rotary encoder (input S2)
int pinAstateCurrent = LOW; // Initialize the current state of Pin S1 as LOW
int pinAStateLast = pinAstateCurrent; // Initialize the last read value of Pin S2 as the current state of Pin S1
// SOM
MP3 mp3(ARDUINO_RX, ARDUINO_TX); //MP3
int8_t musica1 = 0x01; //primeira música do cartão SD
int8_t musica2 = 0x02; //segunda música do cartão SD
int8_t folderName = 0x01; //pasta do cartão
int8_t lastVolume = 20; //volume atual
int ss = musica1; // sound selected default option musica1
// MOTOR
const int Enable = 2; // Define digital pin 2 as Enable pin
const int step = 3; // Define digital pin 3 as step pin
const int dirPin = 4; // Define digital pin 4 as direction pin
int calibratedaux = 100; // Auxiliary value for calibration
int calibrated_value = 100; // Value to store calibrated values for feed
int amount = 200; // Default feed amount
//TEMPHUMIDITY
dht DHT;
// In the line below, the function `read22(dataPin)` from the DHT library is being used to read the temperature and humidity values from the sensor
float temperature, humidity;
// CUSTOM CHARACTERS FOR THE LCD SCREEN
//GARFINHO
void garfofaca(){
byte garfo[8] = {0b00000, 0b01010, 0b01110,
0b01110,
0b00100,
0b00100,
0b00100,
0b00000
};
byte faca[8] = {
0b00000,
0b00100,
0b01100,
0b01100,
0b01100,
0b00100,
0b00100,
0b00000
};
lcd.createChar(0, garfo);
lcd.createChar(1,faca);
lcd.setCursor(16, 3);
lcd.write(byte(0));
lcd.setCursor(17, 3);
lcd.write(byte(1));
}
// FAQUINHA
// void faca(){
// byte faca[8] = {
// 0b00000,
// 0b00100,
// 0b01100,
// 0b01100,
// 0b01100,
// 0b00100,
// 0b00100,
// 0b00000
// };
// lcd.createChar(0, faca);
// lcd.setCursor(17, 3);
// lcd.write(byte(0));
// }
// ANIMAÇÃO DELAY
int anim = 300;
// AUX VARIABLES
int st = 0; //setting state 0 - main screen state
bool refresh_screen = true; // impede q fique printando a tela 900x até ir pra proxima (NÃO FICA PISCANDO)
int encoder = 0; // Rotary encoder reading, default to zero
int line = 0; // Current line of the LCD screen
int timer = 0; // pra contar o num de ciclos até voltar pra tela inicial
int meal[2][4] = { { 7, 30, 200, 0 }, { 16, 30, 200, 0 } }; // meals settings
//HOUR, MINUTE, PORTION AND SLOW (ON/OFF)
int x = 0; //aux to meals settings
char slowmode[2][4] = { "off", " on" }; //slow mode
int nextmeal=0; // hora da próxima refeição
int currentSec, currentMin, currentHour, currentDay, currentMonth, currentYear;
int nexthour, nextmin;
/*################# END VARIABLES OR FIXED ##################################
#############################################################################*/
/*###########################################################################
#################### SETUP ##################################################*/
void setup() {
Serial.begin(9600); // Initialise the serial communication
// ENCODER
pinMode(switchPin, INPUT_PULLUP); // Enable the switchPin as input with a PULLUP resistor
pinMode(pinA, INPUT); // Set PinA as input
pinMode(pinB, INPUT); // Set PinB as input
// LCD
lcd.init(); // Initialize the LCD
lcd.setBacklight(HIGH); // Turn on the backlight
// SOM
mp3.setVolume(lastVolume); //selecionar o volume
//RTC
URTCLIB_WIRE.begin();
// Comment out below line once you set the date & time.
// Following line sets the RTC with an explicit date & time
// for example to set January 13 2022 at 12:56 you would call:
//rtc.set(0, 34, 14, 6, 2, 6, 23);
// rtc.set(second, minute, hour, dayOfWeek, dayOfMonth, month, year)
// set day of week (1=Sunday, 7=Saturday)
// MOTOR
pinMode(step, OUTPUT);
pinMode(dirPin, OUTPUT);
pinMode(Enable, OUTPUT);
}
/*################## END SETUP ##############################################
############################################################################*/
/*###########################################################################
##################### AUX FUNCTIONS #########################################*/
// ENCODER
int read_encoder() { // Read and interpret rotary encoder inputs because it wans't working on the other thing
int movement = 0; // Initializes a variable to hold the encoder movement
int pinAstateCurrent = digitalRead(pinA); // Read the current state of pinA
// If the encoder knob was turned
if ((pinAStateLast == LOW) && (pinAstateCurrent == HIGH)) { // hecks if pinA state has transitioned from LOW to HIGH, which indicates that the encoder was turned
if (digitalRead(pinB) == HIGH) {
movement = 1; // direção do relogiooo
} else {
movement = -1; //oposto do relogiooo
}
}
pinAStateLast = pinAstateCurrent; //guarda o ultimo estado do pinA pra comparar depois
return movement; //retorna o movimento binito
}
int Cursor_nav() { //nova função de navegação do encoder, funciona apenas na interface
static int aux = 0; //onde o encoder tá
static int aux_last = -1; // will store the last position of aux, I should write everything in one language
click = digitalRead(switchPin); // Read the digital value of the switch (LOW/HIGH)
// If the switch is pressed (LOW), return current line
if (click == LOW) {
return aux;
}
// Read encoder
int encoder_move = read_encoder(); //determines if the rotary encoder has moved
if (encoder_move == 1) { //se for na direção do relógio é positivo
aux++;
if (aux > 3) aux = 0;
} else if (encoder_move == -1) { //se for no oposto do relógio é negativo
aux--;
if (aux < 0) aux = 3; //volta pra primeira linha se atingir o 3
}
// Only print cursor if it moved
if (aux != aux_last) {
lcd.setCursor(0, 0); // Clear all possible cursor positions
lcd.print(" ");
lcd.setCursor(0, 1);
lcd.print(" ");
lcd.setCursor(0, 2);
lcd.print(" ");
lcd.setCursor(0, 3);
lcd.print(" ");
// Set the cursor to the new position and print an arrow
lcd.setCursor(0, aux);
lcd.print("->");
// Save the current cursor position as the last position
aux_last = aux;
}
return aux;
}
int encoderChangehour(int hour) { //ajustar hora
int encoder_move = read_encoder(); //determines if the rotary encoder has moved
if (encoder_move == 1) {
hour++;
if (hour > 23){
hour = 0;
}
} else if (encoder_move == -1) {
hour--;
if (hour < 0){
hour = 23;
}
}
//hour = constrain(hour, 0, 23); // Constrain the hour value between 0 and 23 to represent valid hours in a day
// Only print cursor if it moved
if (hour != meal[x][0]) {
meal[x][0] = hour; // Update the stored hour
}
return hour; // Return the new hour value
}
int encoderChangeminute(int minute) { //ajustar minuto
int encoder_move = read_encoder(); //determines if the rotary encoder has moved
if (encoder_move == 1) {
minute++;
if (minute > 59){
minute = 0;
}
} else if (encoder_move == -1) {
minute--;
if (minute < 0){
minute = 59;
}
}
// minute = constrain(minute, 0, 59);
// Only print cursor if it moved
if (minute != meal[x][1]) {
meal[x][1] = minute; // Update the stored minute
}
return minute;
}
int encoderChangeportion(int portion) { //ajustar porção
int encoder_move = read_encoder(); //determines if the rotary encoder has moved
if (encoder_move == 1) {
portion= portion+10;
} else if (encoder_move == -1) {
portion= portion-10;
}
portion = constrain(portion, 0, 1000);
// Only print cursor if it moved
if (portion != meal[x][2]) {
meal[x][2] = portion;
}
return portion;
}
int encoderCalibratePortion(int CP) { //ajustar calibrated portion
int encoder_move = read_encoder(); //determines if the rotary encoder has moved
if (encoder_move == 1) {
CP= CP+10; //Incrementa de 10 em 10
} else if (encoder_move == -1) {
CP= CP-10;
}
CP = constrain(CP, 0, 1000);
// Only print cursor if it moved
if (CP != calibratedaux) {
calibratedaux = CP;
}
return CP;
}
// END ENCODER
// SOM
int encoderChangeVolume(int volume) { //ajustar volume
int encoder_move = read_encoder(); //determines if the rotary encoder has moved
if (encoder_move == 1) {
volume++;
} else if (encoder_move == -1) {
volume--;
}
volume = constrain(volume, 0, 30);
// Only print cursor if it moved
if (volume != lastVolume) {
mp3.setVolume(volume);
lastVolume = volume;
}
return volume;
}
// END SOM
//MOTOR
void stepper(float Screw_turns, int motorpin, bool direction) {
int speed = 830; //NEEDS TO BE CALIBRATED !!!
int full_turn = 4000; // A full turn of the motor corresponds to 4000 steps
int steps = Screw_turns * full_turn; // get how many steps
digitalWrite(Enable, LOW); // Enables the motor driver
digitalWrite(dirPin, direction); // Sets the direction of motor rotation
for (int x = 0; x < steps; x++) { // Loop for the given number of steps
digitalWrite(motorpin, HIGH);
delayMicroseconds(speed);
digitalWrite(motorpin, LOW);
delayMicroseconds(speed);
}
digitalWrite(Enable, HIGH); // Disables the motor driver after steps are completed
}
void feed(int cal, int amount, bool slow) {
float turns;
turns = 50 / cal; // to get how many turns to get 50g
for (int i = 0; i <= amount; i = i + 50) {
stepper(turns, step, 0); //1 turn = 4000 steps = 103g
stepper(0.2, step, 1); //spins backwards chug control
if (slow) {
delay(1000);
}
}
}
//END MOTOR
//TEMPHUMIDITY
void readDHTData(float& temperature, float& humidity) { // Reads temperature and humidity
int readData = DHT.read22(dataPin);
temperature = DHT.temperature;
humidity = DHT.humidity;
}
//END TEMPHUMIDITY
/*################### END AUX FUNCTIONS #####################################
#############################################################################*/
/*###########################################################################
###################### LOOP #################################################*/
void loop() {
// ENCODER
static int click_last = HIGH;
click = digitalRead(switchPin); // Read the state of the switch
// RTC
rtc.refresh();
// Convert the RTC values to strings and store them in the auxtime array
currentSec = rtc.second();
currentMin = rtc.minute();
currentHour = rtc.hour();
currentDay = rtc.day();
currentMonth = rtc.month();
currentYear = rtc.year();
// TEMPHUMIDITY
readDHTData(temperature, humidity);
// Calculates next meal time
// It looks through the scheduled meal times and selects the one that is next in line
static int proximoHorario = -1;
int horaatual = (currentHour*100)+currentMin;
int meal1 = (meal[0][0]*100)+meal[0][1];
int meal2 = (meal[1][0]*100)+meal[1][1];
int mealtimes[2]={ meal1 , meal2 };
for (int horario : mealtimes) {
if (horario > horaatual) {
proximoHorario = horario;
break;
}
}
if (proximoHorario != -1) {
nextmeal = proximoHorario;
} else {
nextmeal = mealtimes[0];
}
nexthour = nextmeal/100;
nextmin = nextmeal%100;
// SWITCH SCREENS
// Checks the state variable 'st' and updates the display to behave accordingly
switch (st) {
case 0: // main screen
if (refresh_screen) {
// If the screen needs refreshing, call the Home_screen function to display the main screen
Home_screen();
refresh_screen = false; // Only refresh the screen once per state change
timer = 0;
digitalWrite(Enable, HIGH);
// If it's meal time, trigger the feed function to dispense food
if (meal[0][0] == currentHour && meal[0][1] == currentMin) {
feed(calibrated_value, meal[0][2], 0);
Treat();
for (int i = 0; i <=2; i++) {
mp3.playWithVolume(ss, lastVolume); //PLAY SONG
delay(3000);
}
}
if (meal[1][0] == currentHour && meal[1][1] == currentMin) {
feed(calibrated_value, meal[1][2], 0);
Treat();
for (int i = 0; i <=2; i++) {
mp3.playWithVolume(ss, lastVolume); //PLAY SONG
delay(3000);
}
}
}
if (click == LOW) { // If button was pressed, transition to the settings screen
st = 1;
refresh_screen = true;
delay(200);
} else {
// If there's no activity for some time, refresh the screen
if (timer >= TIMEOUT_COUNT) {
st = 0;
refresh_screen = true;
}
}
break;
case 1: // settings screen, where you can select Treat, Schedule, Calibrate and Sound
if (refresh_screen) {
Settings_screen();
refresh_screen = false;
timer = 0;
}
line = Cursor_nav();
if (click == LOW && line == 0) { // If button was pressed while 'Treat' was selected, transition to the treat screen
st = 18;
refresh_screen = true;
delay(200);
}
if (click == LOW && line == 1) { // schedule
st = 2;
refresh_screen = true;
delay(200);
}
if (click == LOW && line == 2) { //calibration
st = 11;
refresh_screen = true;
delay(200);
}
if (click == LOW && line == 3) { // Sound
st = 15;
refresh_screen = true;
delay(200);
}
else {
if (timer >= TIMEOUT_COUNT) {
st = 0;
refresh_screen = true;
}
// delay(200);
}
break;
case 2: // schedule screen, where you can select the scheduled hours to edit or remove
if (refresh_screen) {
Schedule_screen();
refresh_screen = false;
timer = 0;
}
line = Cursor_nav();
if (click == LOW && line == 3) { //BACK
st = 1;
refresh_screen = true;
delay(200);
}
if (click == LOW && line == 1) { //SET THE X TO 0, EDIT
st = 3;
x = 0;
refresh_screen = true;
delay(200);
}
if (click == LOW && line == 2) { //SET THE X TO 1, EDIT
st = 3;
x = 1;
refresh_screen = true;
delay(200);
} else {
if (timer >= TIMEOUT_COUNT) {
st = 0;
refresh_screen = true;
}
}
break;
case 3: // schedule options (edit/remove?)
if (refresh_screen) {
Schedule_options();
refresh_screen = false;
timer = 0;
}
line = Cursor_nav();
if (click == LOW && line == 1) { // edit
st = 4;
refresh_screen = true;
delay(200);
}
if (click == LOW && line == 2) { //back
st = 2;
refresh_screen = true;
delay(200);
}
if (click == LOW && line == 0) { //Remove
st = 9;
refresh_screen = true;
delay(200);
} else {
if (timer >= TIMEOUT_COUNT) {
st = 0;
refresh_screen = true;
}
}
break;
case 4: // EDIT
if (refresh_screen) {
Schedule_time_set();
refresh_screen = false;
timer = 0;
}
line = Cursor_nav();
if (click == LOW && line == 0) { //SET TIME
st = 5;
refresh_screen = true;
delay(200);
}
if (click == LOW && line == 1) { //SET PORTION
st = 7;
refresh_screen = true;
delay(200);
}
if (click == LOW && line == 2) { //SLOW MODE
st = 8;
refresh_screen = true;
delay(200);
}
if (click == LOW && line == 3) { // back
st = 2;
refresh_screen = true;
delay(200);
} else {
if (timer >= TIMEOUT_COUNT) {
st = 0;
refresh_screen = true;
}
}
break;
case 5: // time set hour
static int lastPrintedHour = -1;
if (refresh_screen) {
Time_set();
refresh_screen = false;
timer = 0;
}
meal[x][0] = encoderChangehour(meal[x][0]);
if (meal[x][0] != lastPrintedHour) {
lcd.setCursor(2, 2);
lcd.print(" "); // To clear previous value
lcd.setCursor(2, 2);
lcd.print(meal[x][0]);
lastPrintedHour = meal[x][0];
}
if (click == LOW) {
st = 6;
refresh_screen = true; // GO TO MINUTES
delay(200);
} else {
if (timer >= TIMEOUT_COUNT) {
st = 0;
refresh_screen = true;
}
}
break;
case 6: // time set min
static int lastPrintedMinute = -1;
if (refresh_screen) {
Time_set_min();
refresh_screen = false;
timer = 0;
}
meal[x][1] = encoderChangeminute(meal[x][1]);
if (meal[x][1] != lastPrintedMinute) {
lcd.setCursor(2, 2);
lcd.print(" "); // To clear previous value
lcd.setCursor(2, 2);
lcd.print(meal[x][1]);
lastPrintedMinute = meal[x][1];
}
if (click == LOW) {
st = 4; //GO BACK TO EDIT
refresh_screen = true;
delay(200);
} else {
if (timer >= TIMEOUT_COUNT) {
st = 0;
refresh_screen = true;
}
}
break;
case 7: // portion set
static int lastPrintedPortion = -1;
if (refresh_screen) {
Portion_set();
refresh_screen = false;
timer = 0;
}
meal[x][2] = encoderChangeportion(meal[x][2]);
if (meal[x][2] != lastPrintedPortion) {
lcd.setCursor(2, 2);
lcd.print(" "); // To clear previous value
lcd.setCursor(2, 2);
lcd.print(meal[x][2]);
lastPrintedPortion = meal[x][2];
}
if (click == LOW) {
st = 4;
refresh_screen = true; //GO BACK TO EDIT
delay(200);
} else {
if (timer >= TIMEOUT_COUNT) {
st = 0;
refresh_screen = true;
}
}
break;
case 8: // slow mode
if (refresh_screen) {
Slow_Mode();
refresh_screen = false;
timer = 0;
}
line = Cursor_nav();
if (click == LOW && line == 1) { //ON
meal[x][3] = 1;
refresh_screen = true;
delay(200);
}
if (click == LOW && line == 2) { //OFF
meal[x][3] = 0;
refresh_screen = true;
delay(200);
}
if (click == LOW && line == 3) { //BACK
st = 4;
refresh_screen = true;
delay(200);
} else {
if (timer >= TIMEOUT_COUNT) {
st = 0;
refresh_screen = true;
}
}
break;
case 9: // schedule remove
if (refresh_screen) {
Schedule_remove(); //WANT TO REMOVE THE SCHEDULE?
refresh_screen = false;
timer = 0;
}
line = Cursor_nav();
if (click == LOW && line == 3) {
st = 3; //BACK TO SCHEDULE OPTIONS
refresh_screen = true;
delay(200);
}
if (click == LOW && line == 1 || click == LOW && line == 2) {
st = 10; //ERROR SCREEN
refresh_screen = true;
delay(200);
} else {
if (timer >= TIMEOUT_COUNT) {
st = 0;
refresh_screen = true;
}
}
break;
case 10: // Not found o_o
if (refresh_screen) {
Not_found();
refresh_screen = false;
timer = 0;
}
if (click == LOW) {
st = 9;
refresh_screen = true;
delay(200);
} else {
if (timer >= TIMEOUT_COUNT) {
st = 0;
refresh_screen = true;
}
}
break;
case 11: // CALIBRATE
if (refresh_screen) {
Calibrate();
refresh_screen = false;
timer = 0;
}
line = Cursor_nav();
if (click == LOW && line == 3) { //OPTIONS SCREEN
st = 1;
refresh_screen = true;
delay(200);
}
if (click == LOW && line == 2) { //CALIBRATE DROP
st = 12;
refresh_screen = true;
delay(200);
} else {
if (timer >= TIMEOUT_COUNT) {
st = 0;
refresh_screen = true;
}
}
break;
case 12: // Calibrate_Droppingfood
if (refresh_screen) {
Calibrate_DF();
refresh_screen = false;
}
for (int i = 0; i <= 3; i++) { //STEPPER MOTOR
stepper(1, 3, 1); //1 turn = 4000 steps
stepper(0.2, 3, 0);
}
st = 13; //INSERT CALIBRATED PORTION
refresh_screen = true;
delay(200);
break;
case 13: // Calibrate_ INSERT CALIBRATED PORTION
static int lastPrintedCP = -1;
if (refresh_screen) {
Calibrate_TFO();
refresh_screen = false;
timer = 0;
}
calibratedaux = encoderCalibratePortion(calibratedaux);
if (calibratedaux != lastPrintedCP) {
lcd.setCursor(2, 2);
lcd.print(" "); // To clear previous value
lcd.setCursor(2, 2);
lcd.print(calibratedaux);
lastPrintedCP = calibratedaux;
}
calibrated_value = (calibratedaux) / 3; // 1 turn = 103g
if (click == LOW) {
st = 14;
refresh_screen = true;
delay(200);
}
break;
case 14: // CalibrateD
if (refresh_screen) {
CalibrateD();
refresh_screen = false;
timer = 0;
}
if (click == LOW) {
st = 0;
refresh_screen = true;
delay(200);
} else {
if (timer >= TIMEOUT_COUNT) {
st = 0;
refresh_screen = true;
}
}
break;
case 15: // Sound
if (refresh_screen) {
Sound();
refresh_screen = false;
timer = 0;
}
line = Cursor_nav();
if (click == LOW && line == 1) { //CHOOSE SOUND
st = 16;
refresh_screen = true;
delay(200);
}
if (click == LOW && line == 2) { //CHOOSE VOLUME
st = 17;
refresh_screen = true;
delay(200);
}
if (click == LOW && line == 3) { //BACK TO OPTIONS
st = 1;
refresh_screen = true;
delay(200);
} else {
if (timer >= TIMEOUT_COUNT) {
st = 0;
refresh_screen = true;
}
}
break;
case 16: // Sound_choose
if (refresh_screen) {
Sound_choose();
refresh_screen = false;
timer = 0;
}
line = Cursor_nav();
if (click == LOW && line == 1) { //BIRD MUSIC
mp3.playWithVolume(musica1, lastVolume);
ss = musica1;
refresh_screen = true;
delay(200);
}
if (click == LOW && line == 2) { //BELL MUSIC
mp3.playWithVolume(musica2, lastVolume);
ss = musica2;
refresh_screen = true;
delay(200);
}
if (click == LOW && line == 3) { //BACK
st = 15;
refresh_screen = true;
mp3.stopPlay();
delay(200);
} else {
if (timer >= TIMEOUT_COUNT) {
st = 0;
refresh_screen = true;
}
}
break;
case 17: // Sound_volume
static int lastPrintedVolume = -1;
if (refresh_screen) {
Sound_volume();
refresh_screen = false;
timer = 0;
// delay(10);
}
lastVolume = encoderChangeVolume(lastVolume);
if (lastVolume != lastPrintedVolume) { //CHANGE VOLUME WHEN ROTARY ENCODER MOVES
lcd.setCursor(7, 2);
lcd.print(" "); // To clear previous value
lcd.setCursor(7, 2);
lcd.print(lastVolume);
lastPrintedVolume = lastVolume;
}
if (click == LOW) { //BACK
st = 15;
refresh_screen = true;
delay(200);
} else {
if (timer >= TIMEOUT_COUNT) {
st = 0;
refresh_screen = true;
}
}
break;
case 18: // Treat
if (refresh_screen) {
Treat();
refresh_screen = false;
stepper(1, 3, 1); //STEPPER
for (int i = 0; i <=2; i++) {
mp3.playWithVolume(ss, lastVolume); //PLAY SONG
delay(3000);
}
timer = 0;
}
if (click == LOW) {
st = 0;
refresh_screen = true;
delay(200);
}
else {
if (timer >= TIMEOUT_COUNT) {
st = 0;
refresh_screen = true;
}
}
break;
// case 19:
// if (refresh_screen) {
// Treat();
// refresh_screen = false;
// for (int i = 0; i <=2; i++) {
// mp3.playWithVolume(ss, lastVolume); //PLAY SONG
// delay(3000);
// }
// timer = 0;
// }
// else {