Can't call on loops using serial monitor

Hello, I have been working on a snake robot for a while. I want to control the direction of the snake using the serial monitor. I have tried a lot of stuff. Nothing seems to work. I am able to cycle through the directions BUT it gets stuck. Its not constantly running the smae loop. It gets stuck after running the direction loop once.
The robot works as i have tested using buttons but it does not work using the serial monitor.
Any help is appreaciated . The code is as follows.

#include <Servo.h>  // include the Servo library

Servo s1, s2, s3, s4, s5;  // create three servo objects
float Offset_p = 0;        //offset for pitch group
float Offset_y = 0;        //offset for yaw group

float angle1, angle2, angle3, angle4, angle5;  // angles for the three motors
int sequence = 0;

void reset() {

  // reset
  angle1 = 90;
  angle2 = 90;
  angle3 = 90;
  angle4 = 90;
  angle5 = 90;
}

void turnRight() {
  // turning
  float Am_p = 70;              // amplitude for pitch group
  float Am_y = 0;               // amplitude for yaw group
  float PD_p = PI * 120 / 180;  // phase difference for pitch group
  float PD_y = 0;               // phase difference for yaw group
  float PD_py = 0;              // phase difference between pitch and yaw groups

  // rotating + side ways + forward + turning
  float Period_p = 1000;  //period for pitch group, in ms
  float Period_y = 1000;  //period for yaw group, in ms

  // turning
  angle1 = Am_p * sin(2 * PI * millis() / Period_p + 1 * PD_p) + Offset_p + 90;
  angle2 = Am_y * sin(2 * PI * millis() / Period_y + 1 * PD_py) + Offset_y + 90 - 45;
  angle3 = -Am_p * sin(2 * PI * millis() / Period_p + 0 * PD_p) - Offset_p + 90;
  angle4 = Am_y * sin(2 * PI * millis() / Period_y + 1 * PD_py) + Offset_y + 90 - 45;
  angle5 = Am_p * sin(2 * PI * millis() / Period_p + 0 * PD_p) - Offset_p + 90;
}

void turnLeft() {
  // turning
  float Am_p = 70;              // amplitude for pitch group
  float Am_y = 0;               // amplitude for yaw group
  float PD_p = PI * 120 / 180;  // phase difference for pitch group
  float PD_y = 0;               // phase difference for yaw group
  float PD_py = 0;              // phase difference between pitch and yaw groups

  // rotating + side ways + forward + turning
  float Period_p = 1000;  //period for pitch group, in ms
  float Period_y = 1000;  //period for yaw group, in ms

  // turning
  angle1 = Am_p * sin(2 * PI * millis() / Period_p + 1 * PD_p) + Offset_p + 90;
  angle2 = Am_y * sin(2 * PI * millis() / Period_y + 1 * PD_py) + Offset_y + 90 + 45;
  angle3 = -Am_p * sin(2 * PI * millis() / Period_p + 0 * PD_p) - Offset_p + 90;
  /*   angle4 = Am_y * sin(2 * PI * millis() / Period_y + 1 * PD_py) + Offset_y + 90 + 45;
  angle5 = Am_p * sin(2 * PI * millis() / Period_p + 0 * PD_p) - Offset_p + 90; */
}

void rotateLeft() {
  // rotating + side ways
  float Am_p = 30;              // amplitude for pitch group
  float Am_y = 30;              // amplitude for yaw group
  float PD_p = 0;               // phase difference for pitch group
  float PD_y = 0;               // phase difference for yaw group
  float PD_py = PI * 90 / 180;  // phase difference between pitch and yaw groups

  // rotating + side ways + forward + turning
  float Period_p = 1000;  //period for pitch group, in ms
  float Period_y = 1000;  //period for yaw group, in ms

  // rotating
  angle1 = Am_p * sin(2 * PI * millis() / Period_p + 1 * PD_p) + Offset_p + 90;
  angle2 = Am_y * sin(2 * PI * millis() / Period_y + 1 * PD_py) + Offset_y + 90;
  angle3 = -Am_p * sin(2 * PI * millis() / Period_p + 0 * PD_p) - Offset_p + 90;
  angle4 = Am_y * sin(2 * PI * millis() / Period_y + 1 * PD_py) + Offset_y + 90;
  angle5 = -Am_p * sin(2 * PI * millis() / Period_p + 0 * PD_p) - Offset_p + 90;
}

