Turning relay on with HIGH instad of LOW state

Hello guys,

I am struggling with following obstacle. I have succesfully conected my phone android app with arduino via ethernet shield. Now I'dlike to turn devices on with use of relays. But there is one problem. To switch them on I need to send LOW state on my pins. I need HIGH state in order to make them work propelly wit my app. Normally it is ok for me to turn them on with LOW state. However in this case only high cooperates with the app developer scetch.

The app is:

Thank you for any idea could help
Konrad

Konradus:
But there is one problem. To switch them on I need to send LOW state on my pins. I need HIGH state in order to make them work propelly wit my app.

You need to post your Arduino program.

I imagine it is simply a case of changing something in the program.

...R

Well I asume you can't give me a clue without seeing my code, Here it is.

polaczony_z_polskimi_znakami3.ino (10 KB)

Hi,
I guess you have those 6 digital outputs (15 to 20) that you want to control from the Android app.

It looks like the app sends a 'digital' command to tell the output to change

The function digitalCommand() reads the pin and value and then does digitalWrite( pin, value );

All you need to do is change that to: if 'value' is LOW then write the pin HIGH, else write the pin LOW.

Yours,
TonyWilk

just make your declarations before setup.

LOW = RELAY_OFF;
HIGH = RELAY_ON;

or in loop()

if signal==HIGH
digitalWrite(relay, LOW);

dave-in-nj:
just make your declarations before setup.

digitalWrite(relay, LOW);

Perhaps you have got things back-to-front?

I would have expected digitalWrite(relay, RELAY_OFF);

...R

dave-in-nj:
if signal==HIGH
digitalWrite(relay, LOW);

Being a bit pedantic here (sorry) but...

If some code is doing digitalWrite( pin, value );, unless you know exactly what values 'value' can have, then it is not safe to write if( value == HIGH )

It is safe to write if( value == LOW ) or if( value != LOW )

...because, akin to general 'truthyness', a zero is LOW but any non-zero byte value is taken as HIGH.

Yours,
TonyWilk

TonyWilk:
Being a bit pedantic here (sorry) but...

If some code is doing digitalWrite( pin, value );, unless you know exactly what values 'value' can have, then it is not safe to write if( value == HIGH )

It is safe to write if( value == LOW ) or if( value != LOW )

...because, akin to general 'truthyness', a zero is LOW but any non-zero byte value is taken as HIGH.

Yours,
TonyWilk

Well being pedantic....
The arduino API documenation never specifies what specific values LOW or HIGH are nor that non-zero is to be assumed to mean the same as HIGH.
I have see lots of code that makes those types of assumptions but it is simply not ever specified that way in any of the documentation.
While it tends to work when those assumptions are made, it depends on the underlying code implementation.
So, to make such an assumption is abusing the API because it implies a knowledge of the underlying code implementation.
i.e. the code should not presume any specific values for LOW or HIGH

The value parameter to digitalWrite(pin,value) is defined to be HIGH or LOW.
The behavior with any other value including naked constants such as 0 or 1 or other non-zero values is undefined.

Likewise, the return value from digitalRead(pin) is LOW or HIGH.
The specific values for LOW or HIGH are not defined and should not be assumed.

--- bill

bperrybap:
The arduino API documenation never specifies what specific values LOW or HIGH are snip

Granted.

Do you agree with my recommendation in this instance or do you have an alternative suggestion to invert 'value' for digitalWrite() ?

Yours,
TonyWilk

TonyWilk:
Granted.

Do you agree with my recommendation in this instance or do you have an alternative suggestion to invert 'value' for digitalWrite() ?

Yours,
TonyWilk

I assume we are talking about patching code in digitalCommand() to invert the output pin state for the relays.

Interfacing with an external entity as in this case, is tricky and messy.
This is because HIGH and LOW are not specifically defined in Arduino but yet something must be specifically defined on the remote side to send in the message.

I do agree with you that the the typical 'C' way is to do things like compare to zero or not zero.
(but LOW is not technically the same as zero)

So in this case I would not use what you recommended in post #3

All you need to do is change that to: if 'value' is LOW then write the pin HIGH, else write the pin LOW.

As it is presuming that the remote end is using the same specific values for LOW and HIGH - which are not formally defined to be specific values.
In reality the message "API" that is being used is 0 for "off" and 1 for "on".
So the code in digitalCommand() should be checking for those values not LOW or HIGH.
And then translating those API 0 or 1 values to whatever needs to be done to trigger the relays appropriately.

i.e. perhaps something like this:

void digitalCommand(EthernetClient client) {
  int pin, value;
  pin = client.parseInt();
  if (client.read() == '/') {
    value = client.parseInt();
        if(value == 0) // turn off?
            digitalWrite(pin, HIGH); // high turns off relay
        else
            digitalWrite(pin, LOW); // low turns on relay
          
    mode_val[pin] = value;
    client.print(httpOk);
  }
}

This modification assumes that any/all pins used by this code are the relays using inverted levels which may not be the case.
If it isn't the case, then there would need to be some additional code to be able to handle different pins differently.
It also assumes that any value other than 0 means turn on the relay, which might also not be good.
In terms of sanity checking the values passed, the code could make this assumption or the reverse assumption, or it could explicitly check for 0 and 1 and ignore illegal values.

--- bill

bperrybap:
... talking about patching code in digitalCommand() to invert the output pin state for the relays ...

Good argument... like the point of view of inverting the 'API value' instead of the 'HIGHness/LOWness'

Yours,
TonyWilk

It actually leaves the original API value sent alone.
Note that the original value sent is what is stored in the mode_val[] array.
I was concerned that if the actual value was inverted and stored that it might confuse the other end since I think the remote end can query the mode_value[] array to get pin states.

--- bill

Thanks guys. I'll just test your clues and let you know if it works the way I expect it. Thanks a lot for every idea you gave me. Cheers

bperrybap yuour code works better than good. It just makes it work exacly I wanted. Thanks a lot :wink: