From 927fe3080f01d6f807298d77b1449d6ffcac54f6 Mon Sep 17 00:00:00 2001 From: xTechnoErikx Date: Sat, 9 Apr 2022 15:21:22 +0200 Subject: [PATCH] setting up sleep for the arduino, adding mathod to calulate persentage of the potty --- src/arduinoNano/arduinoNano.ino | 59 +++++++++++++++++++++++++++--- src/arduinoNano/calibrateSleep.ino | 1 + 2 files changed, 54 insertions(+), 6 deletions(-) create mode 100644 src/arduinoNano/calibrateSleep.ino diff --git a/src/arduinoNano/arduinoNano.ino b/src/arduinoNano/arduinoNano.ino index 0dfb527..bba464c 100644 --- a/src/arduinoNano/arduinoNano.ino +++ b/src/arduinoNano/arduinoNano.ino @@ -1,16 +1,63 @@ +#include + +float damppotty; void setup() { - pinMode(2, INPUT); - pinMode(9, OUTPUT); - pinMode(7, OUTPUT); - pinMode(5, OUTPUT); - Serial.begin(9600); + pinMode(2, INPUT); + pinMode(A2, INPUT); + pinMode(A4, INPUT); + pinMode(9, OUTPUT); + pinMode(7, OUTPUT); + pinMode(5, OUTPUT); + Serial.begin(19200); + digitalWrite(9 ,HIGH); + - } // the loop function runs over and over again forever void loop() { Serial.println(digitalRead(2)); delay(20); + if (digitalRead(2) == 1) { + sleep(); + } else { + Serial.println("Skipping sleep"); + } + Serial.println("continuing Main"); + delay(100); + Serial.println(calculateTargetDampness() ,4); } + +float calculateTargetDampness(){ + damppotty = analogRead(A2); + return damppotty / 454 ; +} + +void sleep() { + digitalWrite(9, 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(int i){ + // A finction for consistent delays (only one point to adjust delay when clock speed is lowerd) + delay(i); + } + + void afterWakeup(){ + delay(100); + digitalWrite(9,HIGH); + } diff --git a/src/arduinoNano/calibrateSleep.ino b/src/arduinoNano/calibrateSleep.ino new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/src/arduinoNano/calibrateSleep.ino @@ -0,0 +1 @@ +