How to stop "if"

I trigger an "if" with a Telegram message, which makes a motor go at a certain speed.

While the first "if" plays only once and then jumps to the "else" (which I'm also not sure if I understand it correctly), the other "if" just never stops.

How do I handle this?
I'm really new to coding...

if (text == "Action One") {
      bot.sendMessage(chat_id, "Okay, Action One", "");
      set_Motor_Speed(329.628);
      delay(517);
      set_Motor_Speed(-261.626);
      delay(517);
      set_Motor_Speed(220);
      delay(259);
      set_Motor_Speed(-246.942);
      delay(1036);
      set_Motor_Speed(220);
      delay(517);
      set_Motor_Speed(-195.998);
      delay(517);
      set_Motor_Speed(220);
      delay(2331);


      
    }
    
    if (text == "Action Two") {
      bot.sendMessage(chat_id, "Okay, Action Two", "");
      set_Motor_Speed(6);
      delay(30);
      set_Motor_Speed(-6);
      delay(30);
      set_Motor_Speed(6);
      delay(30);
      set_Motor_Speed(-6);
      delay(30);
      set_Motor_Speed(0);
      delay(2331);
      
      
    }
    
    else {
      set_Motor_Speed(50);

    }
  }

Please post the full code.

But briefly, an if statement will keep executing as long as the condition remains true.
So, if you don't want an if to execute, you need to clear the condition somewhere.

1 Like

Because text is always "Action One".
Try changing it to another value..

Please post your complete sketch

The else clause code will always run if text does not equal "Action Two"

  if (text == "Action One") {
    bot.sendMessage(chat_id, "Okay, Action One", "");
    set_Motor_Speed(329.628);
    delay(517);
    set_Motor_Speed(-261.626);
    delay(517);
    set_Motor_Speed(220);
    delay(259);
    set_Motor_Speed(-246.942);
    delay(1036);
    set_Motor_Speed(220);
    delay(517);
    set_Motor_Speed(-195.998);
    delay(517);
    set_Motor_Speed(220);
    delay(2331);
  }

  if (text == "Action Two") {
    bot.sendMessage(chat_id, "Okay, Action Two", "");
    set_Motor_Speed(6);
    delay(30);
    set_Motor_Speed(-6);
    delay(30);
    set_Motor_Speed(6);
    delay(30);
    set_Motor_Speed(-6);
    delay(30);
    set_Motor_Speed(0);
    delay(2331);
  } else set_Motor_Speed(50);

this mean when message is not "Action Two" then it will always run set_Motor_Speed(50);

I'm not sure what you are asking, however sometimes it is helpful to just consider the conditions without what is actually done. Like this...

  if (text == "Action One") {
    // Action one stuff
  }

  if (text == "Action Two") {
    // Action two stuff
  }
  else {
    // else stuff
  }

With this logic if text contains the string "Action One" then obviously the first if code block will execute. The second if code block will not execute because text does not contain the string "Action Two" and therefore will execute the else code block. If you only want the else block to execute if text does not contain "Action One" or "Action Two" then you need to use an else if. Like this:

  if (text == "Action One") {
    // Action one stuff
  }
  else if (text == "Action Two") {
    // Action two stuff
  }
  else {
    // else stuff
  }

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.