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);      }           
}

migrated from electronics.stackexchange.com Aug 24 '14 at 0:34

This question came from our site for electronics and electrical engineering professionals, students, and enthusiasts.

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.

  • Arduino 'language' a.k.a. C++ (would add it in brackets to the answer but too short for edit) – user2973 Aug 24 '14 at 18:35
0

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

  • 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. – Connor Wolf Aug 24 '14 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().

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.