Go Down

Topic: how to replace delay() to millis() (Read 510 times) previous topic - next topic

Romonaga

Here is your code with millis() function in place of delay() function.

Code: [Select]



int led = 13;
int led2 = 12;
int led3 = 11;
int timer = 1000;
// the setup routine runs once when you press reset:
void setup()
{
  // initialize the digital pin as an output.
  pinMode(led, OUTPUT);
  pinMode(led2, OUTPUT);
  pinMode(led3, OUTPUT);
}

// the loop routine runs over and over again forever:
void loop()
{
  digitalWrite(led, HIGH);   // turn the LED on (HIGH is the voltage level)
  timeDelay();//delay(timer);               // wait for a second
  digitalWrite(led, LOW);    // turn the LED off by making the voltage LOW
  digitalWrite(led2, HIGH);
  timeDelay();//delay(timer);
  digitalWrite(led2, LOW);
  digitalWrite(led3, HIGH);
  timeDelay();//delay(timer);
  digitalWrite(led3, LOW);
}

void timeDelay()
{
  unsigned long presentMillis = millis();//read present time of Arduino Timer
  while(millis()- presentMillis <= 1000)  //checking if 1000 ms has elapsed
  {
    ; //do nothing
  }
}

He might as well use delay as this code blocks.
The universe exists only because we are aware of it.
We want a few mad people now. See where the sane ones have landed us!

GolamMostafa

#16
Jul 18, 2019, 10:02 pm Last Edit: Jul 18, 2019, 10:09 pm by GolamMostafa
He might as well use delay as this code blocks.
The OP has not asked for non-blocking codes. Probably, he has wished to get familiar with the usage of millis() function.  

Romonaga

The OP has not asked for non-blocking codes. Probably, he has wished to get familiarization with the usage of millis() function. 
:) fair enough. 
The universe exists only because we are aware of it.
We want a few mad people now. See where the sane ones have landed us!

Robin2

The OP has not asked for non-blocking codes. Probably, he has wished to get familiar with the usage of millis() function. 
That sounds exceptionally unlikely.

The whole purpose of using millis() instead of delay() is to replace blocking code with non-blocking code.

Using millis() to create a different version of delay() would be the equivalent of re-inventing the wheel.

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

UKHeliBob

Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

GolamMostafa

#20
Jul 19, 2019, 05:44 am Last Edit: Jul 20, 2019, 07:47 am by GolamMostafa
The whole purpose of using millis() instead of delay() is to replace blocking code with non-blocking code.
The inherent and actual purpose of the millis() function is known to the veteran; but, not to the OP (a beginner) as I can say from my experience; your experience could be different. Allowing a LED to remain ON for 1-sec time and then looking around within that 1-sec time to see if there is anything to do while checking if the said 1-sec time is over -- a non-blocking concept that can only be visualized by a veteran.

150 senior level pupils were given the following task; unfortunately, none of them could come up with correct solution within 50 minutes of the period and yet, there are students among them who will become teachers of the institute after graduation!

"Create sketch so that the L (built-in LED of UNO) of the following circuit remains ON for 1-sec time as long as the button K1 remains at open condition."


Edited diagram in response to the reaction of Post#21.

UKHeliBob

Quote
150 senior level pupils were given the following task; unfortunately, none of them could come up with correct solution within 50 minutes of the period
Given that atrocious and non standard diagram I am not surprised
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

GolamMostafa

Given that atrocious and non standard diagram I am not surprised
Any suggestion?

Robin2

Any suggestion?
Yes.

Start your own Thread in which to pursue your questions. Don't hijack some other person's Thread.

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

GolamMostafa

Yes.

Start your own Thread in which to pursue your questions. Don't hijack some other person's Thread.
Suggestion has been sought to improve the quality of the diagram (of Post#20)?

Robin2

Suggestion has been sought to improve the quality of the diagram (of Post#20)?
Indeed. But the diagram should never have been posted in this Thread in the first place - it has nothing useful to say to the OP.

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

larryd

#26
Jul 19, 2019, 05:56 pm Last Edit: Jul 19, 2019, 06:02 pm by larryd
Suggestion has been sought to improve the quality of the diagram (of Post#20)?
Why are you using an incandescent lamp symbol where a LED symbol should be used?

"remains ON for 1-sec time as long as the button K1 remains at open condition."
'Turns on' for 1-second when the switch K1 opens and stays open.





No technical PMs.
If you are asked a question, please respond with an answer.
If you are asked for more information, please supply it.
If you need clarification, ask for help.

GolamMostafa

#27
Jul 19, 2019, 07:04 pm Last Edit: Jul 19, 2019, 07:09 pm by GolamMostafa
1. Why are you using an incandescent lamp symbol where a LED symbol should be used?

2. "remains ON for 1-sec time as long as the button K1 remains at open condition."
'Turns on' for 1-second when the switch K1 opens and stays open.
1. From the following 3 symbols of the Library of Visio, I have chosen the first one owing to its resemblance a physical LED.


2.  Thank you for the nice paraphrase of my sentence. (+)

larryd

No technical PMs.
If you are asked a question, please respond with an answer.
If you are asked for more information, please supply it.
If you need clarification, ask for help.

meltDown

#29
Aug 06, 2019, 02:19 pm Last Edit: Aug 06, 2019, 02:20 pm by meltDown
The OP has not asked for non-blocking codes. Probably, he has wished to get familiar with the usage of millis() function. 
At least, not yet
Seems he has now... not in as many words, but needs a button to "immediately" (his word) turn the current led off.

So yeah "well done" to AKRF and GolamMostafa for effectively putting the OP back 3 weeks. He now needs to re-jig the sketch he had to accommodate the immediacy of the button.






Go Up