I am programming a potentiometer, and I got into a trouble while programming the input from Serial monitor. This is part of the code I have, that is supposed to display the prompt "Input the desired amplitude, 0 to 150V: ", and when the user enters a value, it reads and stores the value in the int amp. However, when I run this code, the serial monitor displays
"Input the desired amplitude, 0 to 150V: (amp)V"
"Input the desired amplitude, 0 to 150V: 0V".
Why is the extra second line with 0 appearing?
//amplitude is in V
int amp = 0;
unsigned int wait = 0;
void setup() {
Serial.begin(9600);
}
void loop() {
//voltage input
Serial.print("Input the desired amplitude, 0 to 150V: ");
wait = 1;
while(wait){
if(Serial.available()>0){
wait = 0;
Serial.begin(9600);
amp = Serial.parseInt();
Serial.print(amp);
Serial.println("V");
}
}
Serial.flush();
delay(100);
}
I solved it. Somehow I didn't notice the Serial.begin(9600) in the loop. Deleted it, and seemed like I can change the Serial monitor setting to not make it send '\n' after an input, and that solved the issue. Thank you for your help.