Start and stop event with if and millis

Hi,

I am having a hard time to understand why the following code is not working.
I would like to achieve the following:

  1. loop is continuously running based on an interval (not in code here)
  2. after an input is given through serial, an action is executed (i.e. led on)
    after a fixed time, another action is executed (i.e. led off)

However after the time (in this case 5s) passed and currentMillis gets larger than startTime, nothing happens.

unsigned long startTime = 0;

void setup()
{
Serial.begin(115200);
}

void loop()
{
unsigned long currentMillis = millis();

Serial.print(currentMillis);
Serial.print("   ");
Serial.println(startTime);

if(Serial.available()>0)
{
 char in = Serial.read();
 
 if(in == 'e' && startTime == 0)
 {
 startTime = currentMillis + 5000;
 Serial.println("e");
 }
 if(currentMillis >= startTime)
 {
 Serial.println("e-done");
 startTime1 = 0;
 }

 } // serial done
} // loop done

Many thanks for any help.

    if (currentMillis >= startTime)

This test is inside the test of whether Serial data is available. Is it meant to be ?

yes. However I would not know of any advantages nor disadvantages.

Basically, unless there is something being received by the Serial interface then the test for the end of the period does not happen.

Try this

unsigned long startTime = 0;
boolean timing = false;

void setup()
{
  Serial.begin(115200);
}

void loop()
{
  unsigned long currentMillis = millis();
  Serial.print(currentMillis);
  Serial.print("   ");
  Serial.println(startTime);
  if (Serial.available() > 0)
  {
    char in = Serial.read();
    if (in == 'e' && startTime == 0)
    {
      startTime = currentMillis + 5000;
      Serial.println("e");
      timing = true;
    }
  } // serial done
  if (currentMillis >= startTime && timing)
  {
    Serial.println("e-done");
    startTime = 0;
    timing = false;
  }
} // loop done

I like to do it this way, using startTime != 0 as the timing flag.

unsigned long startTime = 0;

void setup()
{
  Serial.begin(115200);
}

void loop()
{
  unsigned long currentMillis = millis();
  
  Serial.print(currentMillis);
  Serial.print("   ");
  Serial.println(startTime);
  
  if (Serial.available() > 0)
  {
    char in = Serial.read();
    if (in == 'e' && startTime == 0)
// or just "if (in == 'e')" if you want to
// be able to re-start the timer before it ends
    {
      startTime = currentMillis;
      Serial.println("e");
    }
  } // serial done
  
  if (startTime != 0 && currentMillis - startTime > 5000)
  {
    Serial.println("e-done");
    startTime = 0;
  }
} // loop done

Many thanks to both of you.
I somehow managed to forget what the if(Serial.available()>0){...} does to my code here. Just to put it in my own words:
The if(Serial.available()>0){...} is never entered again without any new input from serial. Only when (if Serial.available()>0) constantly remains true, then the "e-done" would be printed after the defined 5s.

Looks so obvious now...

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