Button (time-out/unresponsive period)

I need to make a push button time-out/ unresponsive after it has been pushed.

Currently using an arcade LED push button to trigger a relay. The relay triggers a 12-volt linear actuator. This is currently set up with a 3000 millisecond delay so the actuator moves just the right distance and the LED on the arcade button turns of for the the same period.

However if the button is pushed again while the actuator is moving it will add another 3000 milliseconds and continue to extend beyond the point I want it to.

Ideally i would like the button to be timed-out/unresponsive for 6000 milliseconds and the led to also be off for the same period.

Any advice or guidance is appreciated.

The code i am using is below.

** CODE **
'''
int button = 2;
int relay = 8;
int led = 7;
void setup()
{
pinMode(2, INPUT);
pinMode(8, OUTPUT);
pinMode(7, OUTPUT);
}
void loop()
{
if(digitalRead(button) == 0)
{
digitalWrite(relay, HIGH);
digitalWrite(led, HIGH);
}
else{
digitalWrite(led, LOW);
digitalWrite(relay, LOW);
delay(3000);
delay(3000);
}

}
'''

** CODE END **

Hello mattymattmatt

Interlock the input as long as the timer is running.

Have a nice day and enjoy programming in C++ and learning.
MIND THE GAP

Thanks paul

I am a novice at coding but I will look this up.

is this saying the relay should only be turned on for 3 sec and then off for it to move the correct distance?

wouldn't turning the relay off retract the actuator?

looks like the code hodls the relay HIGH for as long as the button is pressed and immediately turns the relay off when the relay is released. it then waits six seconds before checking the button again

not sure how this works

Three ticks, not single quotes or apostrophes. Nice try, though.

int button = 2;
int relay = 8;
int led = 7;

void setup()
{
pinMode(2, INPUT);
pinMode(8, OUTPUT);
pinMode(7, OUTPUT);
}

void loop()
{

if(digitalRead(button) == 0)
{
  digitalWrite(relay, HIGH);
  digitalWrite(led, HIGH);
}
else{
  digitalWrite(led, LOW);
  digitalWrite(relay, LOW);
  delay(3000);
  delay(3000);
}

}

How is your button wired? Is digitalRead(button) 0 when pressed or 1 when pressed?

The code you posted looks to give 6 seconds, and at the end of 6 seconds if the button is in that position again or still, you get another 6 seconds.

a7

Sorry a copy and paste error regarding the delay only one delay(3000);

Trying to troubleshoot this with mad flu.

The current code keeps the LED illuminated until pressed and keeps the relay inactive until the button is pressed both have a 3 second delay so the actuator moves forward for 3 seconds and the LED stays off for 3 seconds before the light comes back on and the actuator retracts.

Is it possible to make the button unresponsive for 6 seconds and the LED stay off for 6 seconds?

Basically until the actuator has returned to its retracted position.

Image one (rig on standby led on,. Actuator fully retracted)
Image two (rig activated led off, actuator moving forwards for 3 seconds before retracting)


it's not clear what button value is pressed vs unpressed. the conventional approach is to connect the button between the pin and ground, configure the pin as INPUT_PULLUP to use the internal pullup resistor which pulls the pin HIGH and for the button to pull the pin LOW.

so it's not clear what if(digitalRead(button) == 0) means, pressed or not pressed. as well as what state drives the relay: HIGH/LOW

void loop()
{
    if(digitalRead(button) == 0)
    {
        digitalWrite(relay, HIGH);
        digitalWrite(led, HIGH);
    }
    else{
        digitalWrite(led, LOW);
        digitalWrite(relay, LOW);
        delay(3000);
        delay(3000);
    }
}

but in the above code there is a 6 sec delay when the relay is set LOW. if you want it in the other case, if(digitalRead(button) == 0), then move or add the delay there.

@mattymattmatt didn't say if HIGH or LOW lights the LED.

Too lazy to read the description and read the code when it seems like if we make a plausible guess

Ideally i would like the button to be timed-out/unresponsive for 6000 milliseconds and the led to also be off for the same period.

is exactly what your code sez. 6000 being 3000 plus 3000 after all.

a7

Why guess?
People have wasted too much of my time with "professional opinions" during my career.
Why make the reader work to understand what you're trying to say?

easy boys, just wanted to see if it was possible to make a button unresponsive for period of time. I am not looking for someone else to provide the code. More that happy to learn. I got this far and it was working, just needed to make the button unresponsive fora bit.

Just sitting down to work on this now so will look into the info provided. Thanks.

The extra 3000 delay was a copy and paste error and i do not know why the LED turns off and the relay turns on on the button press. I have changed cables, buttons, code but that's the way is wants to work at the moment.

The button will be about 5m from the Arduino so i added the 1k resistor to help in case of noise on the wire. I have added in the internal pull-up on pin 2 as well thanks.

As i understand 0 = LOW the LED should be off but she's bright as can be until pressed.

I have redone the code and wiring, still the same outcome but now the button is triggering itself shortly after it is pressed.

