Does my code work? Please help

Edit: Okay, maybe it's because I made my "period" integer WAY to high, but I can't go back to test my code at the moment. Regardless, thank you to those who responded

Hello, I'm a student. I'm supposed to do an arduino project for a final class assignment. Mine's supposed to be one of those automated soil moisture projects.

However, Despite not getting error messages anymore, it still doesn't seem to work how I want it to. The values I would get from a sensor would seem too low (you'll see it in the code), and the text doesn't appear in the serial monitor. Maybe its how I built it, I don't know. First is the code I used to get the values to put in my actual test code

/*
 */
int sensorValue;
int readPin = A0;

void setup() {
  pinMode(readPin,INPUT);
  Serial.begin(9600);
}

void loop() {
    sensorValue = analogRead(readPin);
    delay(2000); //wait for voltage to settle
    Serial.println(sensorValue);
}

Then here's the test code

/*
 */
const int Pin_D1 = 8;
const int Hydro_D1 = 7; //hydro pump connect to plant of sensor 1

//Measure Soil Moisture Levels
int Value_D1;
int readPin = A0;

//Measure time
int delayTime = 100;
int period = 3600000;
unsigned long time_now = 0;

//Measure Water Level
int waterSensorPin = A1;
int waterValue;

//Water parameter
const int waterThreshold = 320; //value for low water

//Soil parameters
const int dryWall = 350; //value for dry soil
const int wetWall = 335; //value for wet soil

void setup() {
  Serial.begin(9600); //serial monitor
  pinMode(Pin_D1,OUTPUT); //soil moisture sensor 1
  pinMode(readPin,INPUT); //analog input pin
  pinMode(waterSensorPin,INPUT); //monitor water level
}

void loop(){
  time_now = millis ();
  
  nourishment();
  
  while(millis() < time_now + period){
   //wait approx. [period] ms
  }
}

void nourishment() {
  //PLANT ONE
  digitalWrite(Pin_D1,HIGH); //soil moisture sensor 1 ON
  delay(2000); //wait for voltage to settle
  Value_D1 = analogRead(readPin); //Read analog pin as sensor 1
  Serial.println("D1 =");
  Serial.print(Value_D1);
  waterCheck();
  
  if (Value_D1 < dryWall) {
    digitalWrite(Hydro_D1,HIGH); //hydro pump 1 ON
    Value_D1 = analogRead(readPin);
    delay(delayTime);
    Serial.println("D1 =");
    Serial.print(Value_D1);
    waterCheck();
      } else if (Value_D1 < wetWall) {
          Value_D1 = analogRead(readPin);
          delay(delayTime);
          Serial.println("D1 =");
          Serial.print(Value_D1);
          waterCheck();
    }
  if (Value_D1 > wetWall) {
    digitalWrite(Hydro_D1,LOW);
      }
waterCheck();
delay(1000);
}

void waterCheck() {
  waterValue = analogRead(waterSensorPin);
  Serial.println("Water =");
  Serial.print(waterValue);
  if (waterValue < waterThreshold) {
    lowWater();
  }
}
  
void lowWater() {
  digitalWrite(Pin_D1,LOW);
  digitalWrite(Hydro_D1,LOW);
}

Please tell me what I can do, and thank you.

One more example that shows that the default compiler warning level should be ALL.

arduino-cli compile -b arduino:avr:uno --warnings all --output-dir ~/tmp --no-color (in directory: /home/me/Documents/sketchbook/Uno_R3/test)
/home/me/Documents/sketchbook/Uno_R3/test/test.ino:12:14: warning: overflow in implicit constant conversion [-Woverflow]
 int period = 3600000;
              ^~~~~~~
Sketch uses 5600 bytes (17%) of program storage space. Maximum is 32256 bytes.
Global variables use 208 bytes (10%) of dynamic memory, leaving 1840 bytes for local variables. Maximum is 2048 bytes.
Compilation finished successfully.

uh, wrong post?

Right post. Turn your compiler warning level to ALL and re-compile.

oh, okay. Sorry. Can I do this on cloud?

Cloud is not in my wheelhouse.

Damn. That's where I did the code

Compiling it in the IDE with the compiler warning level set to ALL would seem to be an obvious solution. Or you could just read the warning message that the compiler generated for me...

Alright, or I can half the timer since that appears to be the issue. Thank you for responding

actually 10 minutes

That would be incorrect.

Pop quiz: what is the range of the int type on an Uno R3? Which is what I'm assuming by default you are using since you didn't specify otherwise.

Honestly, I don't know. I'm assuming its around 30,000 to -30,000

But yeah, it's an uno r3 (I don't think it being an elegro makes a difference)

Numeric range of int is?

Suggest you try unsigned long int, let us know how it works then. That's just a first observation, there may be more.

Oh, yeah, you've failed the millis() test - to guarantee calculation success, use a test of the form

millis() - period >= last

because the following fails the rollover:

There are tutorials on this forum about that.

alright, thank you. Though are there some tutorials you could provide?

Start here, but ask questions here(not there).

It as a "CLI" command for verbose output, so maybe there is a GUI setting...

arduino-cloud-cli device create --name mydevice -v

Close. 32767 to -32768 to be exact.

So 3600000 isn't going to fit. And neither is half of that, 1800000.

The solution to getting rid of the warning is to use the correct type, which you already did for time_now, namely unsigned long.

This

int period = 3600000;

should be

unsigned long period = 3600000;

And because subtracting unsigned works through wrap around whereas adding them doesn't, this

  while(millis() < time_now + period){
   //wait approx. [period] ms
  }

should be this

  while(millis() - time_now < period){
   //wait approx. [period] ms
  }

or more simply in your application, which isn't doing anything inside the while loop:

  delay(period);

This sounds comprehensible, so thank you. Since 1 hour is too big, does that mean I should change it to 30 seconds (30,000) instead? Want to make sure

The time delay is entirely up to you.