finish basic funtunalitiy

This commit is contained in:
Erik Foris 2022-04-10 18:53:40 +02:00
parent 927fe3080f
commit 373ea07fff
3 changed files with 95 additions and 23 deletions

View File

@ -1,16 +1,21 @@
#include <avr/sleep.h>
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");
}
}

View File

@ -1 +0,0 @@

View File

@ -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;
}