need help in delay button

recently i face a problem on my elevator project.

when i press my button it show’s high but after i release it is low

how to delay my button as high for 5 sec ?

i did try with this program, lets said

const int button = 2; //declare button in pin 2

const int motorup = 8;

const int motordown = 9;

void setup() {

pinMode (button,INPUT);

pinMode (motorup,OUTPUT);

pinMode (motordown,OUTPUT);

}

void loop(){

buttonState = digitalread(button);

if (buttonState == HIGH){

digitalwrite(motorup,HIGH);

delay(5000);

}

but the delay of 5 sec might effect my other program below, how to make the switch on only for 5 sec without interrupt the other program below just only the switch will latch for 5 sec

Thanks in advance.

Yes. Because the delay(5000) will prevent anything else from running. You have to learn to use millis() instead. Look in your examples folder for the "Blink without Delay" example.

i confused with that, since millis() they make a statement like from previous and current. how should i latch it with my button?

should i put

unsigned long previousMillis = 0; // will store last time LED was updated

// constants won't change :
const long interval = 5000; // interval at which to blink (milliseconds)

void setup() {
// set the digital pin as output:
pinMode(button, INPUT);
}

void loop() {

unsigned long currentMillis = millis();

if (currentMillis - previousMillis >= interval) {
previousMillis = currentMillis;

if (buttonState == LOW) {
......
}

}
}

i abit confuse current and previous

can you show me the example of button latch ?

To make it easy for members to read and download your code, please edit your post and place the code inside code tags, as explained in the forum introduction posts at the top of the forum.

The demo several things at a time illustrates the use of millis() - it is an extended example of BWoD.

The longer demo may make the concept easier to grasp.

...R

You can make it easier by using a flag to indicate whether you are watching time pass for your button press:
Look at the blink without delay example in the IDE.

void loop(){
currentTime = millis();
// was button pressed, and 5 second timer not running already?
if ((digitalRead(button) == LOW) && (5secondFlag == 0)){
  startTime = millis();
  5secondFlag = 1; // show timer as running, set an output pin high
  digitalWrite (outputPin, HIGH);
  }
// see if 5 seconds has passed
if (5secondFlag == 1){
  elapsedTime = currentTime - startTime;
  if (elapsedTime >=5seconds){ // declare 5seconds = 5000UL;
  5secondFlag = 0; // show timer as stopped, clear the output pin
  digitalWrite (outputPin, LOW);
  }
// do other stuff while time passes by
} //end loop

Thanks a lot, I already solve this problem.

by crossroads