I'm doing a project but I came across this line and I'm in doubt what it does.
The line in question is line 4
'serial_byte = (serial_byte <0)? 0: ((serial_byte> 180)? 180: serial_byte); '
can you help me know what this line says?
void read_serial_byte_set_servo(Servo& servo, bool invert)
serial_byte = Serial.read();
serial_byte = (serial_byte < 0) ? 0 : ((serial_byte > 180) ? 180 : serial_byte);
servo.write(180 - serial_byte);
It's a couple of if statements, made more confusing. Look up the C++ Ternary operator.
What doubts do you have?
If it does what you want,there should be no need for doubt.
If you simply don't understand it, tell us which bit you don't understand.
If serial_byte really is a byte, there should be no need to see if it is less than zero
If you do what I want, I would have no doubts to interpret.
My question is in the topic. Thanks for reading.
thanks. from what I understand it looks like one inside the other
It's simply ensuring serial_byte is in the range 0 to 180, and clipping it to those values if not.
As I wrote, the "<0" test is probably redundant.
Thank you all.
I've already figured it out. I didn't know what this was.
It is a Ternary Operator. In this case, one inside the other.
I realized thanks to everyone greetings
This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.