import org.firmata4j.Pin;
import org.firmata4j.ssd1306.SSD1306;

import java.util.TimerTask;

class task extends TimerTask {
   //final variables
   private final Pin sensorVoltage;
   private final SSD1306 Ledscreen;
   private final Pin waterPump;

   //constructor
   task(Pin sensorVoltage, SSD1306 Ledscreen, Pin waterPump) {
       this.sensorVoltage = sensorVoltage;
       this.Ledscreen = Ledscreen;
       this.waterPump = waterPump;
   }

   @Override
   public void run() {
       //this is to diaplay string on OLED
       Ledscreen.getCanvas().clear();
       String valueString = String.valueOf(sensorVoltage.getValue());
       Ledscreen.getCanvas().drawString(0, 0, valueString);
       Ledscreen.display();

       try {
           if (sensorVoltage.getValue() > 700) {
               waterPump.setValue(1);
               Ledscreen.getCanvas().clear();
               //displaying this message when it's dry
               this.Ledscreen.getCanvas().drawString(0, 14, "Soil is dry! need water");
               this.Ledscreen.getCanvas().drawString(0, 25, "The pump is giving water");
               Ledscreen.display();
               System.out.println("Soil is really dry! need water"); //displaying this message when it's dry
           } else {
               waterPump.setValue(0);
               Ledscreen.getCanvas().clear();
               //displaying this message when it's wet
               this.Ledscreen.getCanvas().drawString(0, 14, "Soil is Wet enough! no need to water it");
               this.Ledscreen.getCanvas().drawString(0, 25, "The pump is Off");
               Ledscreen.display();
               System.out.println("Soil is Wet enough! no need to water it"); //displaying this message when it's wet
           }
       } catch (Exception exception) {
           System.out.println("There is an error!!");
       }
   }
}

import org.firmata4j.*;
import org.firmata4j.firmata.FirmataDevice;
import org.firmata4j.ssd1306.SSD1306;

import java.io.IOException;
import java.util.Timer;

public class MinorMinorProject {
   static final int A0 = 15; //Moisture sensor Port
   static final int D2 = 2; // MOSEFT Pin

   public static void main(String[] args)
           throws IOException, InterruptedException {
       // Starting the GroveBoard
       String myPort = "/dev/cu.usbserial-0001";
       IODevice arduinoObject = new FirmataDevice(myPort);
       arduinoObject.start();
       arduinoObject.ensureInitializationIsDone();
       // The OLED
       var i2cObject = arduinoObject.getI2CDevice((byte) 0x3C);
       SSD1306 OlED = new SSD1306(i2cObject, SSD1306.Size.SSD1306_128_64);
       OlED.init();

       // Controlling the Moisture Sensor
       var moistureSensor = arduinoObject.getPin(A0);
       moistureSensor.setMode(Pin.Mode.ANALOG);
       //Controlling the Water Pump
       var waterPump = arduinoObject.getPin(D2);
       waterPump.setMode(Pin.Mode.OUTPUT);
       // Calling Timer Task Class
       var CallingTimerTask = new task(moistureSensor, OlED, waterPump);
       Timer timerObject = new Timer();
       timerObject.schedule(CallingTimerTask, 0, 2000);//setting a timer
   }
}