user78027: I am trying to get the serial monitor to prompt me to input two settings - the PWM level and the Resonance level. I am being prompted to input the PWM level, but before I can input the Resonance level it is set to zero and then I am asked again for the PWM level.
I am using Serial.flush(); inbetween the commands to clear the serial input, but I still cannot input the Resonance level before it is set to zero.
Here is my code:
int PWMPin = 10;       // Pin 10 connects to MOSFET gate on breadboard 
int ResPin = 4;        // Pin for the resonator stage 


void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);   // Define baud rate for console to be 9600
  pinMode(PWMPin, OUTPUT);  
  pinMode(ResPin, OUTPUT);   

}

void loop() {
  // put your main code here, to run repeatedly:

Serial.println("What PWM level do you want to test?");   // Testing only  
 while (Serial.available() == 0) {
     // Wait for user input
  }
  int test_level_PWM = Serial.parseInt();
    analogWrite(PWMPin, test_level_PWM);
    Serial.print("PWM Level: ");
    Serial.println(test_level_PWM);
    
Serial.flush();

Serial.println("What Resonance level do you want to test?");    
 while (Serial.available() == 0) {
     // Wait for user input
  }
  int test_level = Serial.parseInt();
    analogWrite(ResPin, test_level);
    Serial.print("Resonance Level: ");
    Serial.println(test_level);

   delay(1000); 


Here is the serial monitor output. I was able to input the PWM level but the Resonance level was set to zero immediately after and then I was asked to input the PWM level again.


user16780: Your line
   Serial.flush();
is probably intended to remove additional input after the expected numerical input. But that's not what it's doing. (See @jurai's comment)
Either avoid additional input like NewLine characters, or remove them using something like
while (Serial.available()) {delay(1); Serial.read();}
( The delay(1); takes care of slowly coming in serial data at 9600 Baud )

78027: <p>I am trying to get the serial monitor to prompt me to input two settings - the PWM level and the Resonance level. I am being prompted to input the PWM level, but before I can input the Resonance level it is set to zero and then I am asked again for the PWM level.</p>
<p>I am using Serial.flush(); inbetween the commands to clear the serial input, but I still cannot input the Resonance level before it is set to zero.</p>
<p>Here is my code:</p>
<pre><code>int PWMPin = 10;       // Pin 10 connects to MOSFET gate on breadboard 
int ResPin = 4;        // Pin for the resonator stage 


void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);   // Define baud rate for console to be 9600
  pinMode(PWMPin, OUTPUT);  
  pinMode(ResPin, OUTPUT);   

}

void loop() {
  // put your main code here, to run repeatedly:

Serial.println(&quot;What PWM level do you want to test?&quot;);   // Testing only  
 while (Serial.available() == 0) {
     // Wait for user input
  }
  int test_level_PWM = Serial.parseInt();
    analogWrite(PWMPin, test_level_PWM);
    Serial.print(&quot;PWM Level: &quot;);
    Serial.println(test_level_PWM);
    
Serial.flush();

Serial.println(&quot;What Resonance level do you want to test?&quot;);    
 while (Serial.available() == 0) {
     // Wait for user input
  }
  int test_level = Serial.parseInt();
    analogWrite(ResPin, test_level);
    Serial.print(&quot;Resonance Level: &quot;);
    Serial.println(test_level);

   delay(1000); 

</code></pre>
<p>Here is the serial monitor output. I was able to input the PWM level but the Resonance level was set to zero immediately after and then I was asked to input the PWM level again.</p>
<p><a href="https://i.stack.imgur.com/RvQ91.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/RvQ91.png" alt="enter image description here" /></a></p>
 16780: <p>Your line</p>
<p><code>   Serial.flush();</code></p>
<p>is probably intended to remove additional input after the expected numerical input. But that's not what it's doing. (See @jurai's comment)</p>
<p>Either avoid additional input like NewLine characters, or remove them using something like</p>
<p><code>while (Serial.available()) {delay(1); Serial.read();}</code></p>
<p>( The delay(1); takes care of slowly coming in serial data at 9600 Baud )</p>
