Hello all. I've been searching and reading everywhere for a solution to my simple issue. I just can't wrap my head around on how to use the millis function with no delay to monitor a pin that if it goes LOW then wait 2 seconds and then have it go HIGH.
I'm successfully using "delay" with the following code however it's pausing the loop for the required 2 seconds which is not desirable for my project.
Below is some code from someone else but I can't get the desired function. When I set the pin LOW it immediately goes HIGH within what looks like a fraction of a millisecond. All I want is for the code to watch for pin if LOW then go HIGH after 2 seconds using millis without delay. Thank you in advance for any help or suggestions.
I'm setting the pinmode to output and having it go HIGH on bootup. This controls a relay that is off while pin is HIGH. I control the pin though an Android app that sets the pin LOW when I press a button on my phone. Hopefully that makes sense.
start of loop()
if the pin becomes LOW (NB not is LOW)
save start time from millis()
end if
if current time - start time >= 2 seconds
set pin HIGH
end if
//other code here if required
end of loop()
I just can't wrap my head around on how to use the millis function with no delay to monitor a pin that if it goes LOW then wait 2 seconds and then have it go HIGH.
If the pin is an input pin, then it makes sense to be monitoring it, to see when it goes low. You can't make an input pin go high, so your whole sentence does not make sense.
If you explain what you are REALLY trying to accomplish, then we can help you.
Perhaps you want to do something if a pin goes low, and is still low two seconds later. That would be easy, combining the state change detection example and the blink without delay example.
I just activate a relay connected to pin D5 which should turn off automatically after 2 seconds. Again I can do this with delay however I would like to do it with millis() with no delay.
I believe UKHeliBob has exactly the right idea but I'm not sure how put in code
Please go easy on me, I'm still very new to arduino. Thank you for the help thus far.
You set it as an output and set it high in setup. You then wait for it to go low. Is there unseen code that writes a low to it? Or is there an external agent that shorts the high-side transistor pin to ground?
const int PiROV = 5; //pin 5
boolean isButtonPressed = false;
unsigned long startTime;
unsigned long interval = 2000;
void setup() {
pinMode(PiROV, OUTPUT);
digitalWrite(PiROV, HIGH);
}
void loop() {
//here put your code and change isButtonPressed to true when you receive from the Android app that the button is pressed
if (isButtonPressed == true) {
startTime = millis();
digitalWrite(PiROV, LOW);
isButtonPressed = false;
}
if (millis() - startTime >= interval && digitalRead(PiROV) == LOW) {
digitalWrite(PiROV, HIGH);
}
}
Blackfin I'm using an android app called Blynk which connects my NodeMCU and android phone together via a cloud server. In this app I can directly set any pin I want to HIGH or LOW with virtual buttons on the screen. However I'm trying to simulate a momentary 2 second button via code as I can't program this on the Blynk app. It works great with delay (first code I posted) however I want to use millis() with no delay.
I've been playing with danardos code but it's not working. I believe it's because the code is assuming it's a button that's being pressed when in fact the app it's just writing HIGH or LOW command to the NodeMCU.
First image shows the description of how the Blynk virtual button works.
Second image shows the virtual button when is pressed which sends the signal to the NodeMCU via the cloud. I know the buttons work because I can engage my relays with no issues however one of the relays I need to automatically go LOW after 2 seconds after it gets triggered to go HIGH.
I was hoping code below would work but no luck.
const int PiROV = D5; //pin 5
unsigned long startTime;
unsigned long interval = 2000;
void setup() {
pinMode(PiROV, OUTPUT);
digitalWrite(PiROV, HIGH);
void loop() {
if (digitalRead(PiROV) == LOW);
if (millis() - startTime >= interval)
{
digitalWrite(PiROV, HIGH);
}
}
malvar:
Blynk works fine. I just left it out to show the basic code for what I'm trying to accomplish.
And you don't think the fact that you're using Blynk is relevant information? You didn't even mention that fact until Reply #8. So, until then everyone was wondering how a pin you set as an OUTPUT would spontaneously change state. Good luck getting help when you don't supply crucial information.