invert state of digitalWrite output independent of current state

I am quite new to Arduino programing and I have not found the solution yet searching several forums

Of course I can change the output state by these commands:

digitalWrite(relay1Pin, HIGH);
digitalWrite(relay1Pin, LOW);

Depending on the situation the outut can be any state and I want to be able to change the state without knowing what the current state is.

I guess it could look something like this:

digitalWrite(relay1Pin, INVERT);

Unfrtunate I did not find any reference that this command even exists.

As alternative I can use the "else if" command to check the current state first and change over depending on the current state but being able to invert an output directly would be more convenient.

Any suggestions ?

You can use digitalWrite(relay1Pin, !digitalRead(relay1Pin));

This should toggle the pin state.

Ian.

Thanks, I will try that ;D

That's why part of my default functions is

inline void digitalToggle(byte pin){
  digitalWrite(pin, !digitalRead(pin));
}

Depending on the situation the outut can be any state and I want to be able to change the state without knowing what the current state is.

Deliberant ignorance of the pin state is avoidable - you are calling digitalWrite, you can remember
the last state written...

#define NPINS (1+A5)  // only right for Uno etc
byte pinstates [NPINS] ;

void myDigitalWrite (byte pin, byte state) // use this rather than digitalWrite()
{
  pinstates[pin] = state ;  // remember state
  digitalWrite (pin, state) ;
}

void digitalInvert (pin)
{
  myDigitalWrite (pin, (byte) (! pinstates[pin])) ;
}

Why all that effort when that info is already stored in the Arduino? With the extra dangerous you accidentally use digitalWrite() and mess up the state array...

Because its not provided to the user in a portable function. If you use direct port manipulation it is
available, but the code isn't portable.

Remember digitalRead() is not telling you what you last wrote, its telling you the actual state of the
pin by sensing its voltage - if a pin is shorted to ground it returns LOW irrespective of what you wrote
to it, and if the pin had its mode changed to INPUT, it returns whatever voltage the pin happens to be
(it might be floating).

As long as it's an output it's not a problem. If you short the pin I can tell you, the Arduino will reset :stuck_out_tongue: (Been there, done that :p)

The part of it being an input is right but your function does not fix that either. It will just toggle the pull ups where as mine just sets the pull up to the inverted state of the input. In both cases that's probably not what you want. although I have to give you that toggling the pull up might be useful in some cases but for most it's not and I don't think it worth the extra RAM and the risk of messing it up by using digitlWrite() again. And I would say digitalToggle() is in line with digitalWrite() also working on an input.

septillion:
As long as it's an output it's not a problem. If you short the pin I can tell you, the Arduino will reset :stuck_out_tongue: (Been there, done that :p)

The part of it being an input is right but your function does not fix that either. It will just toggle the pull ups where as mine just sets the pull up to the inverted state of the input. In both cases that's probably not what you want. although I have to give you that toggling the pull up might be useful in some cases but for most it's not and I don't think it worth the extra RAM and the risk of messing it up by using digitlWrite() again. And I would say digitalToggle() is in line with digitalWrite() also working on an input.

Well yes but the problem I'm solving is to remember the last state we set the pin to, correctly, in a way
that won't cause confusion and bugs in unusual circumstances - ie not a hack. The discipline of only
using myDigitalWrite is the weak point as you point out.

The alternative is a much more complex piece of code to read the state from the PORTx registers, which
ought to live in wiring_digital.c if anywhere.

If you want a real toggle then it's only fair to also add a pinMode() to that function. That's the only way to be sure you also don't mess up and do "weird" stuff.

And the last I agree, although it would be nice to have it should than be part of the IDE (and all board) as to keep portable which is just a hellish job :smiley:

And I'm a sucker for not misusing macro's for stuff like that but that's another topic (A)