Hi everyone . i have this sketch below, where i am to HOLD(and not release) a push button(input) and i have a relay(output) that i want to act as a button (momentarily triggering) . whereas if i HOLD the input, the relay will momentarily be ON, and after 200 milliseconds, it will be off .
and if i Release the button (from holding) , it will also be momentarily ON for 200milliseconds and after that it will be OFF.
however, this is what happens with my sketch below;
whenever i HOLD my input button, the process keeps on repeating , the relay is momentarily ON, then OFF, and then it will trigger again ON and then off, but i am not yet releasing the input button .
what i want is that at the time that i hold the input button (and not yet releasing), it will be momentarily ON, after 200ms it will be OFF and that's it. it must happen for only ONE time the moment i hold it . and the same must be true at the time that i release the input button.
what is the simplest way to do it ?
/////////this is my sketch///////////////////
const int buttonPin = 2;
int relay1 = 1;
void setup() {
pinMode(relay1, OUTPUT);
pinMode(buttonPin, INPUT);
}
void loop(){
int buttonState = digitalRead(buttonPin);
if (buttonState == HIGH) {
digitalWrite(relay1, HIGH);
delay(200);
digitalWrite(relay1, LOW);
}
else {
digitalWrite(relay1, HIGH);
delay(200);
digitalWrite(relay1, LOW);
}
}