seperate stuff to functions

This commit is contained in:
Marcus 2024-03-31 05:47:43 +02:00
parent 58e42a814d
commit c3e6e8dd9e

View file

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