(SOLVED)millis inside while

Hi everyone,

Have an issue with a code. I am trying to get a result of how long time was turned on. But I cant get any results in serial monitor. Looks like my programming skills need to be tweaked. Can someone please look into code and give advice where is the problem?

/*

int analogPin = 1; int val = 0; long startTime; long duration;

// the setup routine runs once when you press reset: void setup() {

Serial.begin(9600); }

// the loop routine runs over and over again forever: void loop() {

val = analogRead(analogPin);

if(val > 50) { //Checking if enough light appeared startTime = millis(); while(val > 50); //while light is still on long duration = millis() - startTime; Serial.println(duration); // should print duration }

delay(100); }

   if(val > 50)
              {
                //digitalWrite(led, HIGH);

                     // here if the switch is pressed
                 startTime = millis();
                       while(val > 50);

If val is greater than 50, record the current time. Then, start an infinite loop.

Check back with us when that loop finally finishes.

 while(val > 50);

hmm.

Honestly don't think the loop is the problem.

It stuck when it get in while zone. Modified code a little bit. When threshold is not reached for a while statement LED goes on and off fine. But if I lower threshold, and gets in while, I never get time and LED stays on. Modified fragment below:

if(val > 50) { digitalWrite(led, HIGH); //Checking if enough light appeared startTime = millis(); while(val > 60); //while light is still on long duration = millis() - startTime; Serial.println(duration); // should print duration } else { digitalWrite(led, LOW); }

delay(100);

Honestly don't think the loop is the problem.

Then, you are wrong.

If you want to loop while some condition is true, you need to update the factors that determine whether the condition is true in the body of the while statement, unless the factors are interrupt-driven (which is not the case in your code).

      // Block until the analog pin reading is less than 50
      while(val > 50)
      {
         val = analogRead(analogPin);
      }

while(val > 60); //while light is still on//while val (which never changes) is greater than 60

st0gas:
Honestly don’t think the loop is the problem.

It stuck when it get in while zone. Modified code a little bit. When threshold is not reached for a while statement LED goes on and off fine. But if I lower threshold, and gets in while, I never get time and LED stays on. Modified fragment below:

if(val > 50)
{
digitalWrite(led, HIGH);
//Checking if enough light appeared
startTime = millis();
while(val > 60); //while light is still on
long duration = millis() - startTime;
Serial.println(duration); // should print duration
}
else
{
digitalWrite(led, LOW);
}

delay(100);

if your goal is to attempt to solve the problem with while(), then that is one thing, keep working on that.

If your goal is to measure the time the input was above your desired threshold, and you don’t care about the approach, then you could consider this (untested) method:

int pin = A1;
unsigned long startTime;
int state;
int oldState;

void setup()
{
  Serial.begin(9600);
}
void loop()
{
  int val = analogRead(pin);
  if (val > 50)
  {
    if (state == 0)
    {
      startTime = millis();
    }
    state = 1;
  }
  else if (val < 47)//hysteresis.. your settings may be different  (edit: changed if to else if)
  {
    state = 0;
    if (oldState == 1)
    {
      Serial.print(F("Lite on was: "));
      Serial.print((millis() - startTime)/1000.0);
      Serial.print(F("seconds"));
    }
  }
  oldState = state;
}

Thank's to everyone.

I managed to fix while loop, and even got additional solution.

Thank you everyone.

st0gas: Thank's to everyone.

I managed to fix while loop, and even got additional solution.

Thank you everyone.

great

I'd be interested to see how you did it. Always nice to share the solution for when someone else has a similar issue.