Here it is:
#include <Wire.h> // Comes with Arduino IDE
#include <LiquidCrystal_I2C.h>
const int vand_niveau = A3; // Vand niveau i reservoir.
const int vand = 8; // Spændings kontrol for vand lavt niveau.
const int fugtighed = 4; // Spændings kontrol for jord fugtighed.
const int jord = A0; // Jord fugtighed.
int motor_pwm = 9; // Kontrol af pumpe mængde.
int output_vand; // Vand niveau skrives hertil.
int output_jord; // Jord fugt skrives hertil
unsigned long int_tim = 0;
unsigned long pump_tim = 0;
unsigned long pumpStartTime = 0;
unsigned long display_tim = 0; // Serial or display timer
unsigned long display_backlight_tim = 0;
LiquidCrystal_I2C lcd(0x3F, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE); // LCD I2C addresse (0x3F)
void setup() {
Serial.begin(9600); // Kommunikations vindue
lcd.begin(16, 2); // Initialize the LCD 16 Char 2 Lines, turn on backlight
for (int i = 0; i < 6; i++) // ------- Quick 6 blinks of backlight ---------
{
lcd.backlight();
delay(150);
lcd.noBacklight();
delay(150);
}
lcd.backlight(); // Finish with backlight on
pinMode (vand, OUTPUT);
pinMode (fugtighed, OUTPUT);
pinMode (motor_pwm, OUTPUT);
pinMode (LED_BUILTIN, OUTPUT);
Serial.begin(9600);
// set up the LCD's number of columns and rows:
lcd.begin(16, 2);
// Print a message to the LCD.
lcd.print("Analyserer fugt");
lcd.setCursor(0, 1);
lcd.print("vent venligst");
Serial.println("Analyserer fugt:");
delay(2000);
lcd.clear();
}
void loop() {
// when characters arrive over the serial port...
if (Serial.available()) {
// wait a bit for the entire message to arrive
//delay(100);
// clear the screen
lcd.clear();
// read all the available characters
while (Serial.available() > 0) {
// display each character to the LCD
lcd.write(Serial.read());
}
}
// Turns on the VCC for the humidity sensor once sensor every hour, and then reads the value - also check water level in holding tank
if (millis() - int_tim >= 6000) { // OBS_1 <- Den her tid skal ændres i forbindelse med test, men ikke til mindre end 2000
digitalWrite(vand, HIGH);
digitalWrite(fugtighed, HIGH);
if (millis() - int_tim >= 6250) { // OBS <- Den her tid skal være 250 ms større end OBS_1
output_vand = analogRead(vand_niveau);
output_vand = map(output_vand, 0, 350, 0, 100);
output_jord = analogRead(jord);
output_jord = map(output_jord, 1023, 400, 0, 100);
//Serial.print("Moisture : ");
//lcd.print("Fugt:");
//lcd.print(output_vand);
//lcd.print("%");
//lcd.setCursor(0, 1);
//lcd.print("Plante:");
//lcd.print(output_jord);
//lcd.print("%");
//Serial.print(output_vand);
//Serial.print("%");
//lcd.clear();
int_tim = millis();
digitalWrite(vand, LOW);
digitalWrite(fugtighed, LOW);
}
}
//unsigned long currentMillis = millis();
if (output_jord < 40 // Fugtighed I plante under 40% start motor
&& output_vand > 40) { // Fugtighed I reservoir over 40% start motor
analogWrite(motor_pwm, 200);
pumpStartTime = millis();
}
else {
analogWrite (motor_pwm, 0);
}
// If the pump is "ON" and 1000 ms have elapsed, turn off the pump
if (millis() - pumpStartTime >= 1000) {
analogWrite (motor_pwm, 0);
}
// If water level in tank is below threshold, blink the display with 1000 ms cycle
if (output_vand < 40) {
if (millis() - display_backlight_tim >= 1000) {
lcd.noBacklight();
}
if (millis() - display_backlight_tim >= 2000) {
lcd.backlight();
display_backlight_tim = millis();
}
}
// Write to the display every 1000 ms
if (millis() - display_tim >= 1000) {
lcd.clear();
Serial.print("Moisture : ");
lcd.print("Fugt:");
lcd.print(output_vand);
lcd.print("%");
lcd.setCursor(0, 1);
lcd.print("Plante:");
lcd.print(output_jord);
lcd.print("%");
Serial.print(output_vand);
Serial.print("%");
display_tim = millis();
}
}