What does this do? uint8_t lastState = (curState != 0) ? 0 : 1;

  uint8_t curState = digitalRead(PPM_PIN);
  uint8_t lastState = (curState != 0) ? 0 : 1;

So...digitalread return a bool, and ...what exactly does the second line do?
if curState is not 0 ... then ..(questionmark?) ..toggles it between 0 and 1 ? ...or something like that?

No, digitalRead returns an int.

It depends, on mega-avr it returns PinStatus

why not

 uint8_t lastState = ! curState;

imho in this case it should be written with LOW and HIGH only as the API for digitalRead is defined to return LOW or HIGH

  uint8_t curState = digitalRead(PPM_PIN);
  uint8_t lastState = (curState != LOW) ? LOW : HIGH;  // inverse the curState

thanks - as to "why not" : IDK - I am just trying to understand somebody else's code - and this way of making conditional decisions was new to me.
the source of my question is here: https://www.instructables.com/Arduino-Based-JETI-PPM-to-USB-Joystick-Converter-f/

Mastering a language and mastering a problem does not always come together.

I have this understanding: digitalRead() returns a value whose data type is int. If the returned value is within 3.0V - 5.0V, then the value is defined as HIGH; if the returned value is within 0.0V - 0.9V, then the value is defined as LOW.

... which is even further from being a bool (sorry, I tried to reply earlier but got blocked by some noob filter)

Doesn't that depend on the supply voltage of the processor?

Yes! But, the default supply voltage is 5V.

Only on a limited range of the Arduino "family" - not, for instance, on a Due, any of the 3.3V AVRs or the ESP lines of processors.
You may supply 5V to them, but that's not the logic level.

The logic and current levels are:
At a given Vcc and Temperature --
VOH and IOH
VOL and IOL
VIL and IIL
VIH and IIH

1 Like

It's just a more explicit way to execute a boolean if you want to run a function or anything else than just getting/setting a value.
You could say:
uint8_t lastState = (curState != 0) ? 0 : FunctionStateOn();

Anything after the ? is either true : false; you could also return null. In your case, it just return the number. Probably an habit from the programmer to always use it like this.

The voltage you supply to the board may not be the processor's Vcc, so your post is pretty much just so much obfuscating noise, with almost zero information.

Don't forget, between the USB +5V and the processor's Vcc pin, there may be a regulator.

The reference Board is Arduino UNO; where, the 5V coming from PC over the USB Port is directly connected (via MOSFET switch) to the Vcc (digital) pin of ATmega328P MCU of the UNO Board. (Please, see the schematic here.)

I'm not sure I understand the use of the definite article in my quote there.
Surely Arduino have reference designs for all their boards?

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