Millis Is It Possible Or Not?

Hello to all team members. I tried to set delayed shutdown or delayed start using millis function.
Compiled, I couldn't run? I tried to adapt it to sample codes.
Can you share a small code blog? I would appreciate if you help me. It flashes like a flasher in all shared codes.
Why is the flasher used, does it turn on or off when the time expires with millis?
Anyone using reverse?

`unsigned long startMillis;  //some global variables available anywhere in the program
unsigned long currentMillis;
const unsigned long period = 1000;  //the value is a number of milliseconds
const byte ledPin = 13;    //using the built in LED

void setup()
{
  Serial.begin(115200);  //start Serial in case we need to print debugging info
  pinMode(ledPin, OUTPUT);
  startMillis = millis();  //initial start time
}

void loop()
{
  currentMillis = millis();  //get the current "time" (actually the number of milliseconds since the program started)
  if (currentMillis - startMillis >= period)  //test whether the period has elapsed
  {
    digitalWrite(ledPin, !digitalRead(ledPin));  //if so, change the state of the LED.  Uses a neat trick to change the state
    startMillis = currentMillis;  //IMPORTANT to save the start time of the current LED state.
  }
}

flasher millis output led

unsigned long startMillis;  //some global variables available anywhere in the program
unsigned long currentMillis;
const unsigned long period = 1000;  //the value is a number of milliseconds
const byte ledPin = 13;    //using the built in LED

void setup()
{
  Serial.begin(115200);  //start Serial in case we need to print debugging info
  pinMode(ledPin, OUTPUT);
  startMillis = millis();  //initial start time
}

void loop()
{
  currentMillis = millis();  

  if (currentMillis - startMillis >= period)  
  
{digitalWrite(ledPin, HIGH); }
else
{digitalWrite(ledPin, LOW); }
 
startMillis = currentMillis;  
  
}

Please restate the issue you are having.

You have to be much more specific what this means

Do you mean general start of your program working
Do you mean making your microcontroller not executing any code = "shutdown"
You can't shutdown a microcontroller like a computer. You would have to add hardware that switches off the power-supply

The more details you describe in your next posting what you really want to do the faster you will have a solution, because the less asking back for the left out details is nescessary
best regards Stefan

1 Like

Hello,
Afraid I have no idea what the problem you are trying to describe is.

Try to break it down into its simplest form and state it in logical order

Eg
When I do x
I want y to happen
My current code does z

You might also state the end goal as sometimes people try to solve the wrong problem.

like this?

hope that helps....

1 Like

easy to explain, sorry for translation mistakes, start millis. if (currentMillis - startMillis >= 1000)
0> 1000ms led lights up. then it will close

or ,, 0> 1000ms led off. then it lights up. continuous.

thanks

I don't think there is a translation problem, there is an explanation problem. Try again, following closely the advice in reply #4.

1 Like

yes this design works as i want.. i will compile and make a sample application. sharzaad
Thanks Edison, I thought the opposite. led turns on and off

"Edison" is a forum user level, not a name.

1 Like

I'm in a funny situation. My computer is stone age internet outage. I'm getting the names mixed up now. I'm running out of battery to write code?

Please restate your issue.

This timeout will never expire because you reset the timer at every run of loop().
So LED will never light on.

if (currentMillis - startMillis >= 1000)

example
0> 1000ms led lights up. then it will close

Which means? You want the LED to light up for one second and no more? Could you describe what you are wanting to do?

yes that's exactly what i wanted
.
Do you want the LED to turn on in one second and no longer light up?

try this:

void loop()
{
  currentMillis = millis();  
  if (currentMillis - startMillis < period) 
     {digitalWrite(ledPin, HIGH); }
  else
     {digitalWrite(ledPin, LOW); }
  
}

i tried this i'm trying again thanks

Because of this !digitalRead(ledPin) it toggles state every timeout.

If you want it to only make one action and never trigger again, add a condition to the if statement (and you don't need to remember when you turned it off):

  if (digitalRead(ledPin) == HIGH && currentMillis - startMillis >= period)  //test whether the period has elapsed
  {
    digitalWrite(ledPin, LOW);  // turn it off
    // startMillis = currentMillis;  //not IMPORTANT to save the start time of the current LED state.
  }
1 Like

yes it worked, thanks to the whole team.

< fixed the magic state of this sign

So you have solved your actual detail problem

Well there are two major ways to finish a project.

snap-chattering like nzmxp with always too short postings to be really helpful
spinning very fast in circles with proceeding sloooooooowly

  • or slowing down
  • taking the time to write a detailed posting

and after that start through in a whoooosh with afterburner reaching the solution 50 postings earlier and three days earlier than with snap-chattering

If your favorite fun is snap-chattering just go on.

best regards Stefan

1 Like