setting up sleep for the arduino, adding mathod to calulate persentage of the potty

This commit is contained in:
Erik Foris 2022-04-09 15:21:22 +02:00
parent bcd25bd40a
commit 927fe3080f
2 changed files with 54 additions and 6 deletions

View File

@ -1,10 +1,16 @@
#include <avr/sleep.h>
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);
}
@ -13,4 +19,45 @@ void setup() {
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);
}

View File

@ -0,0 +1 @@