0

Hi everyone I am hoping someone here can help me. I am trying to make an arduino turn my project on and off.Using a relay. I am looking for it to act like a computer power button. A short press to turn on and them stay on and a long press (say 5 sec) to turn off and then stay off till button is pressed again. I would like it so that the project can not be accidentally turned off Can anyone please help. Thanks

  • 1
    What have you tried so far, and what part is giving you difficulty? If you need a hint to get started, you code will need to store the relay state (on/off), measure the time of each button-press, and compare the time to the length of a short-press or long-press, depending on the current state. – JRobert Dec 30 '19 at 17:43
  • Hi JRobert after spending the best part of 2 days looking around the web. Reading posts looking at you tube videos. I cant find an example to do this. The Arduino IDE as a button on, off, example in it. But I need a long press to turn off. To prevent accidental power off. Basically I want a pin to go high on a short press. and only go low if it sees a long press of say 5 secs. So if the button is pressed accidentally it will do nothing unless the press is long. – David Boswell Dec 30 '19 at 20:18
0

Here is a test sketch that I wrote for a different question. The other question's requirement was for a short press (minimum 100ms) to turn on an LED, and a long press (1 second) would turn it off. With a slight modification of this sketch, it may work for you. You will need to install the Bounce2 library.

// Press and hold button for 100 ms to turn LED ON.
// Press and hold button for 1000 ms to turn LED OFF.
#include <Bounce2.h>

byte buttonState = 0;
const byte ledPin = 3;
const byte buttonPin = 2;
const unsigned long debouncerInterval = 50;
unsigned long buttonPressedTime = 0;
unsigned long currentMillis = 0;

Bounce debouncer = Bounce();

void setup(){
  pinMode(ledPin, OUTPUT);
  digitalWrite(ledPin, LOW);
  debouncer.attach(buttonPin, INPUT_PULLUP);
  debouncer.interval(debouncerInterval);
}

void loop(){

  // Get the current time.
  currentMillis = millis();

  // Check if button was pressed or released.
  if(debouncer.update()){

    // Pressed.
    if(debouncer.read() == 0){
      if(buttonState == 0){
        buttonState = 1;
        buttonPressedTime = currentMillis;
      }
    }

    // Released.
    else{
      buttonState = 0;
    }
  }

  // Turn the LED ON or OFF.
  if(buttonState == 1){
    if(currentMillis - buttonPressedTime >= 1000){
      digitalWrite(ledPin, LOW);
      buttonState = 0;
    }
    else if(currentMillis - buttonPressedTime >= 100){
      digitalWrite(ledPin, HIGH);
    }
  }
}
|improve this answer|||||
  • Thank you so much for your help it works great. I just changed the buttonPreeedTime from 1000 to 5000 and it dose everything I wanted... Thank you so much – David Boswell Dec 30 '19 at 21:38
0

To clarify the problem, do you want to control ON OFF using Arduino? It is very simple. Please read this simple tutorial, and modify it according to your needs.

|improve this answer|||||
  • This is not what @David Boswell is trying to do. A short button press turns on the relay, and the user must press and hold the button down for 5 seconds to turn the relay off. The button in the link of the tutorial turns the relay on, then automatically turns the relay off after 5 seconds has elapsed. – VE7JRO Dec 30 '19 at 20:30
  • @VE7JRO yes of course with small modification. jees. – juliussin Dec 30 '19 at 20:41
  • You hit the nail on the head..VE7JRO. that is what i need to do. Press and hold to turn off – David Boswell Dec 30 '19 at 20:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.