millis() understanding

hi guys, I'm sorry I know there are lot's of example out there lying around, and believe me I tried my best to understand it and now I would like to ask you if my understanding is right.

and by the way I would like to thank "UKHeliBob" and "Robin" for there threads about this specific matter.

I'm aiming on blinking the LED while continuously controlling the servo.

in the comment above the "BlinkWithoutDelay" example
it says:
"Turns on and off a light emitting diode (LED) connected to a digital pin,
without using the delay() function. This means that other code can run at the
same time without being interrupted by the LED code."

well lets say I want that LED to blink without any changes in timing, and in the same time I'm constantly moving a potentiometer (means that an arduino code is continuously reading that data) that controlls the servos,
do you think the LED's code will not interrupt the SERVO's code. even for a millisecond?

well if it is, where shall I put my SERVO code, inside the curly braces? before the if statement? or after the if statement? :
" if (currentMillis - startMillis <= period) {} "

again guys I'm sorry about this but just don't know if I am right.

thanks again.

The Arduino cannot multi-task, so time has to be giving to the tasks in some order as determined by your code.

Try something like this (untested).

unsigned long cycleStart;
int onTime = 500, cycleTime = 1000;
const byte led = 13;

void setup()
{
  pinMode(led,OUTPUT);
}
void loop()
{
  digitalWrite(led,millis() - cycleStart < onTime;
  if(millis() - cycleStart > cycleTime)
    cycleStart += cycleTime;

  control_servo
  ...
  ...
  ...
}
  if(millis() - cycleStart >= cycleTime)

:wink:

I'm aiming on blinking the LED while continuously controlling the servo.

In loop(), test whether it is time to change the state of the LED using millis() timing. If it is then do it.

Whether or not you just changed the state of the LED, read the pot and write to the servo.

Two separate sets of actions.
Yes, changing the state of the LED will delay the pot read/servo move but loop() repeats thousands of times per second so you are not going to notice.

if(millis() - cycleStart >= cycleTime)

I am NOT a dumbass! :stuck_out_tongue: , TNX for headsup.

jasonsteph:
do you think the LED's code will not interrupt the SERVO's code. even for a millisecond?

Yes. It will interrupt the servo's code for a few microseconds. And for most processes that Arduinos manage - swinging a servo arm, turning a LED on and off - a few microseconds is effectively nothing.

(Please try to not ask negatively phrased questions in future when you are dealing with computer programmers: we detest them.)

Please try to not ask negatively phrased questions in future when you are dealing with computer programmers

Agreed wholeheartedly.

A system that I worked with had a question in its setup screen, the help text for which was "Answer YES if you do not want this not to happen"

jasonsteph:
and by the way I would like to thank "UKHeliBob" and "Robin" for there threads about this specific matter.

well if it is, where shall I put my SERVO code, inside the curly braces? before the if statement? or after the if statement? :
" if (currentMillis - startMillis <= period) {} "

I very much appreciate your kind words. However if you are referring to my demo Several Things at a Time I am somewhat at a loss to understand your question because that demo has an example using a servo.

...R

jasonsteph:
well if it is, where shall I put my SERVO code, inside the curly braces? before the if statement? or after the if statement? :

This is kinda like asking the dude who sells you a car "When do I press the accelerator and brake if I want to get to the shops?"

jasonsteph:
where shall I put my SERVO code, inside the curly braces? before the if statement? or after the if statement? :
" if (currentMillis - startMillis <= period) {} "

If you wanted the "SERVO code" to happen only when the LED changes you would put it inside the curly braces, but it sounds like you want your "SERVO code" to act independently of the LED so you would put it either before or after the 'if' statement. In this case it doesn't appear to matter which you choose because the state of the LED probably has no bearing on the "SERVO code". If, for some reason, the state of the LED is important to the "SERVO code" you should probably put the "SERVO code" after the 'if' statement so the LED state is determined just before the "SERVO code" is executed.