It looks Simple but I can't get it working. Push button > off 5 sec then on Push button again do the same

Hello everybody,

Sorry for my English, from the Netherlands you know :smiley:

I think I have a simple question but I am a real arduino-noob.

I want this:

I have a Arduino Uno, a 5v relais and a button.

The Button is located at 2 (and ground).
The relais on 5v, gnd and the output to trigger the relais is located at 10.

Can someone make a code for me????? I have searched the whole internet and tried a lot of scripts. I changed in those scripts the way I thought it must be. But I am going crazy. :smiley:

It's very simple, i think.

Normally the relais must be closed, so the led is on.

When I push the button one time the led must go off foor 5 seconds and than turn on again, forever.

When I push the button again the same thing.

So...

Led on.
push button (very short)
Led off 5 seconds than on.
push button (very short)
Led off 5 seconds than on.
push button (very short)
Led off 5 seconds than on.

And so on :D.

Please can someone help me. I will be very happy!!!

Try your best and post your code. We don't just write code for you here, we encorage learning.

Post your attempt, we will help you with that.

When you post your code, please use code tags so it looks
like this

Hahahahaha
Yeah you are right.

I must learn,

I have something like this...

Changed it like the way I think it must work...

Tried a lot of other codes also...

int pbuttonPin = 2;// connect output to push button
int relayPin = 10;// Connected to relay (LED)

int val = 0; // push value from pin 2
int lightON = 1;//light status
int pushed = 0;//push status

void setup() {
// Robojax.com code and video tutorial for push button ON and OFF
Serial.begin(9600);
pinMode(pbuttonPin, INPUT_PULLUP);
pinMode(relayPin, OUTPUT);

}

void loop() {
// Robojax.com code and video tutorial for push button ON and OFF
val = digitalRead(pbuttonPin);// read the push button value

if(val == HIGH && lightON == LOW){

pushed = 1-pushed;
delay(100);

}

lightON = val;

  if(pushed == HIGH){

     Serial.println("Light OFF");
    digitalWrite(relayPin, LOW); 
     delay(5000);
    digitalWrite(relayPin, HIGH);
   delay(100000);
  }else{
    Serial.println("Light OFF");
            digitalWrite(relayPin, LOW); 
     delay(5000);
    digitalWrite(relayPin, HIGH);
   delay(100000);

}

  }

So I thought if and else, I use the same.
But it looks like de second delay don't let the button work again.

Now it goes on.... 10000 milliseconds on 5000 milliseconds off...

When I push the button nothing happens.

(I'm so ashamed, you guys/girls are laughing now hahahahaha)

This is my project.

I have made my own Terminator (life size).

I made a motor in his arm and this motor will move his middlefinger up and down.
So when he shows his middlefinger it must wait 5 seconds... When finger down also 5 seconds.

Hahahahaha I am crazy, I know. But when it works I will show a movie :smiley:

hi~
maybe using relay( DPDT) .
Normally the relais,1ch Relay (COM-NC) LED is on.
When I push the button relayPin set HIGH, Led off 5 seconds and than turn on again.

Yes, this is the classic newbie problem. When you use delay(), nothing else can happen until the delay has finished. Buttons being pressed or released will not be detected during that time. Instead, you must re-write your code to use millis() for timing, and no using delay() (expect for perhaps the occasional short delay).

There used to be some sticky posts explaining how to do this. I wonder if they are still there after the forum upgrade....

You are right :smiley: Tnx.
I will try tomorrow en let everybody know :smiley:
It is not easy to find the right words with searching.

Check out the examples in How to write Timers and Delays in Arduino

Tried so much...
It really makes me crazy...

First on
Push button turn off relais directly 5 seconds then turn on
Push button turn off relais directly 5 seconds then turn on
Push button turn off relais directly 5 seconds then turn on

And so on that's all.

Help!!! :smiley:

Really?

Hello
This is my standard recipe:
Take a bag of timers and a small finite machine and stir everything together until the desired functionality.

I hope I have it now...
Try it today:

unsigned long event = 5000;// 5 seconds unsigned long buttonPushed =0;

void setup() {

pinMode(2,INPUT_PULLUP);
int SWITCH = 10; //the pin we connect the Relais
digitalWrite(SWITCH, HIGH);
}

void loop() {
int relais = digitalRead(2);// read pin 2
if(relais ==HIGH && buttonPushed ==0)
{

digitalWrite(relais, LOW); //write 1 or LOW to relais pin
buttonPushed =millis();

}
if( relais ==HIGH && buttonPushed >0 )
{
unsigned long difference = millis()- buttonPushed;
if( difference >= event)
{
digitalWrite(relais, HIGH); //write 1 or HIGH to relais pin
}
}

}

Does it work to your satisfaction?
If not, try again :slight_smile:

NO!!!!!!!!!!!!!!!!!!!!!!!!!!
I look like my project now :smiley:

I think it works now!!

Find here a solution:

My code....

int pin = 2;
int relais = 10;
void setup()
{
pinMode(pin, INPUT_PULLUP);
pinMode(relais, OUTPUT);
attachInterrupt(0, isr, FALLING);
}

volatile long last = 0;
volatile bool turnOff = true;
volatile long offAt = 0;

void isr()
{
if( (millis() - last ) > 20 ) //if at least 20 ms has passed since last press, this is not a dup
{
last = millis(); //note the time, for ignoring duplicate presses
turnOff = true;
offAt = millis() + 5000; //save a variable of now + 5 seconds
digitalWrite(relais, LOW); //turn off
}
}

void loop()
{
if(turnOff)
{
if(millis() >= offAt)
{
digitalWrite(relais, HIGH); //turn on
}
}
}

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.