How to decipher: analogWrite( output( last < min ? min : ( last > max ? max : ( last))));

Surfing the internet I have found a pieco of code and I am having trouble understanding how this is used.

analogWrite( output( last < min ? min : ( last > max ? max : ( last))));

( where are the code tags??)

Can anyone please explain to me how to read this piece of code or point me to a website or post that explains this and how to write this in a noob way?

As far as I understand is that the analogWrite handles differently depending on the value of last..
If last is smaller than min, it has to do something or bigger than max do something else, my guess..

you pass to analogWrite() the result provided by the output() function with a parameter such as

if last is less than min, then you pass min
if last is more than min, then if last is more than max then you pass max
otherwise you pass last

so basically you call the output() function with last as parameter but constrained between min and max and use the result in analogWrite()

basically that's what they say in code:

if (last < min) analogWrite(output(min));
else if (last > max) analogWrite(output(max)); 
else analogWrite(last);

The analog write function requires two parameters...
I don’t see the comma in there.

But analogWrite requires TWO arguments! The pin, and the PWM value (0-255).

That line of code is BS.

unless it's a custom analogWrite() function
this should compile and work

void analogWrite(int val) {
  analogWrite(5, val);
}

void setup() {
  analogWrite(127);
}

void loop() {}

Post the entire bit of code, not just the single line. There's some strange things going on here.

Thank you all for sharing your knowledge.

@RayLivingston and @lastchancename You are right about the comma. While copying the code I forgot to type the comma. The piece of code should be

analogWrite( output, ( last < min ? min : ( last > max ? max : ( last))));

where output is the pin to which analogWrite is connected.
Sorry , my mistake.

But it makes all sense now. Thanks again.

definitely makes more sense indeed :wink:

It smels like "Obfuskation"

analogWrite( output, constrain(last,min,max));

@combie the way you write it with a constrain is also one to be easily understood.
Thanks for sharing.

Probably not obfuscation, just reinventing the wheel.

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