Switching relay by releasing momentary switch

Hi,

I’ve hit an issue with my attempt at programming a relay to switch on and then delay turn off after a momentary switch is held down and then released.

I have it pieced together and working with the program below but don’t know how to reverse the switch operation so it will drive the relay after the button is held down and then released.

At the moment it will trigger the relay and turn off as i need it to when the switch it is pressed,
i need it to trigger the relay after the button has been pushed ( held down maybe for at least a minute ) then after the button is released drive the relay for the set time and turn off and not start the process till the button is pressed and held again.

Thanks for your help.

int pinButton = 8;
int Relay = 2;
int stateRelay = LOW;
int stateButton;
int previous = HIGH;
long time = 0;
long debounce = 500;
int stayON = 500; //stay on for 5000 ms

void setup() {
pinMode(pinButton, INPUT);
pinMode(Relay, OUTPUT);
}

void loop() {
stateButton = digitalRead(pinButton);
if(stateButton == LOW && previous == HIGH && millis() - time > debounce) {
if(stateRelay == HIGH){
digitalWrite(Relay, LOW);
} else {
digitalWrite(Relay, HIGH);
delay(stayON);
digitalWrite(Relay, LOW);
}
time = millis();
}
previous == stateButton;
}

Is "HIGH" an int?

Have a look at what an int is.

Sorry copied the wrong sketch it was ment to be this one.

This is my 1st go at programming and have copyed things off other sketches to try to make it work.

int pinButton = 8;
int Relay = 2;
int stateRelay = LOW;
int stateButton;
int previous = LOW;
long time = 0;
long debounce = 500;
int stayON = 500; //stay on for 5000 ms

void setup() {
pinMode(pinButton, INPUT);
pinMode(Relay, OUTPUT);
}

void loop() {
stateButton = digitalRead(pinButton);
if(stateButton == HIGH && previous == LOW && millis() - time > debounce) {
if(stateRelay == HIGH){
digitalWrite(Relay, LOW);
} else {
digitalWrite(Relay, HIGH);
delay(stayON);
digitalWrite(Relay, LOW);
}
time = millis();
}
previous == stateButton;
}

"HIGH" & "LOW" are not an int. Read the link in reply above.

DangerToMyself:
“HIGH” & “LOW” are not an int. Read the link in reply above.

OP is NOT putting HIGH and LOW in quotes. The names HIGH and LOW have values that fit in an int. The values do NOT have explicitly defined types, so the compiler assumes that they are ints, so, YES, THEY ARE INTS.

  previous == stateButton;

== is the equality operator, NOT the assignment operator.

How IS the switch wired? Using INPUT_PULLUP as the mode makes wiring much simpler.

PaulS:
OP is NOT putting HIGH and LOW in quotes. The names HIGH and LOW have values that fit in an int. The values do NOT have explicitly defined types, so the compiler assumes that they are ints, so, YES, THEY ARE INTS.

I read that and said to myself "Duh! I knew that because I've used it exactly the same way". I really need a caffeine I.V. this morning (like most mornings). My apologies jcg. Still learning too.

 previous == stateButton;

This is wrong.
== for comparison
= for assignment

int stayON = 500; //stay on for 5000 ms

500 ms or 5000 ms ?
Either way, all timing variables to be used with mills() should be unsigned long to avoid problems with rollover to zero

This is quick wiring diagram of how it is wired up, i am using it in a race car 12 volts on the momentary switch stepped down to 5 volts for the arduino, then driving 12 volts out of the 5 volt relay.

That is NOT a wiring diagram. Draw and post a picture.

i tried to upload the picture of the wiring diagram i have draw by it wont allow it as it is saying contact administrator

jcg:
i tried to upload the picture of the wiring diagram i have draw by it wont allow it as it is saying contact administrator

What format are you trying to upload?

Are you following this process?

Try again

DOC111017 (1).pdf (12.6 KB)