"Translating" existing sketch

As a complete newbie I've been trying to understand the sketch supplied with an Elecrow watering kit (2.0), which is not a typical board as it's pretty much pre-assembled on a Leonardo board. I had to add the U8glib and RTClib files to the library and then uploading the sketch to the Arduino seems to make it work fine - at this point. The main parameters of interest are the moisture levels that trigger the watering system and the duration of the pumping function to each pot.

As I look through the sketch, it gets so confusing to me that I don't know how I get to the level of understanding required just to be able to change some of the variables. I've included a sample of the sketch below to see if I can get some guidance on "translating" it into plain enough English that I could modify it if I needed to. (I haven't planted anything in the pots yet, as I'm just working on the set-up.)

(In addition to the code in this box, I've also included the segment of code that follows it which seems to mostly deal with the display function.)

Thanks for any guidance. I can easily supply the rest of the sketch if needed, or it can be viewed on the Elecrow web site on the bottom of that page.

void setup()
{
  //draw_elecrow();
  //delay(5000);
  Wire.begin();
  RTC.begin();
  Serial.begin(9600);
  // declare relay as output
  pinMode(relay1, OUTPUT);
  pinMode(relay2, OUTPUT);
  pinMode(relay3, OUTPUT);
  pinMode(relay4, OUTPUT);
  // declare pump as output
  pinMode(pump, OUTPUT);
  // declare switch as input
  pinMode(button, INPUT);
  //pinMode(ROTARY_ANGLE_SENSOR, INPUT);
  // water_flower();
}

void loop()
{
  // read the value from the moisture sensors:
  read_value();
  water_flower();
  int button_state = digitalRead(button);
  if (button_state == 1)
  {
    //read_value();
    u8g.firstPage();
    do
    {
      drawTH();
      drawflower();
    } while ( u8g.nextPage() );
    delay(500);
  }
  else
  {
    u8g.firstPage();
    do
    {
      drawtime();
      u8g.drawStr(8, 55 , "www.elecrow.com");
    } while (u8g.nextPage());
    delay(500);
  }
}

//Set moisture value
void read_value()
{
  float value1 = analogRead(A0);
  moisture1_value = (value1 * 120) / 1023; delay(20);
  float value2 = analogRead(A1);
  moisture2_value = (value2 * 120) / 1023; delay(20);
  float value3 = analogRead(A2);
  moisture3_value = (value3 * 120) / 1023; delay(20);
  float value4 = analogRead(A3);
  moisture4_value = (value4 * 120) / 1023; delay(20);
}

void water_flower()
{
  if (moisture1_value < 30)
  {
    digitalWrite(relay1, HIGH);
    relay1_state_flag = 1;
    delay(50);
    if (pump_state_flag == 0)
    {
      digitalWrite(pump, HIGH);
      pump_state_flag = 1;
      delay(50);
    }
  }
  else if (moisture1_value > 55)
  {
    digitalWrite(relay1, LOW);
    relay1_state_flag = 0;
    delay(50);
    if ((relay1_state_flag == 0) && (relay2_state_flag == 0) && (relay3_state_flag == 0) && (relay4_state_flag == 0))
    {
      digitalWrite(pump, LOW);
      pump_state_flag = 0;
      delay(50);
    }
  }

  if (moisture2_value < 30)
  {
    digitalWrite(relay2, HIGH);
    relay2_state_flag = 1;
    delay(50);
    if (pump_state_flag == 0)
    {
      digitalWrite(pump, HIGH);
      pump_state_flag = 1;
      delay(50);
    }
  }
  else if (moisture2_value > 55)
  {
    digitalWrite(relay2, LOW);
    relay2_state_flag = 0;
    delay(50);
    if ((relay1_state_flag == 0) && (relay2_state_flag == 0) && (relay3_state_flag == 0) && (relay4_state_flag == 0))
    {
      digitalWrite(pump, LOW);
      pump_state_flag = 0;
      delay(50);
    }
  }

  if (moisture3_value < 30)
  {
    digitalWrite(relay3, HIGH);
    relay3_state_flag = 1;
    delay(50);
    if (pump_state_flag == 0)
    {
      digitalWrite(pump, HIGH);
      pump_state_flag = 1;
      delay(50);
    }
  }
  else if (moisture3_value > 55)
  {
    digitalWrite(relay3, LOW);
    relay3_state_flag = 0;
    delay(50);
    if ((relay1_state_flag == 0) && (relay2_state_flag == 0) && (relay3_state_flag == 0) && (relay4_state_flag == 0))
    {
      digitalWrite(pump, LOW);
      pump_state_flag = 0;
      delay(50);
    }
  }

  if (moisture4_value < 30)
  {
    digitalWrite(relay4, HIGH);
    relay4_state_flag = 1;
    delay(50);
    if (pump_state_flag == 0)
    {
      digitalWrite(pump, HIGH);
      pump_state_flag = 1;
      delay(50);
    }
  }
  else if (moisture4_value > 55)
  {
    digitalWrite(relay4, LOW);
    relay4_state_flag = 0;
    delay(50);
    if ((relay1_state_flag == 0) && (relay2_state_flag == 0) && (relay3_state_flag == 0) && (relay4_state_flag == 0))
    {
      digitalWrite(pump, LOW);
      pump_state_flag = 0;
      delay(50);
    }
  }

}

Sketch segment 2.txt (5.18 KB)

Unfortunately, the code is pretty simple, which tells me you need to do a little studying about how to program in the Arduino environment. There are a bazillion C tutorials available free online, and many of them are specific to the Arduino IDE. I would suggest you start there and see what best suits your needs.

You'd do yourself a big favor, and NOT use that crappy sketch.

Learn about arrays and for loops. The author of that sketch didn't.
Learn about header files, for defining bitmaps. The author of that sketch didn't.
Learn how to use the delete key, to get rid of code you don't want. The author of that sketch didn't.
Learn how to use local variables, as much as possible. The author of that sketch didn't.
Learn how to use meaningful names for functions, including using plurals in names of functions that do more than one thing. The author of that sketch didn't.
Learn not to copy/paste code. Write a function that takes an argument, or more than one, instead. The author of that sketch didn't.

Yikes! I'm 72 years old and have spent hours just trying to get the basics, with no past experience in programming. I've looked through quite a few tutorials but seem to get quickly lost in some of the descriptions and language. I'm willing to learn what I can, but it's hard at my age, and I fear all of my plants will be dead several times over by the time I figure it out. :o

One of the problems with a bazillion tutorials free online is that I don't even know enough to pick ones that are relevant to my set-up, including the Arduino that's already configured for a specific purpose. That code came from Elecrow, who sells the device.

I fear all of my plants will be dead several times over by the time I figure it out.

Water them by hand in the meantime.

Most soil moisture sensors are unreliable, and anything with electrodes exposed to damp soil and relying on soil conductivity (like those in the Elecrow kit) will become fouled and useless in time. Sometimes quite rapidly.

So if you value your plants, you won't trust that system completely, if at all.