I recently used some code from a YouTube video to work with a rotary encoder. There are a few lines that I don't fully understand. I've tried looking it up but have not found answers. The code works perfectly as is, I just want to really know what going on with these lines so that I can better implement them in my future code. Always trying to learn more. Not just knowledge, but understanding.
This seems to be working like an "if" statement within the parentheses. So, in this case, if virtualPosition is greater than lastPosition, it prints "Up", otherwise, it prints "Down". Is that correct? If so, where can I learn more about this topic. I normally would have just used "if else" and had two separate prints. this is MUCH cleaner.
Read aloud:
"virtualPosition will take the minium (smaller) value of "100" or "max(x)" and "x" will be the maximum (larger) value of "0" or "virtualPosition"
Unabridged code might look like this:
if (virtualPosition > 0)
virtualPosition = virtualPosition; // do not change virtualPosition
else
virtualPosition = 0; // change virtualPosition to 0
if (virtualPosition < 100)
virtualPosition = virtualPosition; // do not change virtualPosition
else
virtualPosition = 100; // change virtualPosition to 100
Another way to write this is:
if (virtualPosition <=0)
virtulPosition = 0;
if (virtualPosition >= 100)
virtualPosition = 100;
You can recognize this as keeping a value within the boundaries (0) to (100) inclusive.
The NOT "!" means this is looking for a boolean 0 (LOW, FALSE). If it were looking for a 1/HIGH/TRUE, it would be written:
(digitalRead(pinSW)); // without the "!"
You are testing a "condition"... ("while" is a condition statement, as is "if")... and if you want only one action after the condition, you do not need the braces "{" and "}". Only if you have more than one action after the condition do you need to "embrace" the multiple actions with braces.
While this is syntactically correct, it is poor style. It doesn't cost any extra to put those braces and it makes it much clearer without having to stop and focus on that line.
In general you put those braces either way just to make things look nice.
Maybe the person writing this code is having to pay by the character for their keyboard.
... and when you want to add another action after the condition... and you do not add the braces, then only the first action depends on the condition, where the added action will occur regardless of the condition.
(condition) // no open brace
this line happens depending on (condition)
this line happens always
// no close brace
as opposed to the good way...
(condition) { // open brace
this line happens depending on (condition)
this line happens depending on (condition)
} // close brace