From c3e6e8dd9ef042b62e06e29277df894f7a59ac99 Mon Sep 17 00:00:00 2001 From: Marcus Date: Sun, 31 Mar 2024 05:47:43 +0200 Subject: [PATCH] seperate stuff to functions --- Arduino/CanGrow/CanGrow.ino | 98 ++++++++++++++++++++++++++++++------- 1 file changed, 79 insertions(+), 19 deletions(-) diff --git a/Arduino/CanGrow/CanGrow.ino b/Arduino/CanGrow/CanGrow.ino index ed3e965..c0d7399 100644 --- a/Arduino/CanGrow/CanGrow.ino +++ b/Arduino/CanGrow/CanGrow.ino @@ -12,14 +12,6 @@ * D6 - MOSFET Pump * A0 - water level * - * - * Water level - * =========== - * - * 0 - 199 : CRITICAL - * 200 - 399 : WARNING - * >400 : OK - * */ @@ -116,6 +108,78 @@ unsigned int readI2CRegister16bit(int addr, int reg) { return t; } +int getWaterlevel() { + + /* + * waterlevelRAW + * =========== + * 0 - 199 : CRITICAL + * 200 - 399 : WARNING + * >400 : OK + * + * waterlevel + * ========== + * 2 : CRITICAL + * 1 : WARNING + * 0 : OK + */ + + int waterlevelWARN = 200; + int waterlevelOK = 400; + int waterlevelRAW = 0; + int waterlevel = 0; + + // enable Vcc for water level sensor + digitalWrite(PINwater, HIGH); + // wait a bit to let the schematic "settle in" + delay(200); + // get the value + waterlevelRAW = analogRead(PINAwater); + // disable Vcc for the sensor (in case of chemical reactions of the probes) + digitalWrite(PINwater, LOW); + + if( waterlevelRAW >= waterlevelOK) { + waterlevel = 0; + } else if( waterlevelRAW >= waterlevelWARN) { + waterlevel = 1; + } else { + waterlevel = 2; + } + + return waterlevel; +} + +float getTemperature(bool tempSensor) { + /* + * tempSensor + * ========== + * 0/false : DHT11 temp sensor + * 1/true : chirp I2C temp sensor + */ + + float temperature = 0; + + if(tempSensor == false ) { + // read temperature from DHT11 + temperature = dht.readTemperature(); + } else { + // read temperature from chrip I2C + temperature = readI2CRegister16bit(0x20, 5) * 0.10 ; + } + + return temperature; +} + +float getHumidity() { + float humidity = dht.readHumidity(); + return humidity; +} + +int getSoilmoisture() { + int soilmoisture = readI2CRegister16bit(0x20, 0); + return soilmoisture; +} + void setup() { // setup pins pinMode(PINfan, OUTPUT); @@ -192,27 +256,23 @@ void loop() { display.setCursor(0,0); display.print("I2C: "); - display.print(readI2CRegister16bit(0x20, 0)); + display.print(getSoilmoisture()); display.print(", "); - display.println(readI2CRegister16bit(0x20, 5) * 0.10 ); + display.println(getTemperature(1)); /* display.print(", "); writeI2CRegister8bit(0x20, 3); //request light measurement display.println(readI2CRegister16bit(0x20, 4)); */ display.print("DHT11: "); - display.print(dht.readTemperature()); + display.print(getTemperature(0)); display.print(", "); - display.print(dht.readHumidity()); - //display.println(readI2CRegister16bit(0x20, 0)); + display.println(getHumidity()); display.display(); - digitalWrite(PINwater, HIGH); - delay(200); - display.print("Water: "); - display.println(analogRead(PINAwater)); - - digitalWrite(PINwater, LOW); + display.print(":) Water: "); + display.println(getWaterlevel()); + display.display(); delay(800);