Moisture detection leds problem with the serial monitor

Greetings, I have the problem with this code that when the sensor variable is activated, the serial monitor stops.

I want to know how I can make the monitor continue showing the humidity measurements.

The programming is made so that every 24 hours the red leds turns on and when the humidity is optimal, the green leds turns on and stays on for 24 hours. While this happens, the serial monitor is supposed to continue working, but it doesn't.

i share the code here in this link Circuit design light code problem with serial monitor | Tinkercad

//
int humidity = 0 ;

int period = 500;
unsigned long time_now = 0;
void setup()
{
  Serial.begin(9600);
  pinMode(A0, INPUT);
  pinMode(9, OUTPUT);
  pinMode(8, OUTPUT);
}

void Light(){
 if(millis() >= time_now + period){
        time_now += period;
     if (20 > humidity ) {
    digitalWrite(9, LOW);
    digitalWrite(8, HIGH);
   
  } else {
    if (20 < humidity ) {
      
      digitalWrite(9, HIGH);
      digitalWrite(8, LOW);
        delay(86400000); // Wait for 86400000 millisecond(s)(24 hours)
        
    }
  }
       
    }
} 


void loop()
{
  
  humidity = (analogRead(A0) / 10);   
  Serial.println(humidity);
  Light();
  delay(20); 
}


Please post your code here, using < CODE/ > tags when you do

1 Like

Are you surprised if you use delay?

Which code ?

I already put the code in the post

i already put the code in the post, thank for remember

delete empty lines of code and format the sketch by using cntl +t.

This line will stop the CPU from doing anything for 420000 milliseconds, or about seven minutes.
If you want the CPU to be able to send things to the serial monitor, you need to use the same millis() technique you use elsewhere in your program.

1 Like

so?

if (20 < humidity ) {
     
      digitalWrite(9, HIGH);
      digitalWrite(10, LOW);
     millis(420000);
        
      }

did you try it for yourself?

yes, and doesn't respond
@paulpaulson

Where did you get the code from? If you don't understand Paul's comment, there's no way this was written by you, so there's a bigger problem.

I did it and tried it, but the millis() not respond

It is my code and I ask for help not a jtrial.

Not a trial, my friend, just an observation. millis() returns a value, it doesn't do the equivalent of delay() so it can't be used in the same way.

Several other points; you're writing to different output pins than you've set as outputs. 8/9 vs 9/10.
Your code is taking a nap for a long time, so of course the display won't be written to.
This
if (20 < humidity )
is unnecessary, as you're already in the else clause of
if (20 > humidity )
But, what happens if humidity is = 20?
As for your question, read some more about non-blocking code, and figure out how to properly respond to your once-a-day need without taking a 24 hour dirt nap.

This is unreliable, it will fail when millis() rolls over. Please read the millis() documentation and examples to understand why. What you have made, is a future timestamp and you compare millis() against it. What you must do, is store a past time stamp, subtract it from current millis(), and make decisions based on the difference.

All of the example code does that. So I guess you never bothered to look at it. That, or else you believe that a lot of seasoned programmers are wrong.

1 Like

i solve the pin problems.

I need something similar to delay() that every 24 hours the device notifies me when to give water to the plants.

Yeah, it's millis(), used properly. Or better, an RTC.

1 Like

LOL. Fortunately timed well between sips of coffee.

a7

1 Like

Hello sebastianriv

consider - untested

#define usl unsigned long // I´m lazy to type
int humidity = 0 ;
enum Relays {One, Two};
constexpr int LedPin[] {8, 9};
constexpr int HumiPin {A0};
struct TIMER
{
  usl now;
  usl span;
};
TIMER timer24h {0, 24 * 60 * 1000};
TIMER readHumi {0, 10000};
void setup()
{
  Serial.begin(9600);
  for (auto Led:LedPin) pinMode(Led,OUTPUT);
}
void loop()
{
  usl currentMillis = millis();
  if (currentMillis - timer24h.now >= timer24h.span)
  {
    timer24h.now = currentMillis;
    (analogRead(HumiPin) / 10) < 30 ? digitalWrite(LedPin[One], HIGH) : digitalWrite(LedPin[One], LOW);
    digitalWrite(LedPin[Two], digitalRead(LedPin[One])? LOW: HIGH);
  }
  if (currentMillis - readHumi.now >= readHumi.span)
  {
    readHumi.now = currentMillis;
    Serial.println(analogRead(HumiPin) / 10);
  }
}

Have a nice day and enjoy coding in C++.

1 Like