Soil moisture sensor code

can anyone please help ive been trying for so long
the plan is when i click one of the buttons it should execute the code for the specific plant and it should display the soil moisture in real time

but the problem is the first plant will always execute but will repeatedly display the first sensed input and not the real time input for example: it started at 0%, even if theres water around if the first input is 0% it will always display 0%.

second problem is when i choose the first plant i cant change to the second plant because im stuck inside the loop and idk how to get out.

can anyone help?
thank you so much.

//for soil moisture
const int AirValue = 810;    //edit later
const int WaterValue = 390;  //edit later
int SoilMoistureValue = 390;
int SoilMoisturePercent = 100;
const int SMrelay = 8;
int lastnightshadestate;
int lastcruciferstate;
int lastbeanstate;
int currentnightshadestate;
int currentcruciferstate;
int currentbeanstate;

void setup() {
  // initialize serial communications at 9600 bps:
  Serial.begin(9600);


  pinMode(51, INPUT);
  pinMode(52, INPUT);
  pinMode(53, INPUT);

  pinMode(A4, INPUT);
  pinMode(8, OUTPUT);

  currentnightshadestate = digitalRead(51);
  currentcruciferstate = digitalRead(52);
  currentbeanstate = digitalRead(53);
}

void loop() {
  digitalWrite(8, HIGH);
  lastnightshadestate = currentnightshadestate;
  lastcruciferstate = currentcruciferstate;
  lastbeanstate = currentbeanstate;
  currentnightshadestate = digitalRead(51);
  currentcruciferstate = digitalRead(52);
  currentbeanstate = digitalRead(53);
  SoilMoistureValue = analogRead(A4);  //SM sensor
  SoilMoisturePercent = map(SoilMoistureValue, AirValue, WaterValue, 0, 100);

  if (lastnightshadestate == HIGH && currentnightshadestate == LOW) {
    Serial.print("Nightshade");
    for (int i = 0; i < i + 1; i++) {
      Serial.println(SoilMoistureValue);
      if (SoilMoisturePercent <= 70) {

        Serial.print(SoilMoisturePercent);
        Serial.print("%");
        digitalWrite(SMrelay, LOW);  //water pump on
        delay(700);
      } else if (SoilMoisturePercent > 70) {

        Serial.print(SoilMoisturePercent);
        Serial.print("%");
        digitalWrite(SMrelay, HIGH);  //water pump off
        delay(700);
      }
      Serial.println();
    }
  }


  if (lastcruciferstate == HIGH && currentcruciferstate == LOW) {
    Serial.print("Crucifers");
    for (int i = 0; i < i + 1; i++) {
      Serial.println(SoilMoistureValue);
      if (SoilMoisturePercent <= 70) {

        Serial.print(SoilMoisturePercent);
        Serial.print("%");
        digitalWrite(SMrelay, LOW);  //water pump on
        delay(700);
      } else if (SoilMoisturePercent > 70) {

        Serial.print(SoilMoisturePercent);
        Serial.print("%");
        digitalWrite(SMrelay, HIGH);  //water pump off
        delay(700);
      }
      Serial.println();
    }
  }

  if (lastbeanstate == HIGH && currentbeanstate == LOW) {
    Serial.print("Beans");
    for (int i = 0; i < i + 1; i++) {
      Serial.println(SoilMoistureValue);
      if (SoilMoisturePercent <= 60) {

        Serial.print(SoilMoisturePercent);
        Serial.print("%");
        digitalWrite(SMrelay, LOW);  //water pump on
        delay(700);
      } else if (SoilMoisturePercent > 60) {

        Serial.print(SoilMoisturePercent);
        Serial.print("%");
        digitalWrite(SMrelay, HIGH);  //water pump off
        delay(700);
      }
      Serial.println();
    }
  } else {
  }
}

Please explain this:


for (int i = 0; i < i + 1; i++) {

i.e. what is this all about i < i + 1

for that part to continue executing

but this program has a lot of errors i dont understand so feel free to change it. thanksss

When will i < i + 1 ever be false :thinking:

thats the problem i have idk how to end it so when i select a plant it cant change into the other because its stuck

  • When the loop starts, i = 0

  • You add 1 to i now it is equal to 1

  • You ask the question is this true, i < i + 1 that is 1 < 2 hence there’s a problem.

  • You need to rethink more about what the upper limit should be.

yes im aware but i dont want to give it a limit, it should end when another plant is pressed

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.