Automationhat relay write

I would like to trigger one of the relays by passing a variable to a method. It looks like it’s possible, but I’m puzzled by the description in the function reference: automationhat.relay.write(1) # 1 = ON, 0 = OFF. I can’t tell how to specify which of the three relays to write to. I’m sure I’m missing something simple. Can you help?

It uses the same paradigm as Explorer HAT, for better or worse:

automationhat.relay.one.write(0) # Turn off relay one
automationhat.relay.two.write(1) # Turn on relay two
automationhat.relay.three.write(1) # Turn on relay three

automationhat.relay.write(0) # Turn off all relays

I think we need to add an example to better illustrate this :D (not to mention documentation)

There are not of practical examples out there that show how to write a programme that reference inputs and then switch outputs / relays. I have written a small programme to show how to control a pump (via one of the relays) in a water tank that will run when the water level gets to a “tank full” switch and stop when the level drops to a “tank empty” switch and then wait for the tank to fill again.

In the example i am using 2 of the digital inputs, one output and one relay.

import time
import automationhat
while True:
if automationhat.is_automation_hat():
automationhat.light.power.write(1) # switches power light on
in_1=automationhat.input.one.read() #Tank full switch
in_2=automationhat.input.two.read() #Tank empty switch
hold_1=automationhat.output.one.read()
if in_1>=1:
automationhat.relay.one.on()
automationhat.output.one.on()
if hold_1>=1:
automationhat.relay.one.on()
if in_2>=1:
automationhat.relay.one.off()
automationhat.output.one.off()
continue

simpler version, no need for a retaining variable

import time
import automationhat
while True:
if automationhat.is_automation_hat():
automationhat.light.power.write(1) # switches power light on at start of programme
in_1=automationhat.input.one.read() #Tank full switch
in_2=automationhat.input.two.read() #Tank empty switch
if in_1==1:
automationhat.relay.one.on() #energise relay No 1 to start pump
automationhat.output.one.on() #LED indicator on “pump running”
if in_2==1:
automationhat.relay.one.off() #de-energise relay No1 to stop pump
automationhat.output.one.off() #LED indicator off “pump running”
continue

We should probably drop some of these into the GitHub examples folder.

Tip: you can use three backticks to format your code nicely in the forums:

```
like so
```