# Need help coding a button switch relay

**URL:** <https://forum.arduino.cc/t/need-help-coding-a-button-switch-relay/1026730>\
**Category:** Programming\
**Created:** [August 29, 2022, 12:28pm UTC](https://forum.arduino.cc/t/need-help-coding-a-button-switch-relay/1026730 "2022-08-29T12:28:44Z")\
**Posts on this page:** 1\
**Showing post:** 3

<div class="post-metadata">

**Author:** ![ToddL1962](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/toddl1962/32/225060_2.png) [@ToddL1962](https://forum.arduino.cc/u/ToddL1962)\
**Post date:** [August 29, 2022, 3:17pm UTC](https://forum.arduino.cc/t/need-help-coding-a-button-switch-relay/1026730/3 "2022-08-29T15:17:49Z")

</div>

> [@codergamerhawkalphasqudron](#):
>
> ```arduino
> const int buttonPin = 8;
> const int relayPin = 7;
> int buttonState = 1;
> void setup() {
> pinMode(7, OUTPUT);// connected to S terminal of Relay
> pinMode(buttonPin, INPUT);
> 
> }
> void loop() {
> buttonState = digitalRead(buttonPin);
> {
> if (buttonState == LOW)
> digitalWrite(relayPin,LOW);
> delay(3000); 
> if (buttonState == HIGH)  
> digitalWrite(relayPin,HIGH);
> delay(25000);
> }}
> 
> ```

First, congratulations for posting your code properly!

1. The way you have it coded it implies you have the button wired to ground with an external pull up resistor. If that is not the case there is a problem with your button. I would suggest wiring the button to ground and using the internal pull up resistor.
2. You are basing your conditionals on whether the button IS pressed and not when the button BECOMES pressed. You want to know when the button becomes pressed to start a timer. See the following tutorial: [StateChangeDetection](https://forum.arduino.cc/index.php?topic=711767.msg4782013#msg4782013)
3. In the code below I assume you think both the _digitalWrite()_ and _delay()_ calls only execute if the conditional is true. That is not the case below. Only the _digitalWrite()_ executes as part of the conditional. The _delay()_ calls are executing for every _loop()_.

```arduino
if (buttonState == LOW)
  digitalWrite(relayPin,LOW);
  delay(3000); 
if (buttonState == HIGH)  
  digitalWrite(relayPin,HIGH);
 delay(25000);

```

I think what you intended was this:

```arduino
if (buttonState == LOW)
{
  digitalWrite(relayPin,LOW);
  delay(3000); 
}
if (buttonState == HIGH)  
{
  digitalWrite(relayPin,HIGH);
  delay(25000);
}

```

1. I don't recommend delay() to time the relay. It will cause the button to not be read during the relay activation because the program is stuck not doing anything during the delay. I recommend using millis() to time the relay. Look at these tutorials:  
[Example-code for timing based on millis()](https://forum.arduino.cc/t/example-code-for-timing-based-on-millis-easier-to-understand-through-the-use-of-example-numbers-avoiding-delay/974017)  
[BlinkWithoutDelay](https://www.arduino.cc/en/Tutorial/BlinkWithoutDelay)

---

_[View the full topic](https://forum.arduino.cc/t/need-help-coding-a-button-switch-relay/1026730)._
