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
}