# Digital Output Oscillating on U12

## Recommended Posts

I just got a U12 and I'm experimenting with it.

Right now I want to turn off and on a relay based on a voltage.

The voltage is read from channel 8 - analog (differential)

The output is read from channel 4 - digital (db-25)

I wrote the following sequence:

```while (1)
if (bat01[0] &gt; 1)
r01 = 1
else
r01 = 0
endif
delay(1)
endwhile```

My problem is that the value of r01 keeps changing from 0 to 1 even though bat01 is always > 0. Thus, the circuit that the relay is controlling is going off and on. What am I doing wrong? I've tried a lot of variations of the code above with the same results. I'm probably doing something so stupid that I can't see it. Any help would be appreciated.

##### Share on other sites

Are you perhaps reading from the same channel that you are writing too? I.e. do you have a digital input AND a digital output on channel 4? If so, you can't do this. The pin needs to be one or the other. When you set the output =0 or =1 DAQFactory automatically sets this pin as an output and then sets the appropriate value. If you then try and read the pin, it will automatically set the pin as an input, at which point the voltage on the pin I believe is pulled high, which is why the relay goes back on. The oscillation is because the sequence setting the value and the timing loop reading the input are fighting each other.

So, delete the input channel on 4.

That said, you also should do this a little different. Two things you can do:

1) instead of using a sequence, use the Event on the Bat01 channel. The event gets called every time a new value comes in for that channel, which is exactly when you want to compare it and toggle your output. You can then get rid of the while/endwhile and delay() and just put the if. But...

2) you can actually replace such a simple if with boolean logic. Remember, true = 1 and false = 0, so you can just do:

r01 = (bat01[0] > 1)

instead of the if, which means your event would contain just that line of code.

Important point: DO NOT put a while loop (or for that matter a delay()) in the event of a channel or you will stall the acquisition loop. Remember, the event is called in the acquisition loop, keyword being "loop", and so you don't want to create a loop of your own inside the event.

##### Share on other sites

I don't think I'm reading from channel 4. I am only using two channels right now. The timing on both channels is 1 second.

I tried putting similar code in the R01 channel event and it really bogged down my system. I have a very fast computer. But when I put the code you suggested in the BAT01 channel event, it works a lot better. However,

I think the output to the relay is not constant. The relay is controlling the circuit to a small light bulb right now. The light bulb flickers about every second, so the input to the relay coil must not be constant.

If I change the timing on R01 to 5 seconds, then the light bulb flickers every 5 seconds.

If I change BAT01 to 5 seconds and keep R01 at 1 second, then the light bulb goes on only briefly every 5 seconds (most of the time, the light bulb is off). I would have thought that R01 would retain its previous value until it gets an updated BAT01. But it seems like until BAT01 gets updated again, R01 goes to 0.

I would really like the relay to be clamped closed when the conditions for BAT01 are met. Any suggestions?

##### Share on other sites

OK

I didn't realize that if I set the timing to 0 on R01, then the problem goes away. Perhaps that is what you meant by your comment about using R01 as both an input and an output.

Thanks for the help

##### Share on other sites

Your output channel should have a timing of 0, not 1. The fact that it is one makes me think that you have selected Digital Input instead of Digital Output. Anyway, set it to Digital Output and a Timing of 0. As for the script, if you put that code in the R01 channel you have an infinite loop which will basically hang DAQFactory. This is because, remember, the code gets called every time a new value arrives at R01, and then you are setting R01 inside that script, so it just goes around in circles.

##### Share on other sites

Thanks for your help and patience with my dumb questions. Even the sequence works when I set the timing on the output to 0.