Hello All, I'm trying to write code to allow a user to enter at least two inputs. Here my so far. Its pretty straight forward but I'm doing something wrong. Please help.
int elevation;
int azmuth;
String msg1 = "What is the Azmuth?";
String msg2 = "Going to :";
String msg3 = "What is the Elevation?";
void setup() {
Serial.begin(9600);
}
void loop() {
azmuth = 0;
elevation = 0;
Serial.println(msg1);
while (elevation == 0){
}
azmuth = Serial.parseInt();
Serial.println(msg3);
while (Serial.available()== 0){
}
elevation = Serial.parseInt();
Serial.println(msg2 + azmuth + "/" + elevation);
}
Welcome to the forum
Why did you start a topic in the Uncategorised category of the forum when its description is
DO NOT CREATE TOPICS IN THIS CATEGORY
Your topic has been moved to the Programming category
Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'
Use code tags (the < CODE/ > icon above the compose window) to make it easier to read and copy for examination
https://forum.arduino.cc/t/how-to-get-the-best-out-of-this-forum
while (elevation == 0){
}
How is the code ever going to get out of this while loop as the value of elevation does not change within it ?
You need to give the user a way to enter the elevation inside the while loop
Thank you for the help. I did have the code as
While (Serial.available() ==0) {}
But the then it would loop with a result of:
Going to: azimuth/0
Let me change the code back and rerun the sketch and give you the total sketch and output.
Thanks again!
OK here is the redone code. Any Help getting me past my issue is much appreciated!
< CODE/ >
int elevation;
int azmuth;
String msg1 = "What is the Azmuth?";
String msg2 = "Going to :";
String msg3 = "What is the Elevation?";
void setup() {
Serial.begin(9600);
}
void loop() {
azmuth = 0;
elevation = 0;
Serial.println(msg1);
while (Serial.available() == 0){
}
azmuth = Serial.parseInt();
Serial.println(msg3);
while (Serial.available()== 0){
}
elevation = Serial.parseInt();
Serial.println(msg2 + azmuth + "/" + elevation);
}
</ CODE >
serial monitor output:
What is the azimuth?
The input was 323
And the response is (without waiting) :
What is the elevation?
Going to : 323/0
What is the azimuth?
Hi @LarryDo Welcome to the forum..
took a quick look, more than likely even after parseInt, there's is still a char left in the serial buffer, probably a new line char..
quick fix is to clear out any pending before expecting more..
tried it in a sim, seemed to clear up the issue you are seeing..
int elevation;
int azmuth;
String msg1 = "What is the Azmuth?";
String msg2 = "Going to :";
String msg3 = "What is the Elevation?";
void setup() {
Serial.begin(9600);
}
void loop() {
azmuth = 0;
elevation = 0;
Serial.println(msg1);
while (Serial.available() == 0) {
}
azmuth = Serial.parseInt();
while (Serial.available())Serial.read();
Serial.println(msg3);
while (Serial.available() == 0) {
}
elevation = Serial.parseInt();
while (Serial.available())Serial.read();
Serial.println(msg2 + azmuth + "/" + elevation);
}
good luck.. ~q
gcjr
July 31, 2023, 10:15am
9
look this over
obviously need to do something with the values
set serial monitor to terminate with Newline
int elevation;
int azmuth;
int
getValue (
const char *prompt )
{
char buf [30];
Serial.print (prompt);
while (! Serial.available ()) // wait for some input
;
int n = Serial.readBytesUntil ('\n', buf, sizeof(buf)-1);
buf [n] = '\0';
int val = atoi (buf);
Serial.print (": ");
Serial.println (val);
return val;
}
void setup ()
{
Serial.begin(9600);
azmuth = getValue ("azmuth:");
elevation = getValue ("elevation:");
}
void loop()
{
}
system
Closed
January 27, 2024, 10:15am
10
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.