Millis function to send a high state for 5 seconds

Hello, I am trying by means of a function that will send a high level for 5sec to turn on a led then it turns off, all this without stopping the program, that is why I am using millis, the problem I have is that I do not know much about the syntax of c++ and the program does not interact as I have thought, could you help me, I would appreciate it.

my code:

unsigned long time1, tx = 0;
int stadeL1 = LOW;

char comand;
int out = 4;

uint8_t executionTime(int timep){
  time1 = millis();
  
  if( (time1-tx) == timep ){
    Serial.println("turn on");
    digitalWrite(out, HIGH);   //change status for 5 seconds
    tx=time1;
  }
  //change status after 5 seconds
}

void setup() {
  Serial.begin(9600);
  pinMode(out, OUTPUT);
  digitalWrite(out, stadeL1);
}

void loop() { 
    
  if ( Serial.available() > 0 ){
    comand = Serial.read(); 
    
    if( comand == '1' ){
      Serial.println("Execute Function");   
      executionTime(1000);
      
    } else {
      Serial.println("Error");   
    }
  
    comand="";
  }
 
}

Try this

unsigned long startTime;
unsigned long currentTime;
unsigned long period = 5000;
const byte ledPin = 3;

void setup()
{
  Serial.begin(115200);
  pinMode(ledPin, OUTPUT);
  digitalWrite(ledPin, HIGH); //turn off the LED
}

void loop()
{
  currentTime = millis();
  if (Serial.available())
  {
    char inChar = Serial.read();
    if (inChar == '1')
    {
      digitalWrite(ledPin, LOW);  //turn on teh LED
      startTime = currentTime;
    }
  }
  if (startTime != 0) //timer is running
  {
    if (currentTime - startTime >= period)
    {
      digitalWrite(ledPin, HIGH); //turn off the LED
      startTime = 0;  //stop timing
    }
  }
}

I try but I don't know how

It works, however after 5 seconds it does not turn off, I would also like to turn it into a function since I am going to call that function many times

It works on my test system
How is your LED wired ?

If it is wired so that HIGH on the LED pin turns it on then you need to change the state that is output by the digitalWrite()s in the sketch

Another version

boolean myFlag          = false;

char comand;

int out                 = 4;

//timing stuff
unsigned long interval;
unsigned long timeMillis;


//********************************************^************************************************
void setup()
{
  Serial.begin(9600);

  pinMode(out, OUTPUT);
  digitalWrite(out, LOW);

} //END of   setup()


//********************************************^************************************************
void loop()
{
  //**********************************
  if ( Serial.available() > 0 )
  {
    comand = Serial.read();

    if ( comand == '1' )
    {
      Serial.println("turn on");
      digitalWrite(out, HIGH);

      //enable TIMER
      myFlag = true;

      //restart TIMER
      timeMillis = millis();

      //set delay
      interval = 1000;
    }

    else
    {
      Serial.println("Error");
    }

    while ( Serial.available() > 0 )
    {
      //clear out buttfer if it has more characters
      Serial.read();
    }
  }

  //**********************************
  //LED TIMER
  if (myFlag == true && millis() - timeMillis >= interval)
  {
    //disable TIMER
    myFlag = false;
    
    digitalWrite(out, LOW);  
  }

} //END of   loop()


I changed it as it is there, however it does not turn off

Please post the sketch you tried

it worked fine, it was my mistake, thanks

No problem. I am glad it worked

You can, of course, put the code in a function if you want to

Hello, I've been trying to put it in a function for hours, but Millis has a strange interaction, I don't know what's happening, could you help me again please.

unsigned long startTime;
unsigned long currentTime;
unsigned long period = 5000;
const byte ledPin = 4;

void setup()
{
  Serial.begin(9600);
  pinMode(ledPin, OUTPUT);
  digitalWrite(ledPin, LOW); //turn off the LED
}

void loop(){
  
  currentTime = millis();
  
  if (Serial.available()){
    
    char inChar = Serial.read();
    if (inChar == '1'){
      Funcion();
    }
  }
   
}


void Function(){
   digitalWrite(ledPin, HIGH);  //turn on teh LED
   startTime = currentTime;
    
   if (startTime != 0){ //timer is running 
     if (currentTime - startTime >= period){
       digitalWrite(ledPin, LOW); //turn off the LED
       startTime = 0;  //stop timing
     }
  }
}

Do you understand how this works ?

boolean myFlag          = false;

char comand;

int out                 = 4;

//timing stuff
unsigned long interval;
unsigned long timeMillis;


//********************************************^************************************************
void setup()
{
  Serial.begin(9600);

  pinMode(out, OUTPUT);
  digitalWrite(out, LOW);

} //END of   setup()


//********************************************^************************************************
void loop()
{
  //**********************************
  if ( Serial.available() > 0 )
  {
    comand = Serial.read();

    if ( comand == '1' )
    {
      Serial.println("turn on");
      digitalWrite(out, HIGH);

      //enable TIMER
      myFlag = true;

      //restart TIMER
      timeMillis = millis();

      //set delay
      interval = 1000;
    }

    else
    {
      Serial.println("Error");
    }
  }

  //**********************************
  //is the TIMER enabled ?
  if (myFlag == true)
  {
    myFunction();
  }

} //END of   loop()


//********************************************^************************************************
void myFunction()
{
  //**********************************
  //LED TIMER
  if (millis() - timeMillis >= interval)
  {
    //disable TIMER
    myFlag = false;

    digitalWrite(out, LOW);
  }

} //END of   myFunction()

thank you very much bro it worked perfectly

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