Activating a Relay/Motor for a Given Time

Hi im New on this Forum

My Problem is: i want to activate a Relay for a given Time. I have a Button and if i Press it, it should activate the Relay/Motor. I got this working but my Problem is, when i press the Button the delay start when i releas the Button. I want the button to only read a single click and not a period of time. I dont have any code because i delete it acidentelly. Now when i try to recreate to code the Relay is clicking rapidly and is only is on stable when i press the button. I hope you understand what i mean and sorry for the bad English. I hope this is in the right category.

Zerthron

So you do have some code even if it makes the relay click. Post it. We really like having something to help with.

Steve

Hi thankyou for your Reply

I got back on my Pc. This is the Code witch makes the Relay click endlessly. I change the Low and High Statment around because i think this is the Problem. I hope the attachmemt works

Zerthron

Button.ino (282 Bytes)

Here is your code posted in the preferred way

int button = LOW;
int relay = 10;

void setup()
{
  pinMode(button, INPUT_PULLUP);
  pinMode(relay, OUTPUT);
}

void loop()
{
  button = digitalRead(2);
  if (button == LOW)
  {
    digitalWrite(relay, HIGH);
  }
  else
  {
    digitalWrite(relay, LOW);
  }
}

as described in Read this before posting a programming question

Please post code like that in future

As to the code, I can see no reason why the relay should click when the switch is held closed unless the Arduino cannot supply enough current through its pin to hold the relay closed.

Details of the Arduino, relay, power supply and how things are connected please

int button = LOW;
int relay = 10;
void setup()
{
    pinMode(button, INPUT_PULLUP);

in the above code, you define "button" as LOW but use it as a pin #. later you read pin #2. should button be set to "2"?

   button = digitalRead(2);

in the above code, you overwrite the value of button with what you read, but earlier you use it to configure a pin using pinMode().

my Problem is, when i press the Button the delay start when i releas the Button.

   button = digitalRead(2);
    if (button == LOW)

in the above code, the condition "button == LOW" is only true while the button is pressed.

consider

int relayPin  = 10;
int buttonPin = A1;

void setup ()
{
    pinMode (buttonPin, INPUT_PULLUP);
    pinMode (relayPin,  OUTPUT);
}

int buttonLst = HIGH;
int button;

void loop ()
{
    button = digitalRead (buttonPin);

    if (buttonLst != button) {
        buttonLst = button;

        if (LOW == button)
            digitalWrite (relayPin, ! digitalRead (relayPin));
    }
}

Thanks for the replys

I have a Arduino nano and a 5v Relay i will post some Pictures in the Evening when im back home. The relay is connectet to ground, +5 V, and digtal pin 10. the button is connectet to digital 2 and ground. I have changed the High and low Values many times. With this config i got the Relay to turn on when the button was pressed. But if it wasnt pressed the Relay clicks fast end endlessly. I had another code, with this code everything was working. I think it is something with the code but it could also be a wiring issue. Gcjr i will try the code this evening. I understand the issue with the overwriting of the button Value. But what is wrong with the Button==Low statement?

Thanks for the Help
Zerthron

I tested the code from above. Now the relay works mostly. In 4/5 time it works but in 1/5 i dosnt work and i dosnt activate. In the attachments i have some pictures of the Test Setup. The relay isnt connectet to anything at the moment.

Here the are Pictures, it wasnt working above

zerthron:
I tested the code from above. Now the relay works mostly. In 4/5 time it works but in 1/5 i dosnt work and i dosnt activate.

what does works 4/5 times mean?

But what is wrong with the Button==Low statement?

since you use it as an argument to pinMode() it is being used as the button pin. If it's not the button pin, how is the button pin being configured.

pinMode(button, INPUT_PULLUP);

It works but when i press the button fast it somtimes doesnt work or the Led on the relay only lights up fast and the relay doesnt "fully" turn on. How can i make now, that the relay is turned on for a amount of time, for example a Second when i press the button? And how can i make it so it doesnt make a "long Press/ double click"? I hope you undestand what i mean

I hope you undestand what i mean

??

post your code