Repeat something inside if loop

Good day. Im currently working on a personal project. The project is about controlling stuff using An app and gsm. My problem is that i want to send at least 5 times with 200 millisecond delay the command inside the if (readString == "update") loop. Its working but its sending just one data to the app.

Thanks in advance :slight_smile: :slight_smile:

//update
    if (readString == "update")     
    {

  double l1 = emon1.calcIrms(1480);  // Calculate Irms only
  double l2 = emon2.calcIrms(1480);  
  double l3 = emon3.calcIrms(1480);  
  double l4 = emon4.calcIrms(1480);  
  double v1 =l1 - 0.22;        
  double v2 =l2 - 0.23;
  double v3 =l3-0.25;
  double v4 =l4-0.25;
  double t = v1+v2+v3+v4; // total kw
  delay(10);


 if(v1 > 0.2){

    Serial1.print(v1);
    Serial1.print("|");        //1
    Serial1.print(v1*constant);       //2      hourly rate 1
    Serial1.print("|");
    delay(5);    }

  else{
  zero();}
    
  if(v2>0.2){

    Serial1.print(v2);
    Serial1.print("|");      //3
    Serial1.print(v2*constant);  //4          hourly rate 2
    Serial1.print("|");
    delay(5);}

  else{
     zero();}

  
  if(v3>0.2){
;
    Serial1.print(v3);
    Serial1.print("|");      //3
    Serial1.print(v3*constant);  //4          hourly rate 2
    Serial1.print("|");
    delay(5);}
    
  else{
     zero();}
    
  if(v4>0.2){

    Serial1.print(v4);
    Serial1.print("|");      //3
    Serial1.print(v4*constant);  //4          hourly rate 2
    Serial1.print("|");
    delay(5);
    
    }
    
 else{
     zero();}
     
  if (t>2)
    {

    Serial1.print(t);
    Serial1.print("|");      //3
    Serial1.print(t*constant);  //4          hourly rate 2
    Serial1.print("|");
    delay(5);
    
    
    
    }
    

 else{
     zero(); 
    }
 delay(10); 
 
    }
    
    readString="";

    
  }
}
void zero()
{
    Serial1.print(0);
    Serial1.print("|");        //1
    Serial1.print(0);       //2      hourly rate 1
    Serial1.print("|");
}

Always show us your ‘current’ compete sketch.
Use CTRL T to format the sketch.
Please use code tags. Use the </> icon in the posting menu.

[code] Paste sketch here. [/code]

if (readString == "update") is not a loop. It is a statement. It executes once unless you arrange for it to be executed more often - e.g. inside a 'while' or 'for' loop.

Pete

Repeat something inside if loop

An if-statement isn't a loop. :wink:

But of course, you can optionally run a loop depending on the if-condition.

And, you can have nested loops (loops inside loops) and of course if-statements inside loops. ...All very common.

There are 3 kinds of loops (in addition to the main loop). If you want to do something 3 times, you generally want a [u]for-loop[/u].

If you want to loop until some condition is reached (such as loop 'till a button is pushed) there are [u]while() loops[/u] and [u]do-while loops()[/u].