-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdevicescript.ino
More file actions
180 lines (146 loc) · 3.34 KB
/
devicescript.ino
File metadata and controls
180 lines (146 loc) · 3.34 KB
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
#include <DHT.h>
#include <DHT_U.h>
//#include <ArduinoJson.h>
#define Type DHT11
#include <Servo.h>
//DHT sensor
//gnd-gnd
//data-
//vcc-5v
int sensePin = 2;
DHT HT(sensePin,Type);
float humidity;
float tempC;
float lowHumidity = 20;
float highHumidity = 60;
//diodes
int greenD = 4;
int redD = 7;
int blueD = 8;
//servo motor
//brown cable-grnd
//red- power 5V
//yellow/orange- data
int pos = 0;
Servo servo_9;
unsigned long lastSensorReadTime = 0;
//moisture sensor
//vcc -analogRead pin power
//gnd-gnd
//sig -analodRead pin data
#define analogPin A0
#define digitalPin 3
#define vccPin 12
unsigned long time = 0;
int lowMoisture;
int highMoisture;
int timeDelay;
void setup() {
Serial.begin(9600);
delay(500);
HT.begin();
delay(500);
servo_9.attach(9, 500, 2500);
//test if everything is all right
pinMode(greenD, OUTPUT);
pinMode(redD, OUTPUT);
pinMode(blueD, OUTPUT);
delay(500);
moveServo(80,true);
delay(500);
digitalWrite(greenD, HIGH);
digitalWrite(redD, HIGH);
digitalWrite(blueD, HIGH);
delay(1000);
digitalWrite(greenD, LOW);
digitalWrite(redD, LOW);
digitalWrite(blueD, LOW);
//test moisture
pinMode(analogPin, INPUT);
pinMode(digitalPin, INPUT);
pinMode(vccPin, OUTPUT);
digitalWrite(vccPin, LOW);
Serial.print("test");
}
void loop() {
humidity = HT.readHumidity();
tempC = HT.readTemperature();
Serial.print("Temperature:");
Serial.print(tempC);
Serial.println("C");
Serial.print("Humidity:");
Serial.print(humidity);
Serial.println("");
digitalWrite(greenD, LOW);
digitalWrite(redD, LOW);
digitalWrite(blueD, LOW);
if (humidity > highHumidity)
{
digitalWrite(redD,HIGH);
}
else if (humidity< lowHumidity)
{
digitalWrite(blueD, HIGH);
//watering flowers
if (millis() >= 86400000UL) { // 24 hours in milliseconds
moveServo(180, true);
resetMillis(); // Reset the millis counter
}
}
else
{
digitalWrite(greenD, HIGH);
}
Serial.print("Moisture: ");
moistureTest();
delay(1000);
}
void moveServo(int position , bool way)
{
if (way == true)
{
for (pos = 0; pos <= position; pos += 1) {
// tell servo to go to position in variable 'pos'
servo_9.write(pos);
// wait 15 ms for servo to reach the position
delay(15); // Wait for 15 millisecond(s)
}
for (position ; position >= 0;position-= 1) {
// tell servo to go to position in variable 'pos'
servo_9.write(pos);
// wait 15 ms for servo to reach the position
delay(15); // Wait for 15 millisecond(s)
}
}
else if (way == false)
{
for (position; position >= 0; pos -= 1) {
// tell servo to go to position in variable 'pos'
servo_9.write(pos);
// wait 15 ms for servo to reach the position
delay(15); // Wait for 15 millisecond(s)
}
}
}
void moistureTest()
{
if (millis() - time > 3000)
{
digitalWrite(vccPin, HIGH);
delay(100);
int analog = analogRead(analogPin);
bool digit = digitalRead(digitalPin);
Serial.print("Analogovy vstup: ");
Serial.print(analog);
if (digit == HIGH)
{
Serial.print(" | Detekovano prekroceni hranice!");
}
Serial.println();
digitalWrite(vccPin, LOW);
time = millis();
}
}
void resetMillis() {
asm volatile ("jmp 0"); // Resets the Arduino by jumping to the start of the program
}