-
Notifications
You must be signed in to change notification settings - Fork 0
/
water_system_v0_2.ino
171 lines (131 loc) · 3.5 KB
/
water_system_v0_2.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
#include <SoftwareSerial.h>
//blalballlabaslbfds
//test2
#include <Wire.h>
#include <Adafruit_MotorShield.h>
#include "utility/Adafruit_MS_PWMServoDriver.h"
#include <Servo.h>
#include <Time.h>
#include <TimeAlarms.h>
const int buttonPin = 2; // the number of the pushbutton pin
int buttonState = 0; // variable for reading the pushbutton status
bool startup = true;
//servo setup
int servo_left_pos = 0; //variable controlling position
Servo server_left; // servo object of left garden
Servo servo_switch;
// pump setup
// Create the motor shield object with the default I2C address
Adafruit_MotorShield AFMS = Adafruit_MotorShield();
Adafruit_DCMotor *left_pump = AFMS.getMotor(1);
//moisture setup
int moistureSensorPin = A0; // select the input pin for the sensor
void setup() {
Serial.begin(9600);
setTime(16,16,0,7,7,17); // set time to Saturday 8:29:00am Jan 1 2017
printTime();
// create the alarms
Alarm.alarmRepeat(7,00,0, water_everything); // 7:00 every day it is executing 'water_everything'
// initialize the pushbutton pin as an input:
pinMode(buttonPin, INPUT);
server_left.attach(10); // attaches the servo on pin 10 to the servo object
servo_switch.attach(9);
servo_switch.write(0);
Serial.println("set to 0");
delay(3000);
servo_switch.write(180);
Serial.println("set to 0");
delay(3000);
AFMS.begin(); // create with the default frequency 1.6KHz
Serial.println("release it");
left_pump->setSpeed(0);
left_pump->run(RELEASE);
}
void printTime(){
print2digits(hour());
Serial.print(":");
print2digits(minute());
Serial.print(":");
print2digits(second());
Serial.print(" ");
Serial.print(day());
Serial.print("/");
Serial.print(month());
Serial.print("/");
Serial.print(year());
Serial.println();
}
void print2digits(int number) {
if (number >= 0 && number < 10) {
Serial.print('0');
}
Serial.print(number);
}
int get_moisture(){
return analogRead(moistureSensorPin);
}
void water_everything(){
Serial.println("starting watering function");
//switch on servo
servo_switch.write(0);
Serial.println("set to 0");
delay(3000); // somehow we need those delays, otherwise it does not work....fuuu
Serial.println("setting speed");
left_pump->setSpeed(255);
Serial.println("setting forward");
left_pump->run(BACKWARD);
/*wait until it is more moist
for (;;){
if (true || current_moisture - get_moisture() > 300)
break;
Serial.println(get_moisture());
}
*/
unsigned long start = millis();
//perform sweep
for(;;){
Serial.println(millis()-start);
if (millis()-start > 500000)
break;
Serial.println("right sweep");
for (int i = 40; i < 150; i++){
server_left.write(i);
delay(100);
}
delay(2000);
Serial.println("left sweep");
for (int i = 150; i >40; i--){
server_left.write(i);
delay(100);
}
}
delay(4000);
//stop pump
left_pump->run(RELEASE);
//switch off servo
servo_switch.write(180);
Serial.println("set to 0");
delay(3000);
return;
}
void loop() {
if (startup){
startup = false;
water_everything();
}
/*
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin);
// check if the pushbutton is pressed
if (buttonState == LOW) {
//water stuff
Serial.println("start watering it");
water_everything();
}
for(;;){
delay(100);
}
*/
printTime();
Alarm.delay(1000); // wait one second between clock display
}