hello, guys, is there a single instruction in arduino to compliment a digital pin status. for example in 8051 assembly we can use 'CPL p1.0' to compliment the status of p1.0.
thanks
hello, guys, is there a single instruction in arduino to compliment a digital pin status. for example in 8051 assembly we can use 'CPL p1.0' to compliment the status of p1.0.
thanks
You could tell it how nice it is looking today ;)
(sp. "complement")
The language is not "Arduino", it is C/C++, and the usual language rules apply, and there are lots of ways of skinning that cat, depending on context and speed required. digitalWrite (myPin, ~digitalRead (myPin));
might be one of them.
It might be, but it isn’t since HIGH == 1 and LOW == 0 and ~1 == -2
You probably meant:
digitalWrite (myPin, !digitalRead (myPin));
Or perhaps a bit more obvious to the eye:
digitalWrite (pin, (digitalRead (pin) == HIGH ? LOW : HIGH)) ;
Or the completely verbose
if (digitalRead (pin) == HIGH)
digitalWrite (pin, LOW) ;
else
digitalWrite (pin, HIGH) ;
It might be, but it isn't since HIGH == 1 and LOW == 0 and ~1 == -2
It doesn't matter; digitalWrite follows the C convention that zero is false or LOW, and [u]anything[/u] else (i.e. non-zero) is true or HIGH.
digitalWrite (pin, (HIGH + LOW) - digitalRead (pin));