void rotateRight() {
  // rotating + side ways
  float Am_p = 30;               // amplitude for pitch group
  float Am_y = 30;               // amplitude for yaw group
  float PD_p = 0;                // phase difference for pitch group
  float PD_y = 0;                // phase difference for yaw group
  float PD_py = -PI * 90 / 180;  // phase difference between pitch and yaw groups

  // rotating + side ways + forward + turning
  float Period_p = 1000;  //period for pitch group, in ms
  float Period_y = 1000;  //period for yaw group, in ms

  // rotating
  angle1 = Am_p * sin(2 * PI * millis() / Period_p + 1 * PD_p) + Offset_p + 90;
  angle2 = Am_y * sin(2 * PI * millis() / Period_y + 1 * PD_py) + Offset_y + 90;
  angle3 = -Am_p * sin(2 * PI * millis() / Period_p + 0 * PD_p) - Offset_p + 90;
  angle4 = Am_y * sin(2 * PI * millis() / Period_y + 1 * PD_py) + Offset_y + 90;
  angle5 = -Am_p * sin(2 * PI * millis() / Period_p + 0 * PD_p) - Offset_p + 90;
}

void forward() {
  // rotating + side ways + forward + turning
  float Period_p = 1000;  //period for pitch group, in ms
  float Period_y = 1000;  //period for yaw group, in ms


  // forward + backward
  float Am_p = 70;              // amplitude for pitch group
  float Am_y = 0;               // amplitude for yaw group
  float PD_p = PI * 120 / 180;  // phase difference for pitch group
  float PD_y = 0;               // phase difference for yaw group
  float PD_py = 0;              // phase difference between pitch and yaw groups

  // forward
  angle1 = Am_p * sin(2 * PI * millis() / Period_p + 1 * PD_p) + Offset_p + 90;
  angle2 = Am_y * sin(2 * PI * millis() / Period_y + 1 * PD_py) + Offset_y + 90;
  angle3 = Am_p * sin(2 * PI * millis() / Period_p + 0 * PD_p) - Offset_p + 90;
  angle4 = Am_y * sin(2 * PI * millis() / Period_y + 1 * PD_py) + Offset_y + 90;
  angle5 = Am_p * sin(2 * PI * millis() / Period_p + 0 * PD_p) - Offset_p + 90;
}

void backward() {
  // rotating + side ways + forward + turning
  float Period_p = 1000;  //period for pitch group, in ms
  float Period_y = 1000;  //period for yaw group, in ms

  // forward + backward
  float Am_p = 70;               // amplitude for pitch group
  float Am_y = 0;                // amplitude for yaw group
  float PD_p = -PI * 120 / 180;  // phase difference for pitch group
  float PD_y = 0;                // phase difference for yaw group
  float PD_py = 0;               // phase difference between pitch and yaw groups

  // forward
  angle1 = Am_p * sin(2 * PI * millis() / Period_p + 1 * PD_p) + Offset_p + 90;
  angle2 = Am_y * sin(2 * PI * millis() / Period_y + 1 * PD_py) + Offset_y + 90;
  angle3 = Am_p * sin(2 * PI * millis() / Period_p + 0 * PD_p) - Offset_p + 90;
  angle4 = Am_y * sin(2 * PI * millis() / Period_y + 1 * PD_py) + Offset_y + 90;
  angle5 = Am_p * sin(2 * PI * millis() / Period_p + 0 * PD_p) - Offset_p + 90;
}

void rolling() {
  // rolling
  float Am_p = 70;              // amplitude for pitch group
  float Am_y = 70;              // amplitude for yaw group
  float PD_p = PI * 180 / 180;  // phase difference for pitch group
  float PD_y = 0;               // phase difference for yaw group
  float PD_py = PI * 90 / 180;  // phase difference between pitch and yaw groups

  float Period_p = 3000;  //period for pitch group, in ms
  float Period_y = 3000;  //period for yaw group, in ms

  // rolling
  angle1 = Am_p * sin(2 * PI * millis() / Period_p + 1 * PD_p) + Offset_p + 90;
  angle2 = Am_y * sin(2 * PI * millis() / Period_y + 1 * PD_py) + Offset_y + 90;
  angle3 = -Am_p * sin(2 * PI * millis() / Period_p + 0 * PD_p) - Offset_p + 90;
  angle4 = Am_y * sin(2 * PI * millis() / Period_y + 1 * PD_py) + Offset_y + 90;
  angle5 = -Am_p * sin(2 * PI * millis() / Period_p + 0 * PD_p) - Offset_p + 90;
}

void sideWays() {

  // rotating + side ways + forward + turning
  float Period_p = 1000;  //period for pitch group, in ms
  float Period_y = 1000;  //period for yaw group, in ms

  // rotating + side ways
  float Am_p = 30;              // amplitude for pitch group
  float Am_y = 30;              // amplitude for yaw group
  float PD_p = 0;               // phase difference for pitch group
  float PD_y = 0;               // phase difference for yaw group
  float PD_py = PI * 90 / 180;  // phase difference between pitch and yaw groups
    // side ways
  angle1 = Am_p * sin(2 * PI * millis() / Period_p + 1 * PD_p) + Offset_p + 90;
  angle2 = Am_y * sin(2 * PI * millis() / Period_y + 1 * PD_py) + Offset_y + 90;
  angle3 = Am_p * sin(2 * PI * millis() / Period_p + 0 * PD_p) - Offset_p + 90;
  angle4 = Am_y * sin(2 * PI * millis() / Period_y + 1 * PD_py) + Offset_y + 90;
  angle5 = Am_p * sin(2 * PI * millis() / Period_p + 0 * PD_p) - Offset_p + 90;
}

