0

i have nodeMCU v3(esp8266) so i need to make 2 checks 1 check is the pin D1 which needs to be done continuously and another check is web link check every 10 seconds, problem is coz when i put delay then it stops the first check

void loop() {

 if (digitalRead(pinD1)==1){
       digitalWrite(pinD0, HIGH);
       delay(20000);  
 }else{
       digitalWrite(pinD0, LOW);
 }

      loop2();  

}



void loop2(){
   if (json1=="ON"){
    digitalWrite(pinD0, HIGH);
      delay(20000);  
}else{
     digitalWrite(pinD0, LOW);
               delay(10000);  

  }
}
  • loop2() is not a loop, it is a subroutine or a function .... why don't you give it a descriptive name like updateD0()? – jsotola Apr 26 '19 at 4:20
1

Rather than using delay, use millis() and a variable to check if the some time has expired

void loop()
{
    static int check = 0;
    int now = millis();
    if (now >= check) {
        if (digitalRead(pinD1)==1){
            digitalWrite(pinD0, HIGH);
            check = now + 20000;  
        } else {
            digitalWrite(pinD0, LOW);
            check = now;
        }
    }
    loop2();  
}

void loop2()
{
    static int check = 0;
    int now = millis();
    if (now >= check) {
        if (json1=="ON"){
            digitalWrite(pinD0, HIGH);
        } else {
            digitalWrite(pinD0, LOW);
        }
        check = now + 20000;
    }
}
| improve this answer | |

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.