-
Notifications
You must be signed in to change notification settings - Fork 0
/
finaluno.txt
137 lines (120 loc) · 2.81 KB
/
finaluno.txt
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
#include <SoftwareSerial.h>
#include <avr/sleep.h>
#include <avr/power.h>
#include <avr/wdt.h>
#define wlevel1 500
#define valve_PIN (4)
#define temp (analogRead(A1)*500)/1023
#define light analogRead(A0)//node level
#define water analogRead(A2)//node level
#define pH analogRead(A3)//computation at pi
volatile int f_wdt=1;
// Watchdog Interrupt Service. This is executed when watchdog timed out.
ISR(WDT_vect)
{
if(f_wdt == 0)
{
f_wdt=1;
}
else
{
Serial.println("WDT Overrun!!!;");
}
}
// Enters the arduino into sleep mode.
void enterSleep(void)
{
set_sleep_mode(SLEEP_MODE_PWR_DOWN); /* EDIT: could also use SLEEP_MODE_PWR_SAVE for higher power consumption. */
sleep_enable();
/* Now enter sleep mode. */
sleep_mode();
/* The program will continue from here after the WDT timeout*/
sleep_disable(); /* First thing to do is disable sleep. */
/* Re-enable the peripherals. */
power_all_enable();
}
// Setup for the serial comms and the Watch dog timeout.
void frame()
{
Serial.print(";N2;");
Serial.print("T");
Serial.print(temp);
Serial.print(";L");
Serial.print(light);
Serial.print(";M");
Serial.print(water);
Serial.print(";P");
Serial.print(pH);
Serial.print(";");
delay(500);
}
void setup()
{
Serial.begin(9600);
pinMode(valve_PIN,OUTPUT);
Serial.println("setup");
delay(1000);
digitalWrite(valve_PIN,LOW);
/*** Setup the WDT ***/
/* Clear the reset flag. */
MCUSR &= ~(1<<WDRF);
/* In order to change WDE or the prescaler, we need to
* set WDCE (This will allow updates for 4 clock cycles).
*/
WDTCSR |= (1<<WDCE) | (1<<WDE);
/* set new watchdog timeout prescaler value */
WDTCSR = 1<<WDP0 | 1<<WDP3; /* 8.0 seconds */
/* Enable the WD interrupt (note no reset). */
WDTCSR |= _BV(WDIE);
}
//int i,j,k;//ifor 20mins, j for 6 hrs
int i=0;
int r;
char c[9];
char wlevel[3];
void loop()
{
/* Don't forget to clear the flag. */
f_wdt = 0;
/* Re-enter sleep mode. */
enterSleep();
//Serial.print(i);
//Serial.println(". 8 secs");
//delay(1000);
//sleep_watchdog_8sec
if(i==1)//150
{
frame();
//delay(1000);
i=0;
if((c[0]=Serial.read())==';'){
for(r=1;r<8;r++){
c[r]=Serial.read();
}
if(c[1]=='N'&&c[2]=='2')
{
if(c[4]=='W' && c[5]!='0')
{
wlevel[0]=c[5];
wlevel[1]=c[6];
wlevel[2]=c[7];
Serial.print("hi");
delay(1000);
while(water>atoi(wlevel))
{
Serial.print(atoi(wlevel));
digitalWrite(valve_PIN,HIGH);
}
digitalWrite(valve_PIN,LOW);
}
}
}
while(water>wlevel1)
{
//Serial.print(atoi(wlevel));
digitalWrite(valve_PIN,HIGH);
}
digitalWrite(valve_PIN,LOW);
}
i++;
}