void setup() {
  Serial.begin(9600);
  s1.attach(5);   // attaches the servo on pin 5 to the servo object
  s2.attach(6);   // attaches the servo on pin 6 to the servo object
  s3.attach(9);   // attaches the servo on pin 9 to the servo object
  s4.attach(10);  // attaches the servo on pin 10 to the servo object
  s5.attach(11);  // attaches the servo on pin 11 to the servo object
}

void loop() {

  int sequence = Serial.parseInt();
  while (Serial.available() == 0) {
    Serial.println("Please enter your sequence: ");
    Serial.println(sequence);

    if (sequence == 1) {
      Serial.println("forward");
      forward();
    } else if (sequence == 2) {
      Serial.println("backward");
      backward();
    } else if (sequence == 3) {
      Serial.println("turnRight");
      turnRight();
    } else if (sequence == 4) {
      Serial.println("turnLeft");
      turnLeft();
    } else if (sequence == 5) {
      Serial.println("rotateRight");
      rotateRight();
    } else if (sequence == 6) {
      Serial.println("rotateLeft");
      rotateLeft();
    } else if (sequence == 7) {
      Serial.println("rolling");
      rolling();
    } else if (sequence == 8) {
      Serial.println("sideWays");
      sideWays();
    } else if (sequence == 0) {
      Serial.println("reset");
      reset();
    }
  }

  s1.write(angle1);
  s2.write(angle2);
  s3.write(angle3);
  s4.write(angle4);
  s5.write(angle5);
  delay(50);  //ms
}

Welcome to the forum.

Usually replacing logic informed by buttons with logic informed by serial input (or sensor input or whatever) is straightforward.

So please post your complete it compiles and works sketch that uses push buttons and we can help you convert it.

a7

your while-loop will only be entered if there are no characters in the receive-buffer.
My conclusion using the function Serial.parseInt(); does not remove the characters from the receive-buffer which means there are still characters in the receive-buffer and then your condition Serial.available() == 0 never becomes true again.

To check if this is really the case you should stop tinkering around trying this trying that but instead making visible what your code is doing and making visible the values of variables

// MACRO-START * MACRO-START * MACRO-START * MACRO-START * MACRO-START * MACRO-START *
// a detailed explanation how these macros work is given in this tutorial
// https://forum.arduino.cc/t/comfortable-serial-debug-output-short-to-write-fixed-text-name-and-content-of-any-variable-code-example/888298

