BLYNK
BLYNK.IO       📲 GETTING STARTED       📗 DOCS       ❓HELP CENTER       👉 SKETCH BUILDER

Loop inside if and else for line follower

hi can you help me? i’m trying to make a line follower car with ESP8266, and i’m trying to do if else statement. if the V1 button ‘on’ it will send 1, signaly as the boolean var will become true and runProgram() as linefoll code will executed. if the V1 button ‘off’ it will send 0, and stopped() will executed. you can see the code below:


BLYNK_WRITE(V1)
{
  int signaly = param.asInt();
  Serial.print(signaly);
  if (signaly==1){
    pushed = true;
    runProgram(); //linefoll active
  }
  else {
    pushed = false;
    stopped(); //all motors inactive
  }
}

here’s runProgram() and stopped() code:


void runProgram(){
  while (pushed){
  int lst = analogRead(ls); //ir sensor analog read
  Serial.println(lst);
    if(lst>800)
    {
      Serial.println("left");
      left(); //car go left
      delay(tDelay);
    }
    if(lst<800)
    {
      Serial.println("right");
      right(); //car go right
      delay(tDelay);
    }
}
}
void stopped(){
  Serial.println("Line foll inactive");
  digitalWrite(motorA2,LOW);
  digitalWrite(motorA1,LOW);                       
  digitalWrite(motorB2,LOW);
  digitalWrite(motorB1,LOW);
  delay(1000);
}

when i tried with these code, the car don’t stop line following. but if i delete while (pushed) in runProgram(), it goes as i expected but the car only take one ir sensor number when the button on. i want it loop but i want it stop when the button off too

Don’t use while loops and delays with Blynk.
Use a BlynkTimer instead.

Pete.

hi thank you for answering!
should i delete delay()?

also, i’ve do this but it still doesn’t work

if (signaly==1){timer.setInterval(1000L, runProgram);}
else{timer.setInterval(1000L, stopped);}

You should probably read this…

Pete.

it works, thank you so much!