0

My question is simple. Does Arduino set the digitalwrite() the moment it is called or at the end of the while loop. E.G.

int ledPin = 13; 
void setup()
{
 pinMode(ledPin, OUTPUT); 
}
void loop()
{for(int i=0; i<10; i++){
if(i%2==0){
 digitalWrite(ledPin, HIGH); 
 delay(1000);}else{ 
 digitalWrite(ledPin, LOW); 
 delay(1000); } 
}
jippie
2,90114 silver badges23 bronze badges
asked Aug 24, 2014 at 0:23

3 Answers 3

2

A key point of the Arduino 'language' is every statement is completed before the next one can begin.

A simple statement has a ';' at the end of it.
Or followed by ')' instead of ';' in for (...; ...; statement) loop.

So digitalWrite() will set the pin, HIGH or LOW, before it completes and allows the next statement, delay(...) to begin.

Side note:
Actually digitalWrite() takes a small amount of time to do its work, so it is not exactly 'the moment' it is called. digitalWrite() is pretty quick, a few millionths of a second, which is usually near enough to be practically 'the moment' for most applications.

answered Aug 24, 2014 at 1:06
1
  • Arduino 'language' a.k.a. C++ (would add it in brackets to the answer but too short for edit) Commented Aug 24, 2014 at 18:35
0

Arduino sets the pin at the moment digitalWrite() is called.

answered Aug 24, 2014 at 0:29
1
  • 3
    To be extremely pedantic, it sets the IO pin state a short time into the processing time of the function call, which can be important if you're trying to run things very fast (digitalWrite() is actually quite slow, in comparison to writing pin control registers directly). It still sets the pin within the execution of the digitalWrite() function call, in any event. Commented Aug 24, 2014 at 0:50
0

The condition

if(i%2==0)

will be checked immediately upon entering the "for" loop, and again on every iteration of that loop. It will be the first code executed on each iteration. One form or another of the digitalWrite() will be called next, followed by the delay().

answered Aug 24, 2014 at 0:30

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.