While loop timer is not working as expected when measuring values

Hi all, I'm pretty new to Arduinos but have some level of familiarity from a few years back. I am running into an issue in my code, and I cannot quite figure out what is wrong.

I am writing a program where a value is being measured. If the value is above the desired threshold for a pre-determined amount of time, then a rgb light will display red. If the value stays below the threshold for about 30 seconds, then a green light will be displayed, but if the value goes back above the threshold, then the timer resets for green. in the background the light will display blue if either the red or green count is not met.

The green count and light seem to be working as intended, however there seems to be an issue with the red count when testing at values greater than 10 minutes. For shorter time periods (1-6 minutes) the red light displays at the appropriate time. In a trial I've noticed the red count value will all of the sudden jump to a very large value. I'm a little puzzled by this and if anyone has any suggestions on how to alter my code they would be greatly appreciated.

Thanks.

 //Setting 3
  while (analogRead(A2) > 990) {
    if (analogRead(A0) <= 612.98) {
      countG3 = countG3 + 1;
      Serial.println(countG3);
    }
    if (analogRead(A0) > 612.98) {
      countR3 = countR3 + 1;
      Serial.println(countR3);
    }
    if (countG3 >= 3000) {  //30 seconds
      digitalWrite(3, LOW);
      digitalWrite(5, HIGH);
      digitalWrite(6, LOW);
      if (analogRead(A0) > 612.98) {
        countG3 = 0;
      }
    } else if (countR3 >= 180000) {  //30 minutes
      digitalWrite(3, LOW);
      digitalWrite(5, LOW);
      digitalWrite(6, HIGH);
    } else {
      digitalWrite(3, HIGH);
      digitalWrite(5, LOW);
      digitalWrite(6, LOW);
    }
    delay(10);
  }

Welcome to the forum

Please post a complete sketch and not just a snippet of it

Are you trying to use the time taken for a while loop to execute in order to time something ?

First analogRead() does not return a float. Second what is the data type of countR3? it needs to be larger than 16 bits to store a value >= 180000.

Why did you decided that your while loop runs 100 times in a second? (3000 / 30 secs = 100)

Measuring duration by the number of cycle passes is a completely wrong approach. This is a bad idea in principle, and in your case this duration depends on the values ​​that the read analogRead() function. So your estimates of 30 seconds and 10 minutes are very approximate

Why not use the standard Arduino mechanism - a millis counter - which can calculate the real time in seconds and minutes?

Also, it seems that you have a problem with a logic of program.

Which of the two options is correct - the values ​​should be above the threshold of 30 seconds in a row or, for example 10 seconds now, then another 10 seconds after an hour and another 10 seconds at the end of the day?

Thank you, the issue was that the variable was an int instead of unsigned long.