User input from Serial Monitor Window for Self Stabilized Platform

I currently have a self stabilized platform up and running. I am using the arduino uno with a 6DOF IMU and 2 servo motors. I would like the ability to prompt the user to enter the center positions/level positions of both of the servos from the Serial Monitor window before the platform begins to stabilize. I have the center/level positions in the code, but prompting the user in the serial monitor window for a center/level position would give the user the ability to keep platform parallel to the ground, no matter what the platform is mounted to, without changing the code every time. I've been looking around on the internet to find some pointers but have not had luck. The possible idea/pointers I found in the internet were not very useful and any implementation on my behalf was not good. So I currently do not have any ideas how to approach this issue. I'm not sure how to construct code like this or where to put it ( should this code be in the void setup() portion of the code ??? or void loop () portion of the code ???). I relatively new to coding and any info would be greatly appreciated.

Attached is the code for my previously posted problem.I commented the problem section of the code. For now I would like to have the user input a value for the variable “zero1” into the Serial monitor window. The variable zero1 would always have this value unless another value was input by the user. I would like the value for variable “zero1” to be entered and stored before the IMU begins. After i accomplish this I will use the variable “zero1” as my zero/center position.

#include <FreeSixIMU.h>
#include <FIMU_ADXL345.h>
#include <FIMU_ITG3200.h>
#include <Wire.h>
#include <Servo.h>


float angles[3]; // yaw pitch roll
Servo servo1;  //creates servo object to control servo 1 (pitch servo)
Servo servo2;  //creates servp object to control servo 2 (roll servo)
int ser1val = 0;  //position of servo1 (pitch servo)
int ser2val = 0;  //position of servo2 (roll servo)
int zero1 = 1;
int zero2;

// Set the FreeSixIMU object
FreeSixIMU sixDOF = FreeSixIMU();



  void setup() { 
  Serial.begin(115200);
  Wire.begin();
  servo1.attach(9); //servo 1 pin
  servo2.attach(10);  //servo 2 pin
 
 //PROBLEM SECTION OF CODE is Directly below
 //I would like to have the user input a value for the variable "zero1" into the Serial monitor window.
 //The variable zero1 would aways have this value unless another value was input by the user. 
 //I would like the value for variable "zero1" to be entered and stored before the IMU begins.
   Serial.println("\nPlease input pitch zero position: ");
   if(Serial.available()>0){
   int zero1 = Serial.read();
   }
 
 
 //Another attempt that did not work//
 
  // while (Serial.available()>0){
   //zero1= Serial.read();
  // if(Serial.read()==){

  delay(5);
  sixDOF.init(); //begin the IMU
  delay(5);
}

  

  


void loop() { 
  
  
  //sixDOF.getEuler(angles);
  sixDOF.getYawPitchRoll(angles);
  

 ser1val= (1425+angles[1]*9.55);
  servo1.writeMicroseconds(ser1val);
 
  ser2val = (1510+angles[2]*9.55);
  servo2.writeMicroseconds(ser2val);
 

   Serial.print(angles[1]);
   Serial.print(" | ");
   Serial.print(ser1val);
   Serial.print(" || ");
   Serial.print(angles[2]);
   Serial.print(" | ");
   Serial.print(ser2val);
   Serial.print(" | ");
   Serial.println(zero1); // This is here just for test purpose. I wanted to see if the value entered at the serial monitor window was accepted and stored.
   
}

It would help (you) tremendously to put each { on a new line, and use Tools + Auto Format to fix your indenting.

   Serial.println("\nPlease input pitch zero position: ");
   if(Serial.available()>0){
   int zero1 = Serial.read();
   }

Several problems here. First, Serial.read() returns a byte/character. If the user enters a value between 0 and 9, this code will read that, and store the value (actually, the ASCII code for the character) in the local variable named zero1. That local variable goes out of scope immediately, so the whole exercise was pointless.

Lose the int to reference the global variable instead.

Second, the test for serial data available will be performed before the data is even all shifted out to the serial monitor. So, no data will be available.

You need

while(Serial.available() == 0) {}

after the Serial.print() statement, to wait for there to be serial data to read.

Finally, it is unlikely that a single digit response, in ASCII, will be useful. You need to read and store, appending a NULL after each character, an array of characters, until you get the end-of-packet marker that the user types or that the Serial Monitor appends, and then convert that array to an int.

Paul

Thanks for the help and learning experience. You aren't kidding about the need to auto format. I was not aware of this function and was having trouble viewing my own sketches. I will give your suggestions a try shortly.