From 373ea07fff473865b519efab51f87ec535460467 Mon Sep 17 00:00:00 2001 From: xTechnoErikx Date: Sun, 10 Apr 2022 18:53:40 +0200 Subject: [PATCH] finish basic funtunalitiy --- src/arduinoNano/arduinoNano.ino | 91 ++++++++++++++++++++------- src/arduinoNano/calibrateSleep.ino | 1 - src/calibrateSleep/calibrateSleep.ino | 26 ++++++++ 3 files changed, 95 insertions(+), 23 deletions(-) delete mode 100644 src/arduinoNano/calibrateSleep.ino create mode 100644 src/calibrateSleep/calibrateSleep.ino diff --git a/src/arduinoNano/arduinoNano.ino b/src/arduinoNano/arduinoNano.ino index bba464c..64c090e 100644 --- a/src/arduinoNano/arduinoNano.ino +++ b/src/arduinoNano/arduinoNano.ino @@ -1,16 +1,21 @@ #include -float damppotty; +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(9 ,HIGH); + digitalWrite(5 , HIGH); + attachInterrupt(0, wakeUp, FALLING); + } @@ -19,27 +24,37 @@ void setup() { void loop() { Serial.println(digitalRead(2)); delay(20); - if (digitalRead(2) == 1) { - sleep(); - } else { - Serial.println("Skipping sleep"); - } + bedTimeYet(); Serial.println("continuing Main"); - delay(100); - Serial.println(calculateTargetDampness() ,4); + 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 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(9, LOW); + digitalWrite(5, LOW); Serial.println("Going to sleep"); delay(10); sleep_enable(); - attachInterrupt(0, wakeUp, FALLING); + //attachInterrupt(0, wakeUp, FALLING); set_sleep_mode(SLEEP_MODE_PWR_DOWN); delay(10); sleep_cpu(); @@ -49,15 +64,47 @@ void wakeUp() { // detachInterrupt(0); sleep_disable(); Serial.println("woke up"); - //afterWakeup(); + afterWakeup(); } -void wait(int i){ +void wait(long i) { // A finction for consistent delays (only one point to adjust delay when clock speed is lowerd) - delay(i); - } + Serial.print("waiting for "); + Serial.print(i); + Serial.println(" milliseconds"); + delay(i); +} - void afterWakeup(){ - delay(100); - digitalWrite(9,HIGH); - } +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"); + } +} diff --git a/src/arduinoNano/calibrateSleep.ino b/src/arduinoNano/calibrateSleep.ino deleted file mode 100644 index 8b13789..0000000 --- a/src/arduinoNano/calibrateSleep.ino +++ /dev/null @@ -1 +0,0 @@ - diff --git a/src/calibrateSleep/calibrateSleep.ino b/src/calibrateSleep/calibrateSleep.ino new file mode 100644 index 0000000..2ce72d8 --- /dev/null +++ b/src/calibrateSleep/calibrateSleep.ino @@ -0,0 +1,26 @@ + +void setup() { + pinMode(2, INPUT); + pinMode(A4, INPUT); + Serial.begin(19200); + float poty; + float dampness; + + +} + +void loop() { + Serial.println(digitalRead(2)); + delay(200); + Serial.println(analogRead(A4)); + Serial.println(percentage() ,4); + delay(20); +} + +float percentage(){ + +// float poty = analogRead(A2); + //return poty/454; + return analogRead(A4) / 454; + + }