110 lines
2.2 KiB
C++
110 lines
2.2 KiB
C++
#include <avr/sleep.h>
|
|
|
|
|
|
float dampPotty;
|
|
float onTimePotty;
|
|
float solDampness;
|
|
void setup() {
|
|
pinMode(2, INPUT);
|
|
pinMode(A0, INPUT);
|
|
pinMode(A2, INPUT);
|
|
pinMode(A4, INPUT);
|
|
pinMode(9, OUTPUT);
|
|
pinMode(7, OUTPUT);
|
|
pinMode(5, OUTPUT);
|
|
Serial.begin(19200);
|
|
digitalWrite(5 , HIGH);
|
|
attachInterrupt(0, wakeUp, FALLING);
|
|
Serial.println("https://git.sfs.ddnss.org/Erik/garden-watering-controller.git");
|
|
|
|
|
|
}
|
|
|
|
// the loop function runs over and over again forever
|
|
void loop() {
|
|
Serial.println(digitalRead(2));
|
|
delay(20);
|
|
bedTimeYet();
|
|
Serial.println("continuing Main");
|
|
delay(100);
|
|
Serial.println(calculateTargetDampness() , 4);
|
|
Serial.println(calculatePumpOnTime() , 4);
|
|
Serial.println(calculateDampness(), 4);
|
|
pumpOffTime(1);
|
|
wateringTheGarden();
|
|
}
|
|
|
|
float calculateTargetDampness() {
|
|
dampPotty = analogRead(A2);
|
|
return dampPotty / 454 ;
|
|
}
|
|
|
|
float calculatePumpOnTime() {
|
|
onTimePotty = analogRead(A4);
|
|
return (onTimePotty / 452) * 100;
|
|
}
|
|
|
|
float calculateDampness() {
|
|
solDampness = analogRead(A0);
|
|
return solDampness / 500;
|
|
}
|
|
|
|
void sleep() {
|
|
digitalWrite(5, LOW);
|
|
Serial.println("Going to sleep");
|
|
delay(10);
|
|
sleep_enable();
|
|
//attachInterrupt(0, wakeUp, FALLING);
|
|
set_sleep_mode(SLEEP_MODE_PWR_DOWN);
|
|
delay(10);
|
|
sleep_cpu();
|
|
}
|
|
|
|
void wakeUp() {
|
|
// detachInterrupt(0);
|
|
sleep_disable();
|
|
Serial.println("woke up");
|
|
afterWakeup();
|
|
}
|
|
|
|
void wait(long i) {
|
|
// A finction for consistent delays (only one point to adjust delay when clock speed is lowerd)
|
|
Serial.print("waiting for ");
|
|
Serial.print(i);
|
|
Serial.println(" milliseconds");
|
|
delay(i);
|
|
}
|
|
|
|
void afterWakeup() {
|
|
delay(100);
|
|
digitalWrite(5, HIGH);
|
|
}
|
|
|
|
void pumpOffTime(int x) {
|
|
for (int i = 0; i <= x * 6; i++) {
|
|
wait(10000);
|
|
bedTimeYet();
|
|
}
|
|
}
|
|
|
|
void bedTimeYet() {
|
|
if (digitalRead(2) == 1) {
|
|
sleep();
|
|
} else {
|
|
Serial.println("Skipping sleep");
|
|
}
|
|
}
|
|
|
|
void wateringTheGarden() {
|
|
if (calculateTargetDampness() > calculateDampness()) {
|
|
Serial.println("Watering");
|
|
digitalWrite(7, HIGH);
|
|
digitalWrite(9, HIGH);
|
|
wait(calculatePumpOnTime() * 1000);
|
|
digitalWrite(7, LOW);
|
|
digitalWrite(9, LOW);
|
|
} else {
|
|
Serial.println("Wet enough");
|
|
}
|
|
}
|