Those are not equivalent. The first sets the actual output pin referenced by PulseOut_Pin to a HIGH level. The second changes the value of the variable PulseOut_Pin, which has no effect on the output pin itself.
That definitely would be a bit confusing, unless you for some reason needed to cycle the output pins on PORTD through every possible conbination of HIGH and LOW. Might see that if someone wanted to display binary counting on LEDs, but that would execute way too fast to see anything.
Yes - you are correct - I didn't bother to include all of the programming statements - such as a delay of some kind to slow down the processes.
I just included those particular snippets as examples of syntax which would deviate from the what one would see if one were taking an introductory course in Arduino programming.
But it may be the case that the general population of the Arduino community would not be confused by slight differences in syntax.
Mostly beginners and not so much beginners go along with the digitalRead() and digitalWrite() functions.
Usually an obvious case for using direct port manipulation invites comments saying so, and help to do.
It is one of several little bumps that ppl coming here to learn get helped over.
If you are writing code that you intend to have people read and learn from, there is always a balance between doing things in simple ways and exploiting advanced, even mildly, features of C/C++.
If you write code carefully, you can isolate and explain anything you feel absolutely must be done the way an experience engineer would do it, you can still reach a good audience.
As you must know, sometimes doing things in simple "Arduino" manner can be a real mess, and actually present a good opportunity to introduce some "new" concepts to make code tidier.
I'm thinking about getting an entire byte from an input port using digitalRead() and maths of one kind or another.
On the same hand, if you look at most of the projects here, the pin-orientated nature of digitalRead() and digitalWrite() aren't really much of a problem.
If you read long enough here, you will see noobs who don't know the difference between for and if being offered solutions involving classes and pointers to functions and things like that. Especially disappointing when the solution isn't what the inquiry was after.
So just you be you. I like to write code even for myself that uses the least complicated and advanced techniques possible that still gets the job done, is readable and maintainable and so forth.
Yes, we do direct port register writes but have to take care about modes and settings and the order of commands while doing all that so Arduino supplies Beginner Friendly IO functions with built-in safety and consequent speed loss.
Reading the PINx register and turning bits to a flag is 5x faster than digiotalRead() for example.
Arduino is Beginner Friendly. You want to go pro, get the free suite from Microchip.
Functions provide an abstraction for the underlying hardware - if you know digitalWrite() it will work in the same way for all Arduino compatible boards. Registers are hardware specific.
So you have a trade off between readability /portability and efficiency
My teacher has told me if I am presenting codes to readers, then I have to be a "literate programmer" who writes codes based on basic (simple and elementary) syntax and semantic rules of the Arduino/C++ Programming Language; else, I must be smart enough to maintain/optimize portablity, speed, space, and readability in the written codes.
1.
#define PulseOut_Pin 5 //symbolic name PulsOut_Pin is assigned to DPin-5
pinMode(PulseOut_Pin, OUTPUT);
digitalWrite(PulseOut_Pin, HIGH); // to comply with language syntax rule
2.
for(byte i = 0; i < 8; i++)
{
pinMode(i, OUTPUT); //DPin-0, 1, ..., DPin-7 are output lines
}
3.
for(int i = 0; i <= 0b11111111; i++) //sub-ordinate clause within pair of { }
{
PORTD = i;
delay(500);
}
Yes! That is an excellent point!
The functions are not MCU specific
The underlying library takes care of many of house-keeping requirements - such as specific analog register or comparitor register settings - for the specific device. And those registers are going to be different from one MCU to the next.