#define dbg(myFixedText, variableName) \
  Serial.print( F(#myFixedText " "  #variableName"=") ); \
  Serial.println(variableName);
// usage: dbg("1:my fixed text",myVariable);
// myVariable can be any variable or expression that is defined in scope

#define dbgi(myFixedText, variableName,timeInterval) \
  do { \
    static unsigned long intervalStartTime; \
    if ( millis() - intervalStartTime >= timeInterval ){ \
      intervalStartTime = millis(); \
      Serial.print( F(#myFixedText " "  #variableName"=") ); \
      Serial.println(variableName); \
    } \
  } while (false);
// usage: dbgi("2:my fixed text",myVariable,1000);
// myVariable can be any variable or expression that is defined in scope
// third parameter is the time in milliseconds that must pass by until the next time a
// Serial.print is executed
// end of macros dbg and dbgi
// print only once when value has changed
#define dbgc(myFixedText, variableName) \
  do { \
    static long lastState; \
    if ( lastState != variableName ){ \
      Serial.print( F(#myFixedText " "  #variableName" changed from ") ); \
      Serial.print(lastState); \
      Serial.print( F(" to ") ); \
      Serial.println(variableName); \
      lastState = variableName; \
    } \
  } while (false);
// MACRO-END * MACRO-END * MACRO-END * MACRO-END * MACRO-END * MACRO-END * MACRO-END *


#include <Servo.h>  // include the Servo library

Servo s1, s2, s3, s4, s5;  // create three servo objects
float Offset_p = 0;        //offset for pitch group
float Offset_y = 0;        //offset for yaw group

float angle1, angle2, angle3, angle4, angle5;  // angles for the three motors
int sequence = 0;

void reset() {

  // reset
  angle1 = 90;
  angle2 = 90;
  angle3 = 90;
  angle4 = 90;
  angle5 = 90;
}

void turnRight() {
  // turning
  float Am_p = 70;              // amplitude for pitch group
  float Am_y = 0;               // amplitude for yaw group
  float PD_p = PI * 120 / 180;  // phase difference for pitch group
  float PD_y = 0;               // phase difference for yaw group
  float PD_py = 0;              // phase difference between pitch and yaw groups

  // rotating + side ways + forward + turning
  float Period_p = 1000;  //period for pitch group, in ms
  float Period_y = 1000;  //period for yaw group, in ms

  // turning
  angle1 = Am_p * sin(2 * PI * millis() / Period_p + 1 * PD_p) + Offset_p + 90;
  angle2 = Am_y * sin(2 * PI * millis() / Period_y + 1 * PD_py) + Offset_y + 90 - 45;
  angle3 = -Am_p * sin(2 * PI * millis() / Period_p + 0 * PD_p) - Offset_p + 90;
  angle4 = Am_y * sin(2 * PI * millis() / Period_y + 1 * PD_py) + Offset_y + 90 - 45;
  angle5 = Am_p * sin(2 * PI * millis() / Period_p + 0 * PD_p) - Offset_p + 90;
}

void turnLeft() {
  // turning
  float Am_p = 70;              // amplitude for pitch group
  float Am_y = 0;               // amplitude for yaw group
  float PD_p = PI * 120 / 180;  // phase difference for pitch group
  float PD_y = 0;               // phase difference for yaw group
  float PD_py = 0;              // phase difference between pitch and yaw groups

  // rotating + side ways + forward + turning
  float Period_p = 1000;  //period for pitch group, in ms
  float Period_y = 1000;  //period for yaw group, in ms

  // turning
  angle1 = Am_p * sin(2 * PI * millis() / Period_p + 1 * PD_p) + Offset_p + 90;
  angle2 = Am_y * sin(2 * PI * millis() / Period_y + 1 * PD_py) + Offset_y + 90 + 45;
  angle3 = -Am_p * sin(2 * PI * millis() / Period_p + 0 * PD_p) - Offset_p + 90;
  /*   angle4 = Am_y * sin(2 * PI * millis() / Period_y + 1 * PD_py) + Offset_y + 90 + 45;
  angle5 = Am_p * sin(2 * PI * millis() / Period_p + 0 * PD_p) - Offset_p + 90; */
}

void rotateLeft() {
  // rotating + side ways
  float Am_p = 30;              // amplitude for pitch group
  float Am_y = 30;              // amplitude for yaw group
  float PD_p = 0;               // phase difference for pitch group
  float PD_y = 0;               // phase difference for yaw group
  float PD_py = PI * 90 / 180;  // phase difference between pitch and yaw groups

  // rotating + side ways + forward + turning
  float Period_p = 1000;  //period for pitch group, in ms
  float Period_y = 1000;  //period for yaw group, in ms

  // rotating
  angle1 = Am_p * sin(2 * PI * millis() / Period_p + 1 * PD_p) + Offset_p + 90;
  angle2 = Am_y * sin(2 * PI * millis() / Period_y + 1 * PD_py) + Offset_y + 90;
  angle3 = -Am_p * sin(2 * PI * millis() / Period_p + 0 * PD_p) - Offset_p + 90;
  angle4 = Am_y * sin(2 * PI * millis() / Period_y + 1 * PD_py) + Offset_y + 90;
  angle5 = -Am_p * sin(2 * PI * millis() / Period_p + 0 * PD_p) - Offset_p + 90;
}

void rotateRight() {
  // rotating + side ways
  float Am_p = 30;               // amplitude for pitch group
  float Am_y = 30;               // amplitude for yaw group
  float PD_p = 0;                // phase difference for pitch group
  float PD_y = 0;                // phase difference for yaw group
  float PD_py = -PI * 90 / 180;  // phase difference between pitch and yaw groups

  // rotating + side ways + forward + turning
  float Period_p = 1000;  //period for pitch group, in ms
  float Period_y = 1000;  //period for yaw group, in ms

  // rotating
  angle1 = Am_p * sin(2 * PI * millis() / Period_p + 1 * PD_p) + Offset_p + 90;
  angle2 = Am_y * sin(2 * PI * millis() / Period_y + 1 * PD_py) + Offset_y + 90;
  angle3 = -Am_p * sin(2 * PI * millis() / Period_p + 0 * PD_p) - Offset_p + 90;
  angle4 = Am_y * sin(2 * PI * millis() / Period_y + 1 * PD_py) + Offset_y + 90;
  angle5 = -Am_p * sin(2 * PI * millis() / Period_p + 0 * PD_p) - Offset_p + 90;
}

void forward() {
  // rotating + side ways + forward + turning
  float Period_p = 1000;  //period for pitch group, in ms
  float Period_y = 1000;  //period for yaw group, in ms


  // forward + backward
  float Am_p = 70;              // amplitude for pitch group
  float Am_y = 0;               // amplitude for yaw group
  float PD_p = PI * 120 / 180;  // phase difference for pitch group
  float PD_y = 0;               // phase difference for yaw group
  float PD_py = 0;              // phase difference between pitch and yaw groups

  // forward
  angle1 = Am_p * sin(2 * PI * millis() / Period_p + 1 * PD_p) + Offset_p + 90;
  angle2 = Am_y * sin(2 * PI * millis() / Period_y + 1 * PD_py) + Offset_y + 90;
  angle3 = Am_p * sin(2 * PI * millis() / Period_p + 0 * PD_p) - Offset_p + 90;
  angle4 = Am_y * sin(2 * PI * millis() / Period_y + 1 * PD_py) + Offset_y + 90;
  angle5 = Am_p * sin(2 * PI * millis() / Period_p + 0 * PD_p) - Offset_p + 90;
}

void backward() {
  // rotating + side ways + forward + turning
  float Period_p = 1000;  //period for pitch group, in ms
  float Period_y = 1000;  //period for yaw group, in ms

  // forward + backward
  float Am_p = 70;               // amplitude for pitch group
  float Am_y = 0;                // amplitude for yaw group
  float PD_p = -PI * 120 / 180;  // phase difference for pitch group
  float PD_y = 0;                // phase difference for yaw group
  float PD_py = 0;               // phase difference between pitch and yaw groups

  // forward
  angle1 = Am_p * sin(2 * PI * millis() / Period_p + 1 * PD_p) + Offset_p + 90;
  angle2 = Am_y * sin(2 * PI * millis() / Period_y + 1 * PD_py) + Offset_y + 90;
  angle3 = Am_p * sin(2 * PI * millis() / Period_p + 0 * PD_p) - Offset_p + 90;
  angle4 = Am_y * sin(2 * PI * millis() / Period_y + 1 * PD_py) + Offset_y + 90;
  angle5 = Am_p * sin(2 * PI * millis() / Period_p + 0 * PD_p) - Offset_p + 90;
}

void rolling() {
  // rolling
  float Am_p = 70;              // amplitude for pitch group
  float Am_y = 70;              // amplitude for yaw group
  float PD_p = PI * 180 / 180;  // phase difference for pitch group
  float PD_y = 0;               // phase difference for yaw group
  float PD_py = PI * 90 / 180;  // phase difference between pitch and yaw groups

  float Period_p = 3000;  //period for pitch group, in ms
  float Period_y = 3000;  //period for yaw group, in ms

  // rolling
  angle1 = Am_p * sin(2 * PI * millis() / Period_p + 1 * PD_p) + Offset_p + 90;
  angle2 = Am_y * sin(2 * PI * millis() / Period_y + 1 * PD_py) + Offset_y + 90;
  angle3 = -Am_p * sin(2 * PI * millis() / Period_p + 0 * PD_p) - Offset_p + 90;
  angle4 = Am_y * sin(2 * PI * millis() / Period_y + 1 * PD_py) + Offset_y + 90;
  angle5 = -Am_p * sin(2 * PI * millis() / Period_p + 0 * PD_p) - Offset_p + 90;
}

void sideWays() {

  // rotating + side ways + forward + turning
  float Period_p = 1000;  //period for pitch group, in ms
  float Period_y = 1000;  //period for yaw group, in ms

  // rotating + side ways
  float Am_p = 30;              // amplitude for pitch group
  float Am_y = 30;              // amplitude for yaw group
  float PD_p = 0;               // phase difference for pitch group
  float PD_y = 0;               // phase difference for yaw group
  float PD_py = PI * 90 / 180;  // phase difference between pitch and yaw groups
    // side ways
  angle1 = Am_p * sin(2 * PI * millis() / Period_p + 1 * PD_p) + Offset_p + 90;
  angle2 = Am_y * sin(2 * PI * millis() / Period_y + 1 * PD_py) + Offset_y + 90;
  angle3 = Am_p * sin(2 * PI * millis() / Period_p + 0 * PD_p) - Offset_p + 90;
  angle4 = Am_y * sin(2 * PI * millis() / Period_y + 1 * PD_py) + Offset_y + 90;
  angle5 = Am_p * sin(2 * PI * millis() / Period_p + 0 * PD_p) - Offset_p + 90;
}

void setup() {
  Serial.begin(9600);
  s1.attach(5);   // attaches the servo on pin 5 to the servo object
  s2.attach(6);   // attaches the servo on pin 6 to the servo object
  s3.attach(9);   // attaches the servo on pin 9 to the servo object
  s4.attach(10);  // attaches the servo on pin 10 to the servo object
  s5.attach(11);  // attaches the servo on pin 11 to the servo object
}

void loop() {
  
  dbgi("top of loop:",Serial.available(),500); // print once every 500 milliseconds
  int sequence = Serial.parseInt();
  dbgi("2:",sequence,500); // print once every 500 milliseconds
  while (Serial.available() == 0) {
    dbgi("inside while:",Serial.available(),500); // print once every 500 milliseconds
    //delay(500);
    //Serial.println("Please enter your sequence: ");
    dbgi("2:",sequence,500);
    //Serial.println(sequence);

    if (sequence == 1) {
      Serial.println("forward");
      forward();
    } else if (sequence == 2) {
      Serial.println("backward");
      backward();
    } else if (sequence == 3) {
      Serial.println("turnRight");
      turnRight();
    } else if (sequence == 4) {
      Serial.println("turnLeft");
      turnLeft();
    } else if (sequence == 5) {
      Serial.println("rotateRight");
      rotateRight();
    } else if (sequence == 6) {
      Serial.println("rotateLeft");
      rotateLeft();
    } else if (sequence == 7) {
      Serial.println("rolling");
      rolling();
    } else if (sequence == 8) {
      Serial.println("sideWays");
      sideWays();
    } else if (sequence == 0) {
      //Serial.println("reset");
      //reset();
    }
    delay(2000);
  }

  s1.write(angle1);
  s2.write(angle2);
  s3.write(angle3);
  s4.write(angle4);
  s5.write(angle5);
  delay(50);  //ms
}

as a general advice:

you should test things like serial receiving with one or two commands if it is working as expected and after that expanding on more commands

It will remove the characters that constitute the int value but will leave behind any Line Ending characters such as Carriage Return of Linefeed

@Asif_zh what have you got the line ending set to in the Serial monitor ? Anything except No line ending will cause problems because Serial.available() will not return zero th second time that it is used and, sequence will not equal any of your expected values

In addition to that, spamming the Serial monitor is a clumsy way to prompt the user to ender a command. To add to that problem, if the user does not enter a command then the value of sequence is not changed so the same command is executed again and again


#include <Servo.h>  // include the Servo library

Servo s1, s2, s3,s4, s5;    // create three servo objects
float Offset_p = 0;  //offset for pitch group
float Offset_y = 0;  //offset for yaw group

float angle1, angle2, angle3, angle4, angle5;  // angles for the three motors
int sequence;

void turnRight() {
  // turning
  float Am_p = 70;              // amplitude for pitch group
  float Am_y = 0;               // amplitude for yaw group
  float PD_p = PI * 120 / 180;  // phase difference for pitch group
  float PD_y = 0;               // phase difference for yaw group
  float PD_py = 0;              // phase difference between pitch and yaw groups

  // rotating + side ways + forward + turning
  float Period_p = 1000;  //period for pitch group, in ms
  float Period_y = 1000;  //period for yaw group, in ms

  // turning
  angle1 = Am_p * sin(2 * PI * millis() / Period_p + 1 * PD_p) + Offset_p + 90;
  angle2 = Am_y * sin(2 * PI * millis() / Period_y + 1 * PD_py) + Offset_y + 90 - 45;
  angle3 = -Am_p * sin(2 * PI * millis() / Period_p + 0 * PD_p) - Offset_p + 90;
  angle4 = Am_y * sin(2 * PI * millis() / Period_y + 1 * PD_py) + Offset_y + 90 - 45;
  angle5 = Am_p * sin(2 * PI * millis() / Period_p + 0 * PD_p) - Offset_p + 90;
}

void turnLeft() {
  // turning
  float Am_p = 70;              // amplitude for pitch group
  float Am_y = 0;               // amplitude for yaw group
  float PD_p = PI * 120 / 180;  // phase difference for pitch group
  float PD_y = 0;               // phase difference for yaw group
  float PD_py = 0;              // phase difference between pitch and yaw groups

  // rotating + side ways + forward + turning
  float Period_p = 1000;  //period for pitch group, in ms
  float Period_y = 1000;  //period for yaw group, in ms

  // turning
  angle1 = Am_p * sin(2 * PI * millis() / Period_p + 1 * PD_p) + Offset_p + 90;
  angle2 = Am_y * sin(2 * PI * millis() / Period_y + 1 * PD_py) + Offset_y + 90 + 45;
  angle3 = -Am_p * sin(2 * PI * millis() / Period_p + 0 * PD_p) - Offset_p + 90;
/*   angle4 = Am_y * sin(2 * PI * millis() / Period_y + 1 * PD_py) + Offset_y + 90 + 45;
  angle5 = Am_p * sin(2 * PI * millis() / Period_p + 0 * PD_p) - Offset_p + 90; */
}

void rotateLeft() {
  // rotating + side ways
  float Am_p = 30;              // amplitude for pitch group
  float Am_y = 30;              // amplitude for yaw group
  float PD_p = 0;               // phase difference for pitch group
  float PD_y = 0;               // phase difference for yaw group
  float PD_py = PI * 90 / 180;  // phase difference between pitch and yaw groups

  // rotating + side ways + forward + turning
  float Period_p = 1000;  //period for pitch group, in ms
  float Period_y = 1000;  //period for yaw group, in ms

  // rotating
  angle1 = Am_p * sin(2 * PI * millis() / Period_p + 1 * PD_p) + Offset_p + 90;
  angle2 = Am_y * sin(2 * PI * millis() / Period_y + 1 * PD_py) + Offset_y + 90;
  angle3 = -Am_p * sin(2 * PI * millis() / Period_p + 0 * PD_p) - Offset_p + 90;
  angle4 = Am_y * sin(2 * PI * millis() / Period_y + 1 * PD_py) + Offset_y + 90;
  angle5 = -Am_p * sin(2 * PI * millis() / Period_p + 0 * PD_p) - Offset_p + 90;
}

void rotateRight() {
  // rotating + side ways
  float Am_p = 30;               // amplitude for pitch group
  float Am_y = 30;               // amplitude for yaw group
  float PD_p = 0;                // phase difference for pitch group
  float PD_y = 0;                // phase difference for yaw group
  float PD_py = -PI * 90 / 180;  // phase difference between pitch and yaw groups

  // rotating + side ways + forward + turning
  float Period_p = 1000;  //period for pitch group, in ms
  float Period_y = 1000;  //period for yaw group, in ms

  // rotating
  angle1 = Am_p * sin(2 * PI * millis() / Period_p + 1 * PD_p) + Offset_p + 90;
  angle2 = Am_y * sin(2 * PI * millis() / Period_y + 1 * PD_py) + Offset_y + 90;
  angle3 = -Am_p * sin(2 * PI * millis() / Period_p + 0 * PD_p) - Offset_p + 90;
  angle4 = Am_y * sin(2 * PI * millis() / Period_y + 1 * PD_py) + Offset_y + 90;
  angle5 = -Am_p * sin(2 * PI * millis() / Period_p + 0 * PD_p) - Offset_p + 90;
}

void forward() {
  // rotating + side ways + forward + turning
  float Period_p = 1000;  //period for pitch group, in ms
  float Period_y = 1000;  //period for yaw group, in ms


  // forward + backward
  float Am_p = 70;              // amplitude for pitch group
  float Am_y = 0;               // amplitude for yaw group
  float PD_p = PI * 120 / 180;  // phase difference for pitch group
  float PD_y = 0;               // phase difference for yaw group
  float PD_py = 0;              // phase difference between pitch and yaw groups

  // forward
  angle1 = Am_p * sin(2 * PI * millis() / Period_p + 1 * PD_p) + Offset_p + 90;
  angle2 = Am_y * sin(2 * PI * millis() / Period_y + 1 * PD_py) + Offset_y + 90;
  angle3 = Am_p * sin(2 * PI * millis() / Period_p + 0 * PD_p) - Offset_p + 90;
  angle4 = Am_y * sin(2 * PI * millis() / Period_y + 1 * PD_py) + Offset_y + 90;
  angle5 = Am_p * sin(2 * PI * millis() / Period_p + 0 * PD_p) - Offset_p + 90;
}

void bakward() {
  // rotating + side ways + forward + turning
  float Period_p = 1000;  //period for pitch group, in ms
  float Period_y = 1000;  //period for yaw group, in ms

  // forward + backward
  float Am_p = 70;               // amplitude for pitch group
  float Am_y = 0;                // amplitude for yaw group
  float PD_p = -PI * 120 / 180;  // phase difference for pitch group
  float PD_y = 0;                // phase difference for yaw group
  float PD_py = 0;               // phase difference between pitch and yaw groups

  // forward
  angle1 = Am_p * sin(2 * PI * millis() / Period_p + 1 * PD_p) + Offset_p + 90;
  angle2 = Am_y * sin(2 * PI * millis() / Period_y + 1 * PD_py) + Offset_y + 90;
  angle3 = Am_p * sin(2 * PI * millis() / Period_p + 0 * PD_p) - Offset_p + 90;
  angle4 = Am_y * sin(2 * PI * millis() / Period_y + 1 * PD_py) + Offset_y + 90;
  angle5 = Am_p * sin(2 * PI * millis() / Period_p + 0 * PD_p) - Offset_p + 90;
}

void rolling() {
  // rolling
  float Am_p = 70;              // amplitude for pitch group
  float Am_y = 70;              // amplitude for yaw group
  float PD_p = PI * 180 / 180;  // phase difference for pitch group
  float PD_y = 0;               // phase difference for yaw group
  float PD_py = PI * 90 / 180;  // phase difference between pitch and yaw groups

  float Period_p = 3000;  //period for pitch group, in ms
  float Period_y = 3000;  //period for yaw group, in ms

  // rolling
  angle1 = Am_p * sin(2 * PI * millis() / Period_p + 1 * PD_p) + Offset_p + 90;
  angle2 = Am_y * sin(2 * PI * millis() / Period_y + 1 * PD_py) + Offset_y + 90;
  angle3 = -Am_p * sin(2 * PI * millis() / Period_p + 0 * PD_p) - Offset_p + 90;
  angle4 = Am_y * sin(2 * PI * millis() / Period_y + 1 * PD_py) + Offset_y + 90;
  angle5 = -Am_p * sin(2 * PI * millis() / Period_p + 0 * PD_p) - Offset_p + 90;
}

void sideWays() {

  // rotating + side ways + forward + turning
  float Period_p = 1000;  //period for pitch group, in ms
  float Period_y = 1000;  //period for yaw group, in ms

  // rotating + side ways
  float Am_p = 30;              // amplitude for pitch group
  float Am_y = 30;              // amplitude for yaw group
  float PD_p = 0;               // phase difference for pitch group
  float PD_y = 0;               // phase difference for yaw group
  float PD_py = PI * 90 / 180;  // phase difference between pitch and yaw groups
    // side ways
  angle1 = Am_p * sin(2 * PI * millis() / Period_p + 1 * PD_p) + Offset_p + 90;
  angle2 = Am_y * sin(2 * PI * millis() / Period_y + 1 * PD_py) + Offset_y + 90;
  angle3 = Am_p * sin(2 * PI * millis() / Period_p + 0 * PD_p) - Offset_p + 90;
  angle4 = Am_y * sin(2 * PI * millis() / Period_y + 1 * PD_py) + Offset_y + 90;
  angle5 = Am_p * sin(2 * PI * millis() / Period_p + 0 * PD_p) - Offset_p + 90;
}

void setup() {
  s1.attach(5);   // attaches the servo on pin 9 to the servo object
  s2.attach(6);  // attaches the servo on pin 10 to the servo object
  s3.attach(9);  // attaches the servo on pin 11 to the servo object
  s4.attach(10);  // attaches the servo on pin 10 to the servo object
  s5.attach(11);  // attaches the servo on pin 11 to the servo object

  pinMode(0, INPUT);
  pinMode(1, INPUT);

  Serial.begin(9600);
}

void loop() {
  if (digitalRead(0) == LOW) {
    delay(100);
    if (digitalRead(0) == LOW) {
      if (sequence == 8) {
        sequence = 0;
      }
      sequence++;
      Serial.println(sequence);
    }
  }

  if (sequence == 1) {
    Serial.println("turnRight");
    turnRight();
  } else if (sequence == 2) {
    Serial.println("turnLeft");
    turnLeft();
  } else if (sequence == 3) {
    Serial.println("rotateLeft");
    rotateLeft();
  } else if (sequence == 4) {
    Serial.println("rotateRight");
    rotateRight();
  } else if (sequence == 5) {
    Serial.println("forward");
    forward();
  } else if (sequence == 6) {
    Serial.println("bakward");
    bakward();
  } else if (sequence == 7) {
    Serial.println("rolling");
    rolling();
  } else {
    Serial.println("sideWays");
    sideWays();
  }

/*   if(digitalRead(1) == LOW){
    delay(100);
    if (digitalRead(0) == LOW) {
      Serial.println("stop");
    }
    delay(1000); //wait 1 second
  } */

  s1.write(angle1);
  s2.write(angle2);
  s3.write(angle3);
  s4.write(angle4);
  s5.write(angle5);
  delay(50);  //ms
}

Here is the button implimentation

this can functional be reduced to

void loop() {
  if (a button is pressed) {
  set_a_sequence_number;
}

move_robot_according_to_sequence_number

type or paste code here

Your Serial-monitor-version is doing
void loop() {
 read_In_Serial_Input;

as long as there is no serial input loop inside this loop

  while (sequence == 0) {
    keep_on_looping_inside_here;
    never_leave_this_loop_again_because 
    the  variable sequence  is never changed again
  }

Divide and conquer. This is untested, but I hope correct and anyway that you can see my plan for you:

void loop() {
  if (digitalRead(0) == LOW) {
    delay(100);
    if (digitalRead(0) == LOW) {
      if (sequence == 8) {
        sequence = 0;
      }
      sequence++;
      Serial.println(sequence);
    }
  }
}

OK, I just truncated the part that uses sequence and turned your loop into a simple printing of the value.

Without adding the stuff I removed, get a tiny program to work what just spits back the number you collect from the serial input.

Then put the stuff I took out… back in. No need for any new control structure like while loops. As long as the serial number can be printed, it is now identical to the number developed by essentially counting button presses.

a7

My line ending is set to No Line Ending

@Asif_zh I know we are working on serial input control of your process, but your pushbutton code is less than satisfactory.

I suggest when you can that you look into better methods for counting pushbutton presses.

To make sure I wasn't misleading you I placed the code I posted into the wokwi simulator:


Take a the link and play with it here, the wokwi makes experimenting a bit easier.


// https://wokwi.com/projects/348775516561474130

# define buttonPin  7

void setup() {
  Serial.begin(115200);
  Serial.println("Hello W0rld!");

  pinMode(buttonPin, INPUT_PULLUP);
}

int sequence;

void loop() {
  if (digitalRead(buttonPin) == LOW) {
    delay(100);
    if (digitalRead(buttonPin) == LOW) {
      if (sequence == 8) {
        sequence = 0;
      }
      sequence++;
      Serial.println(sequence);
    }
  }
}

You will see that it is a bit tough to get the number to go up by just one with one button press. A press that is too short doesn't register, a press that is too long starts racking up multiple counts.

There are endless tutorials on handling pushbuttons. Hook yourself up with one and make the code I posted here work much better!

a7