Code interpretation project

Hey guys.
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);
  if (invert)
    servo.write(180 - serial_byte);
  else
    servo.write(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 :slight_smile:

1 Like

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.

1 Like

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 :wink: greetings

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.