Triggering different loops

I have a step motor which is connected to a telegram bot through the arduino board.

When I start the whole thing the motor should LOOP a specific action until I write a message to the telegram bot. I have like 5 keywords that each should trigger a different loop which gets done once and then it should go back to the original LOOP.

I‘m really new to arduino and coding in general. But I need to get this done. Please help.

HERE IS PART OF THE CODE:


void setup()
...

  pinMode(Enable_Pin, OUTPUT);
  digitalWrite(Enable_Pin, LOW);

  pinMode(MS1_Pin, OUTPUT);
  digitalWrite(MS1_Pin, LOW);
  pinMode(MS2_Pin, OUTPUT);
  digitalWrite(MS2_Pin, LOW);
  pinMode(MS3_Pin, OUTPUT);
  digitalWrite(MS3_Pin, LOW);

  pinMode(Reset_Pin, OUTPUT);
  digitalWrite(Reset_Pin, HIGH);
  pinMode(Sleep_Pin, OUTPUT);
  digitalWrite(Sleep_Pin, HIGH);
  pinMode(Step_Pin, OUTPUT);
  digitalWrite(Step_Pin, HIGH);
  pinMode(Dir_Pin, OUTPUT);
  digitalWrite(Dir_Pin, LOW);

  ledcAttachPin(Step_Pin, 0);
  ledcSetup(0,1000,8);
  ledcWrite(0,128);
}


void set_Motor_Speed(double speed)
{
  if (speed < 0)
  {
    digitalWrite(Dir_Pin, LOW);
    speed = -speed;
  } else
  {
    digitalWrite(Dir_Pin, HIGH);
  }
  ledcSetup(0,speed,8);
  ledcWrite(0,127);
}



void handleNewMessages(int numNewMessages) {

  Serial.println("handleNewMessages");

  Serial.println(String(numNewMessages));

  for (int i=0; i<numNewMessages; i++) {

    // Chat id of the requester

    String chat_id = String(bot.messages[i].chat_id);

    if (chat_id != CHAT_ID){

      bot.sendMessage(chat_id, "Unauthorized user", "");

      continue;

    }

   


    String text = bot.messages[i].text;

    Serial.println(text);

    String from_name = bot.messages[i].from_name;


    if (text == "One") {

      bot.sendMessage(chat_id, "Playing Loop One", "");

      //So this is the message for loop one. Here it should probably trigger a 'void loop()' or something which gets played once.

    }

   

    if (text == "Two") {

      bot.sendMessage(chat_id, "Playing Loop Two", "");

      //This one for loop two and so on

    }

   

  }

...
}

So my main questions are:

  1. How to write the different loops and make them play only once when triggered?
  2. How to go back to a main loop?
  3. How to trigger each once-played loop through a different keyword?

Thank you in advance!

you are testing so

if(text == "One")

could the string contain other characters such a none printable end of line '\n' '\r' etc
try testing if the strings startwith the required string

if(text.startsWith( "One")) {

read reference about switch case and while().

1 Like

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