'''
const int buttonPin = 2; // the number of the pushbutton pin
const int LEDPin = 7; // the number of the LED pin
const int relayPin = 8; // the number of the relay pin
int buttonState = LOW; // variable for reading the pushbutton status

void setup() {
// initialize the LED pin as an output:
pinMode(LEDPin, OUTPUT);
// initialize the relay pin as an output:
pinMode(relayPin, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonPin, INPUT_PULLUP);
// initialize serial communication at 9600 bits per second:
Serial.begin(9600);
}
void loop() {
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin);

// check if the pushbutton is pressed. If it is, the buttonState is HIGH:
if (buttonState == HIGH) {
// turn relay on:
digitalWrite(LEDPin, HIGH);
// turn LED on:
digitalWrite(relayPin, HIGH);
} else {
// turn relay off:
digitalWrite(LEDPin, LOW);
// turn relay off:
digitalWrite(relayPin, LOW);
delay(3000);
// print out the state of the button:
Serial.println(buttonState);
delay(1); // delay in between reads for stability
}
}
'''

The traditional way to communicate unambiguously about whether buttons are wired to be LOW when pressed or HIGH, and also whether LEDs are wired to be illuminated when the output they are attached to is at 5 volts or 0 volts is to…

draw a schematic and post it.

You can do it with a pencil and shoot a celly of it for us.

Schematic plus code.

We could, as I said, make guesses or deductions from you quite possibly tots clear prose, but saying things like

that's the way is wants to work at the moment.

reduces my motivation. Things work the way they are wired and coded, not they way they feel like working. :expressionless:

a7

a7

Thanks this is only my 2nd big project so lots of trial and error vs tradition at this point.

Tried to do a tinkercad but but most of the components are not available, just did this to help myself understand/check the wiring.

haha true unless its a printer or fax machine then they do what they want or don't want to do.

const int buttonPin = 2;     // the number of the pushbutton pin
const int LEDPin =  7;      // the number of the LED pin
const int relayPin =  8;      // the number of the relay pin
int buttonState = LOW;         // variable for reading the pushbutton status
void setup() {
  // initialize the LED pin as an output:
  pinMode(LEDPin, OUTPUT);
  // initialize the relay pin as an output:
  pinMode(relayPin, OUTPUT);
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT_PULLUP);
}
void loop() {
  // read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin);
  // check if the pushbutton is pressed. If it is, the buttonState is HIGH:
  if (buttonState == HIGH) {             
    // turn relay on:
    digitalWrite(LEDPin, HIGH);  
    // turn LED on:
    digitalWrite(relayPin, HIGH);  
  } else {
     // turn relay off:
    digitalWrite(LEDPin, LOW);
    // turn relay off:
    digitalWrite(relayPin, LOW);
    delay(3000);              
  }
}

with the pin configured with an internal pull-up, the pin is HIGH with nothing connected.

with a button connected between the pin and ground, the pin is pulled LOW when the button is pressed, otherwise it is HIGH.

is the comment correct?

use a serial print to verify your understanding of the hardware/softwaer

That's correct, ran a print tonight and the default value is 1 (high) untill the button is pressed then its 0 (low)

Think i have an issue with the button now its behaving like its being pressed multiple times when its just being pressed once :frowning: will test this tomorrow. Thanks for the help!

mechanical buttons make multiple contact, they bounce. a short (10 msec) after detecting a change is sufficient to prevent this.

in the posted code, the else condition is executed when the button is pressed and there's a 3 sec delay, more than enough to prevent a button bounce if the button is released before 3 sec

It's triggering again after 2 to 3 seconds the lights on the button flashes and the actuator carries on the extending, going to swap out the arcade button and led for a separate button/led to test if its a fault with the arcade button or wiring, also going to add a code to count the button presses.

How is the button connected to Arduino? How is LED connected to Arduino?

The block labeled "5 volt relay", what is it exactly?

You shouldn't be powering that relay from a pin on the Arduino, if we take your diagram as accurate.

Disconnect the "5 vo,t relay" entirely and see if your program runs the LED the way it looks like it should.

Also, does the LED in the switch have a build in current limiting resistor? There need to be one, as a test add a 470 ohm resistor in series with the LED in the switch, it will make it dimmer only, but maybe happier if you don't know for sure.

a7

sorry for the late reply life took over for a bit, ok so i did some troubleshooting on the hardware and code, a resistor between the two negative pins on the button has stopped the device triggering on its own thanks A7.

The relay is not powering the Arduino, pin 8 is the output pin for the relay and activates the coils the closes the circuit on the 12v side.

const int relayPin =  8;    // the number of the relay pin
const int ledPin =  7;      // the number of the LED pin
const int buttonPin = 2;    // the number of the pushbutton pin

int buttonState = HIGH;     // variable for reading the pushbutton status

void setup() {
  pinMode(relayPin, OUTPUT);
  // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT_PULLUP);

}
void loop() {
  // read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin);
  // if it is, the buttonState is HIGH:
  if (buttonState == HIGH) { ;  
    //pin 2 LED on:
    digitalWrite(ledPin, HIGH);
    // pin8 relay off:
    digitalWrite(relayPin, HIGH);  
  } else {
    // pin 2 LED off:
    digitalWrite(ledPin, LOW);
    // pin 8 relay on, activating the coil on the relay to complete the 12v circuit, this atvates the double pole double throw relay:
    digitalWrite(relayPin, LOW);
    delay(3000); 
                 
  }
}

This is working as intended i just need to the button PIN 2 to time out after it has been pushed for 6000ms???