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
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.
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.
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