-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWorking_Code.ino
1257 lines (1134 loc) · 30.6 KB
/
Working_Code.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
//####### SMALL STEPPER #######
#include <Stepper.h>
#define STEPS_PER_MOTOR_REVOLUTION 32
Stepper small_stepper(STEPS_PER_MOTOR_REVOLUTION, 42, 44, 43, 45);
int Steps2Take;
//####### LCD #######
#include <LiquidCrystal_I2C.h>
#include <Wire.h>
LiquidCrystal_I2C lcd(0x3F,16,2, LCD_5x8DOTS); // 0x27 is the I2C bus address for an unmodified backpack
//####### WATER TEMP #######
#include <DallasTemperature.h>
#include <OneWire.h>
#define ONE_WIRE_BUS 34
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
//####### RGB #######
#define green_pin 6
#define red_pin 7
#define blue_pin 5
int r, g, b;
int colour_count = 4; //default to purple
int brightness_count = 5; //default to brightest
//int water_test = 0; //might not need | PLS REVIEW
//int drain_refill_count = 0; //might not need | PLS REVIEW
//####### WATER LEVEL SENSOR #######
#define water_level_sensor_bottom 40 // yellow | black wire
#define water_level_sensor_top 41 // red | brown wire
int buttonState_bottom = 1; // variable for reading the pushbutton status
int buttonState_top = 1; // variable for reading the pushbutton status
//####### AIR HUM AND TEMP #######
#include <Adafruit_Sensor.h>
#include <DHT.h>
#include <DHT_U.h>
#define DHTPIN 35
#define DHTTYPE DHT11
DHT_Unified dht(DHTPIN, DHTTYPE);
//####### VALVE #######
#define valve 23
//####### FAN #######
#define fan 22
//####### PUMPS #######
#define testpump 24
#define mainpump 25
//####### MOTOR #######
#define motor 26
//####### TAKE PICTURE #######
#include <Adafruit_VC0706.h>
#include <SPI.h>
#include <SD.h>
#define SD_CS 53 //define Slave Select pin
Adafruit_VC0706 cam = Adafruit_VC0706(&Serial2);
int i; //to loop for setting resolution
//####### DECODE JPEG #######
#include <JPEGDecoder.h>
int looper;
char pic_name[12];
int five_counter = 0; //to increase pic_name[5]
int six_counter = 0; //to increase pic_name[6]
//####### SD | STORE DATA #######
File myFile;
//####### XY MOVEMENT #######
#define x_step 39
#define x_dir 38 //LOW = towards button | HIGH = away from button
#define y_step 37
#define y_dir 36 //LOW = towards button | HIGH = away from button
#define xhome 49
#define yhome 48
int buttonState_xhome = 1; //read for xhome button press
int buttonState_yhome = 1; //read for yhome button press
int x_release; //to release x button
int y_release; //to release y button
int x_now; //current position for x-axis
int y_now; //current position for y-axis
//####### BUTTONS & INTERRUPTS #######
#define interrupt_a 2
#define interrupt_b 3
#define interrupt_c 18
#define interrupt_d 19
int lcd_A = 0; //colour
int lcd_B = 0; //brightness
int lcd_C = 0; //take picture + test water + decode JPEG
int lcd_D = 0; //drain and refill
volatile int interrupt_1 = 0;
volatile int interrupt_2 = 0;
volatile int interrupt_3 = 0;
volatile int interrupt_4 = 0;
unsigned long button_time_A = 0;
unsigned long last_button_time_A = 0;
unsigned long button_time_B = 0;
unsigned long last_button_time_B = 0;
unsigned long button_time_C = 0;
unsigned long last_button_time_C = 0;
unsigned long button_time_D = 0;
unsigned long last_button_time_D = 0;
//####### TIMER #######
unsigned long button_time_E = 0; //for data logging
unsigned long last_button_time_E = 0;
unsigned long button_time_F = 0; //for changing water
unsigned long last_button_time_F = 0;
unsigned long button_time_G = 0; //for taking photos + water test
unsigned long last_button_time_G = 0;
unsigned long button_time_H = 0; //for checking humidity and temperature
unsigned long last_button_time_H = 0;
void setup() {
Serial.begin(115200);
//####### LCD #######
lcd.begin(); // for 16 x 2 LCD module
lcd.clear();
//####### WATER TEMP #######
sensors.begin();
//####### HUM TEMP #######
dht.begin();
//####### RGB #######
pinMode(red_pin, OUTPUT);
pinMode(green_pin, OUTPUT);
pinMode(blue_pin, OUTPUT);
//####### BUTTONS AND INTERRUPTS #######
pinMode(interrupt_a, INPUT);
pinMode(interrupt_b, INPUT);
pinMode(interrupt_c, INPUT);
pinMode(interrupt_d, INPUT);
attachInterrupt(digitalPinToInterrupt(2), inc_colour_count, RISING);
attachInterrupt(digitalPinToInterrupt(3), brightness_up, RISING);
attachInterrupt(digitalPinToInterrupt(18), take_picture, RISING);
attachInterrupt(digitalPinToInterrupt(19), drain_refill, RISING);
//####### WATER LEVEL SENSOR #######
pinMode(water_level_sensor_bottom, INPUT);
pinMode(water_level_sensor_top, INPUT);
//####### VALVE #######
pinMode(valve, OUTPUT);
digitalWrite(valve, HIGH); //low - turn on relay | high - turn off;
//####### FAN #######
pinMode(fan, OUTPUT);
digitalWrite(fan, HIGH); //low - turn on relay | high - turn off;
//####### PUMPS #######
pinMode(mainpump, OUTPUT);
digitalWrite(mainpump, HIGH); //low - turn on relay | high - turn off;
pinMode(testpump, OUTPUT);
digitalWrite(testpump, HIGH); //low - turn on relay | high - turn off;
//####### MOTOR #######
pinMode(motor, OUTPUT);
digitalWrite(motor, HIGH); //low - turn on relay | high - turn off;
//####### DECODE JPEG #######
pinMode(13, OUTPUT); //SPI might hang if not set
// Start the SD
if (!SD.begin(SD_CS)) {
// If the SD can't be started, loop forever
Serial.println("SD failed or not present!");
while (1);
}
//####### XY MOVEMENT #######
pinMode(x_step, OUTPUT);
pinMode(x_dir, OUTPUT);
pinMode(y_step, OUTPUT);
pinMode(y_dir, OUTPUT);
pinMode(xhome, INPUT);
pinMode(yhome, INPUT);
digitalWrite(motor, LOW);
xhome_go(); //calibrate x-axis to home position
yhome_go(); //calibrate y-axis to home position
digitalWrite(motor, HIGH);
lcd_A = 1; //initialise to show "Colour"
}
void loop() {
button_time_E = millis();
button_time_F = millis();
button_time_G = millis();
button_time_H = millis();
if (button_time_E - last_button_time_E > 600000) //10 minutes
{
// open the file. note that only one file can be open at a time,
// so you have to close this one before opening another.
myFile = SD.open("DATALOG.txt", FILE_WRITE);
// if the file opened okay, write to it:
if (myFile) {
sensors_event_t event;
myFile.print("Humidity;");
dht.humidity().getEvent(&event);
myFile.print(event.relative_humidity);
myFile.print("; Temp;");
dht.temperature().getEvent(&event);
myFile.print(event.temperature);
myFile.print("; Water_temperature;");
sensors.requestTemperatures();
myFile.print(sensors.getTempCByIndex(0));
myFile.print('\n');
// close the file:
myFile.close();
// Serial.println("done.");
} else {
// if the file didn't open, print an error:
Serial.println("error opening DATALOG.txt");
}
last_button_time_E = button_time_E;
}
if (button_time_F - last_button_time_F > 25920000) //3 days
{
buttonState_bottom = digitalRead(water_level_sensor_bottom);
buttonState_top = digitalRead(water_level_sensor_top);
if(buttonState_bottom == HIGH && buttonState_top == HIGH) //both sensors at bottom
{
lcd.setCursor(0,0);
lcd.print("Refilling water ");
lcd.setCursor(0,1);
lcd.print(" ");
//no need to open valve since water level is at minimum
while(buttonState_top != LOW) //keep refilling until TOP is afloat
{
buttonState_top = digitalRead(water_level_sensor_top);
digitalWrite(mainpump, LOW); //turn on main pump
}
digitalWrite(mainpump, HIGH); //turn off main pump
}
else //if((buttonState_bottom == LOW && buttonState_top == HIGH) || (buttonState_bottom == LOW && buttonState_top == LOW))
{
//Step 1. Drain water
lcd.setCursor(0,0);
lcd.print("Draining water ");
lcd.setCursor(0,1);
lcd.print(" ");
while(buttonState_bottom != HIGH)
{
buttonState_bottom = digitalRead(water_level_sensor_bottom);
digitalWrite(valve, LOW); //open valve as long as there is still water
}
digitalWrite(valve, HIGH); //allow valve to close
//Step 2. Refill water
lcd.setCursor(0,0);
lcd.print("Refilling water ");
lcd.setCursor(0,1);
lcd.print(" ");
buttonState_top = digitalRead(water_level_sensor_top); //need to update cuz last saved State was LOW (afloat)
while(buttonState_top != LOW)
{
buttonState_top = digitalRead(water_level_sensor_top);
digitalWrite(mainpump, LOW);
}
digitalWrite(mainpump, HIGH);
}
last_button_time_F = button_time_F;
}
if (button_time_G - last_button_time_G > 3600000) //1 hour
{
//####### TAKE PICTURE #######
lcd.setCursor(0,0);
lcd.print("Moving to Pot 1 ");
lcd.setCursor(0,1);
lcd.print(" ");
pot1_go();
lcd.setCursor(0,0);
lcd.print("Taking picture ");
take_pic();
lcd.setCursor(0,1);
lcd.print("Done! ");
delay(2000);
lcd.setCursor(0,0);
lcd.print("Moving to Pot 2 ");
lcd.setCursor(0,1);
lcd.print(" ");
pot2_go();
lcd.setCursor(0,0);
lcd.print("Taking picture ");
take_pic();
lcd.setCursor(0,1);
lcd.print("Done! ");
delay(2000);
lcd.setCursor(0,0);
lcd.print("Moving to Pot 3 ");
lcd.setCursor(0,1);
lcd.print(" ");
pot3_go();
lcd.setCursor(0,0);
lcd.print("Taking picture ");
take_pic();
lcd.setCursor(0,1);
lcd.print("Done! ");
delay(2000);
lcd.setCursor(0,0);
lcd.print("Moving to Pot 4 ");
lcd.setCursor(0,1);
lcd.print(" ");
pot4_go();
lcd.setCursor(0,0);
lcd.print("Taking picture ");
take_pic();
lcd.setCursor(0,1);
lcd.print("Done! ");
delay(2000);
lcd.setCursor(0,0);
lcd.print("Moving to Pot 5 ");
lcd.setCursor(0,1);
lcd.print(" ");
pot5_go();
lcd.setCursor(0,0);
lcd.print("Taking picture ");
take_pic();
lcd.setCursor(0,1);
lcd.print("Done! ");
delay(2000);
lcd.setCursor(0,0);
lcd.print("Moving to Pot 6 ");
lcd.setCursor(0,1);
lcd.print(" ");
pot6_go();
lcd.setCursor(0,0);
lcd.print("Taking picture ");
take_pic();
lcd.setCursor(0,1);
lcd.print("Done! ");
delay(2000);
lcd.setCursor(0,0);
lcd.print("Moving to Test ");
lcd.setCursor(0,1);
lcd.print(" ");
testarea_go();
hold_test_water();
digitalWrite(testpump, LOW);
delay(3000);
digitalWrite(testpump, HIGH);
lcd.setCursor(0,0);
lcd.print("Taking picture ");
take_pic();
lcd.setCursor(0,1);
lcd.print("Done! ");
dump_test_water();
delay(2000);
lcd.setCursor(0,0);
lcd.print("Sending pictures");
lcd.setCursor(0,1);
lcd.print(" ");
for(looper = 0; looper < 7; looper ++)
{
// Open the root directory
File root = SD.open("/");
strcpy(pic_name, "IMAGE00.JPG");
if(six_counter > 9)
{
five_counter = five_counter + 1;
six_counter = 0;
}
six_counter = six_counter + 1;
setpic_name();
}
digitalWrite(motor, LOW);
xhome_go();
yhome_go();
digitalWrite(motor, HIGH);
last_button_time_G = button_time_G;
}
if (button_time_H - last_button_time_H > 300000) // 5 minutes
{
sensors_event_t event;
lcd.setCursor(0,0);
lcd.print("Air temp | ");
lcd.setCursor(11,0);
dht.temperature().getEvent(&event);
lcd.print(event.temperature);
Serial.print(event.temperature);
lcd.setCursor(0,1);
lcd.print("Humidity | ");
lcd.setCursor(11,1);
dht.humidity().getEvent(&event);
lcd.print(event.relative_humidity);
Serial.print(event.relative_humidity);
dht.humidity().getEvent(&event);
while(event.relative_humidity > 90)
{
digitalWrite(fan, LOW); //turn on the fan
dht.humidity().getEvent(&event);
}
dht.temperature().getEvent(&event);
while(event.temperature > 30)
{
digitalWrite(fan, LOW);
dht.temperature().getEvent(&event);
}
digitalWrite(fan, HIGH);
last_button_time_H = button_time_H;
}
if (lcd_A == 1)
{
switch(colour_count)
{
case 1:
lcd.setCursor(0,0);
lcd.print("COLOUR ");
lcd.setCursor(0,1);
lcd.print(" < RED > ");
display_colour();
break;
case 2:
lcd.setCursor(0,0);
lcd.print("COLOUR ");
lcd.setCursor(0,1);
lcd.print(" < BLUE > ");
display_colour();
break;
case 3:
lcd.setCursor(0,0);
lcd.print("COLOUR ");
lcd.setCursor(0,1);
lcd.print(" < GREEN > ");
display_colour();
break;
case 4:
lcd.setCursor(0,0);
lcd.print("COLOUR ");
lcd.setCursor(0,1);
lcd.print(" < PURPLE > ");
display_colour();
break;
}
lcd_A = 0;
}
else if (lcd_B == 1)
{
switch(brightness_count)
{
case 0:
lcd.setCursor(0,0);
lcd.print("BRIGHTNESS ");
lcd.setCursor(0,1);
lcd.print(" < 0 > ");
display_colour();
break;
case 1:
lcd.setCursor(0,0);
lcd.print("BRIGHTNESS ");
lcd.setCursor(0,1);
lcd.print(" < 1 > ");
display_colour();
break;
case 2:
lcd.setCursor(0,0);
lcd.print("BRIGHTNESS ");
lcd.setCursor(0,1);
lcd.print(" < 2 > ");
display_colour();
break;
case 3:
lcd.setCursor(0,0);
lcd.print("BRIGHTNESS ");
lcd.setCursor(0,1);
lcd.print(" < 3 > ");
display_colour();
break;
case 4:
lcd.setCursor(0,0);
lcd.print("BRIGHTNESS ");
lcd.setCursor(0,1);
lcd.print(" < 4 > ");
display_colour();
break;
case 5:
lcd.setCursor(0,0);
lcd.print("BRIGHTNESS ");
lcd.setCursor(0,1);
lcd.print(" < 5 > ");
display_colour();
break;
}
lcd_B = 0;
}
else if (lcd_C == 1)
{
//####### TAKE PICTURE #######
lcd.setCursor(0,0);
lcd.print("Moving to Pot 1 ");
lcd.setCursor(0,1);
lcd.print(" ");
pot1_go();
lcd.setCursor(0,0);
lcd.print("Taking picture ");
take_pic();
lcd.setCursor(0,1);
lcd.print("Done! ");
delay(2000);
lcd.setCursor(0,0);
lcd.print("Moving to Pot 2 ");
lcd.setCursor(0,1);
lcd.print(" ");
pot2_go();
lcd.setCursor(0,0);
lcd.print("Taking picture ");
take_pic();
lcd.setCursor(0,1);
lcd.print("Done! ");
delay(2000);
/* lcd.setCursor(0,0);
lcd.print("Moving to Pot 3 ");
lcd.setCursor(0,1);
lcd.print(" ");
pot3_go();
lcd.setCursor(0,0);
lcd.print("Taking picture ");
take_pic();
lcd.setCursor(0,1);
lcd.print("Done! ");
delay(2000);
lcd.setCursor(0,0);
lcd.print("Moving to Pot 4 ");
lcd.setCursor(0,1);
lcd.print(" ");
pot4_go();
lcd.setCursor(0,0);
lcd.print("Taking picture ");
take_pic();
lcd.setCursor(0,1);
lcd.print("Done! ");
delay(2000);
lcd.setCursor(0,0);
lcd.print("Moving to Pot 5 ");
lcd.setCursor(0,1);
lcd.print(" ");
pot5_go();
lcd.setCursor(0,0);
lcd.print("Taking picture ");
take_pic();
lcd.setCursor(0,1);
lcd.print("Done! ");
delay(2000);
lcd.setCursor(0,0);
lcd.print("Moving to Pot 6 ");
lcd.setCursor(0,1);
lcd.print(" ");
pot6_go();
lcd.setCursor(0,0);
lcd.print("Taking picture ");
take_pic();
lcd.setCursor(0,1);
lcd.print("Done! ");
delay(2000);
lcd.setCursor(0,0);
lcd.print("Moving to Test ");
lcd.setCursor(0,1);
lcd.print(" ");
testarea_go();
*/ hold_test_water();
digitalWrite(testpump, LOW);
delay(3000);
digitalWrite(testpump, HIGH);
lcd.setCursor(0,0);
lcd.print("Taking picture ");
// take_pic();
lcd.setCursor(0,1);
lcd.print("Done! ");
delay(2000);
dump_test_water();
delay(2000);
lcd.setCursor(0,0);
lcd.print("Sending pictures");
lcd.setCursor(0,1);
lcd.print(" ");
for(looper = 0; looper < 2; looper ++)
{
// Wait for the PC to signal
//while(!Serial.available());
// if(looper == 0)
// strcpy(pic_name, "IMAGE02.JPG");
// else
// strcpy(pic_name, "IMAGE03.JPG");
strcpy(pic_name, "IMAGE00.JPG");
if(six_counter > 9)
{
five_counter = five_counter + 1;
six_counter = 0;
}
setpic_name();
// Open the root directory
File root = SD.open("/");
File jpgFile = SD.open(pic_name);
// Decode the JPEG file
JpegDec.decodeSdFile(pic_name);
// Create a buffer for the packet
char dataBuff[240];
// Fill the buffer with zeros
initBuff(dataBuff);
// Create a header packet with info about the image
String header = "$ITHDR,";
header += JpegDec.width;
header += ",";
header += JpegDec.height;
header += ",";
header += JpegDec.MCUSPerRow;
header += ",";
header += JpegDec.MCUSPerCol;
header += ",";
header += jpgFile.name();
header += ",";
header.toCharArray(dataBuff, 240);
// Send the header packet
for(int j=0; j<240; j++) {
Serial.write(dataBuff[j]);
}
// Pointer to the current pixel
uint16_t *pImg;
// Color of the current pixel
uint16_t color;
// Create a data packet with the actual pixel colors
strcpy(dataBuff, "$ITDAT");
uint8_t i = 6;
// Repeat for all MCUs in the image
while(JpegDec.read()) {
// Save pointer the current pixel
pImg = JpegDec.pImage;
// Get the coordinates of the MCU we are currently processing
int mcuXCoord = JpegDec.MCUx;
int mcuYCoord = JpegDec.MCUy;
// Get the number of pixels in the current MCU
uint32_t mcuPixels = JpegDec.MCUWidth * JpegDec.MCUHeight;
// Repeat for all pixels in the current MCU
while(mcuPixels--) {
// Read the color of the pixel as 16-bit integer
color = *pImg++;
// Split it into two 8-bit integers
dataBuff[i] = color >> 8;
dataBuff[i+1] = color;
i += 2;
// If the packet is full, send it
if(i == 240) {
for(int j=0; j<240; j++) {
Serial.write(dataBuff[j]);
}
i = 6;
}
// If we reach the end of the image, send a packet
if((mcuXCoord == JpegDec.MCUSPerRow - 1) &&
(mcuYCoord == JpegDec.MCUSPerCol - 1) &&
(mcuPixels == 1)) {
// Send the pixel values
for(int j=0; j<i; j++) {
Serial.write(dataBuff[j]);
}
// Fill the rest of the packet with zeros
for(int k=i; k<240; k++) {
Serial.write(0);
}
}
}
}
// Safely close the root directory
root.close();
six_counter = six_counter + 1;
}
colour_count = 4;
lcd_C = 0; //reset lcd_C
lcd_A = 1;
lcd_B = 0;
lcd_D = 0;
digitalWrite(motor, LOW);
xhome_go();
yhome_go();
digitalWrite(motor, HIGH);
}
else if (lcd_D == 1)
{
buttonState_bottom = digitalRead(water_level_sensor_bottom);
buttonState_top = digitalRead(water_level_sensor_top);
if(buttonState_bottom == HIGH && buttonState_top == HIGH) //both sensors at bottom
{
lcd.setCursor(0,0);
lcd.print("Refilling water ");
lcd.setCursor(0,1);
lcd.print(" ");
//no need to open valve since water level is at minimum
while(buttonState_top != LOW) //keep refilling until TOP is afloat
{
buttonState_top = digitalRead(water_level_sensor_top);
digitalWrite(mainpump, LOW); //turn on main pump
}
digitalWrite(mainpump, HIGH); //turn off main pump
}
else //if((buttonState_bottom == LOW && buttonState_top == HIGH) || (buttonState_bottom == LOW && buttonState_top == LOW))
{
//Step 1. Drain water
lcd.setCursor(0,0);
lcd.print("Draining water ");
lcd.setCursor(0,1);
lcd.print(" ");
while(buttonState_bottom != HIGH)
{
buttonState_bottom = digitalRead(water_level_sensor_bottom);
digitalWrite(valve, LOW); //open valve as long as there is still water
}
digitalWrite(valve, HIGH); //allow valve to close
//Step 2. Refill water
lcd.setCursor(0,0);
lcd.print("Refilling water ");
lcd.setCursor(0,1);
lcd.print(" ");
buttonState_top = digitalRead(water_level_sensor_top); //need to update cuz last saved State was LOW (afloat)
while(buttonState_top != LOW)
{
buttonState_top = digitalRead(water_level_sensor_top);
digitalWrite(mainpump, LOW);
}
digitalWrite(mainpump, HIGH);
}
colour_count = 4;
lcd_D = 0; //reset lcd_D
lcd_C = 0; //reset lcd_C
lcd_A = 1;
lcd_B = 0;
}
} //void loop
//####### SMALL STEPPER #######
void hold_test_water()
{
//add line here to turn off backlight? | PLS REVIEW
lcd.noBacklight();
Steps2Take = - 700;
small_stepper.setSpeed(500);
small_stepper.step(Steps2Take);
}
void dump_test_water()
{
Steps2Take = + 500;
small_stepper.setSpeed(500);
small_stepper.step(Steps2Take);
//add line here to turn on backlight? | PLS REVIEW
lcd.backlight();
}
//####### JPEG DECODE #######
// Function to fill the packet buffer with zeros
void initBuff(char* buff) {
for (int i = 0; i < 240; i++) {
buff[i] = 0;
}
}
void setpic_name()
{
switch (five_counter)
{
case 0:
pic_name[5] = '0';
break;
case 1:
pic_name[5] = '1';
break;
case 2:
pic_name[5] = '2';
break;
case 3:
pic_name[5] = '3';
break;
case 4:
pic_name[5] = '4';
break;
case 5:
pic_name[5] = '5';
break;
case 6:
pic_name[5] = '6';
break;
case 7:
pic_name[5] = '7';
break;
case 8:
pic_name[5] = '8';
break;
case 9:
pic_name[5] = '9';
break;
}
switch (six_counter)
{
case 0:
pic_name[6] = '0';
break;
case 1:
pic_name[6] = '1';
break;
case 2:
pic_name[6] = '2';
break;
case 3:
pic_name[6] = '3';
break;
case 4:
pic_name[6] = '4';
break;
case 5:
pic_name[6] = '5';
break;
case 6:
pic_name[6] = '6';
break;
case 7:
pic_name[6] = '7';
break;
case 8:
pic_name[6] = '8';
break;
case 9:
pic_name[6] = '9';
break;
}
}
//####### XY MOVEMENT #######
void pot1_go()
{
move_x(500);
move_y(0);
}
void pot2_go()
{
move_x(500);
move_y(1500);
}
void pot3_go()
{
move_x(2700);
move_y(1500);
}
void pot4_go()
{
move_x(2700);
move_y(0);
}
void pot5_go()
{
move_x(4900);
move_y(0);
}
void pot6_go()
{
move_x(4900);
move_y(1500);
}
void testarea_go()
{
move_x(7200);
move_y(850);
}
void yhome_go()
{
buttonState_yhome = digitalRead(yhome);
while (buttonState_yhome == LOW)
{
digitalWrite(y_dir, LOW); //this direction is towards button
buttonState_yhome = digitalRead(yhome);
digitalWrite(y_step, HIGH);
delay(10);
digitalWrite(y_step, LOW);
delay(10);
// Serial.println("Y returning home");
}
digitalWrite(y_step, LOW);
// Serial.println("Y reached home");
digitalWrite(y_dir, HIGH); //to change direction
for (y_release = 0; y_release < 40; y_release++)
{
digitalWrite(y_step, HIGH);
delay(10);
digitalWrite(y_step, LOW);
delay(10);
}
y_now = 0;
}
void xhome_go()
{
buttonState_xhome = digitalRead(xhome);
while (buttonState_xhome == LOW)
{
digitalWrite(x_dir, LOW); //this direction is towards button
buttonState_xhome = digitalRead(xhome);
digitalWrite(x_step, HIGH);
delay(10);
digitalWrite(x_step, LOW);
delay(10);
// Serial.println("X returning home");
}
digitalWrite(x_step, LOW);
// Serial.println("X reached home");
digitalWrite(x_dir, HIGH); //to change direction
for (x_release = 0; x_release < 40; x_release++)
{
digitalWrite(x_step, HIGH);
delay(10);
digitalWrite(x_step, LOW);
delay(10);
}
x_now = 0;
}
void move_x(int x_go)
{
int steps;
int x_distance;
x_distance = x_go - x_now;
if (x_distance <= 0)
{
digitalWrite(x_dir, LOW);
x_distance = - x_distance;
}
else
{
digitalWrite(x_dir, HIGH);
}
digitalWrite(motor, LOW);
for (steps = 0; steps < x_distance; steps++)
{
digitalWrite(x_step, HIGH);
delay(10);
digitalWrite(x_step, LOW);