Delay / Pulse button - code help


#1

Hello guys,

I´ve tried both codes below but didn’t work(delay and pulse). Could you kindly help me to fix the code.
I want my relay to be active for 10 seconds once pushed. Them it goes back to LOW.

========================================================================
CAYENNE_IN(V11)
{
// get value sent from dashboard
int currentValue = getValue.asInt(); // 0 to 1

// assuming you wire your relay as normally open
if (currentValue == 0) {
t.pulse(RELAY_DIGITAL_PIN6, 10000, 1); //pin, time(milliseconds), initial state
} else {
digitalWrite(RELAY_DIGITAL_PIN6, LOW);
}

}

================================================================

CAYENNE_IN(V11)
{
// get value sent from dashboard
int currentValue = getValue.asInt(); // 0 to 1

// assuming you wire your relay as normally open
if (currentValue == 0) {
digitalWrite(RELAY_DIGITAL_PIN6, HIGH);
delay(10000);
digitalWrite(RELAY_DIGITAL_PIN6, LOW);
} else {
digitalWrite(RELAY_DIGITAL_PIN6, LOW);
}
}

Br,


#2

What kind of things happen? It did not even turn on / off or it just does not go off after 10 seconds?


#3

Hello Ognqn,

so basically the first time that I push the button nothing happens. Them if I hit it the second time it turns on. Them click again and it turns off. But them after some seconds it turns on again automatically. This is the behaviour looking/hearing the relay.

In the web/Android it behaves as a normal button. ON/OFF. Nothing happens after the delay/pulse.

Br,


#4

Try that code, but it’s almost the same you post. Maybe the relay needs a constant HIGH :confused:

CAYENNE_IN(V11) {
int currentValue = getValue.asInt();
if (currentValue == 1){
for (int i = 0; i < 11; i++){
digitalWrite(RELAY_DIGITAL_PIN6, HIGH);
delay(1000);
}
digitalWrite(RELAY_DIGITAL_PIN6, LOW);
}else if (currentValue == 0){
digitalWrite(RELAY_DIGITAL_PIN6, LOW);
}
}

#5

Ooh, I think what it’s happening is that you put if (currentValue == 0) then… try changing that 0 to 1, when a button is not pressed it’s 0 when it’s pressed it’s 1 .


#6

I think that this is the general problem. We want our relay to work for 10 seconds only, but it works only when there is “1”, so we can check if it is “1” and then make a delay and then turn it off. We don’t have to check if it is “0” because if it is “0” it simply does not work. We also have to update the value of the dashboard.

I think that this is the solution.


#7

Hello guys, yes, I did the proposed changes but didn’t work. Replaced the relay board for a different one and didn’t work too.

If I add the code for the normal operation on/off without delays it works ok.

  • Is there a mecanism to check the button state in the dashboard after the timer / delay expires or this would be done just when the button is clicked?

br,


#8

Yes, the mechanism is that you can take after the delay again the current value - getValue.asInt();

I think it will work…I have never try it