Go Down

Topic: Basic Help needed (Read 166 times) previous topic - next topic

amsetti

Hello :)
I am new to this amazing world and I started to do some basic projects (following tutorials) to explore the possibilities.
All good for a while, but at a certain point I started experiencing issues and tried to debug the problem myself, by simplifying the code more and more to get to the root of the  issue.
It simply appears that my board does not get the whole code i am sending, and does not behave as expected.

Board: Arduino MKR1000
IDE version 1.8.13 (Windows)
Code:
String mex="Please insert a number";
String mex2="Your number is: ";
int j;


void setup() {
  // put your setup code here, to run once:
Serial.begin(9600);
}


void loop() {
  // put your main code here, to run repeatedly:  
Serial.println(mex);
 while(Serial.available()==0){
  }
j=Serial.parseInt();
Serial.print(mex2);
Serial.println(j);
}



I would then expect the serial monitor to start by asking me the question (mex), but it DOES NOT!
Knowing what i coded, I provide the answer anyways, and it gives me the answer, followed immediately after by another question (mex) AND an answer that i did not type, resetting the value of j to 0.
Then the function starts looping, asking the number as expected, but then always resetting the value to 0 and printing, which is not coded, and i do not understand where it is getting this command from :(
In this example, i typed the following numbers, followed by "Enter" each time: 2; 3; 4; 123. 


Serial monitor:
Your number is: 2
Please insert a number
Your number is: 0
Please insert a number
Your number is: 3
Please insert a number
Your number is: 0
Please insert a number
Your number is: 4
Please insert a number
Your number is: 0
Please insert a number
Your number is: 123
Please insert a number
Your number is: 0
Please insert a number


Can you please help me understand what could possibly be going on with my board?
I cannot get over the fact that the absolute first line is "print message" and it does not print it..
Thanks you very much in advance, any input is precious. :)
Alessandro

amsetti

#1
Jan 12, 2021, 01:26 am Last Edit: Jan 12, 2021, 01:37 am by amsetti
I have been instructed to change the serial monitor setting from "NewLine" to "NoLineEnding", as that is messing with the typed integer. It now avoids resetting the variable, and does not print it two times in a row :)

Still, i cannot understand why it doesn't print the first command >:(
I inserted a countdown at the beginning, but still cannot get it to print the first line.
It starts with 2, 1, mex, instead of 3, 2, 1, mex.
(Only in the first loop, though, it does print it every other loop)
Code: [Select]
void loop() {
  delay(1000);
  Serial.println("3");
  delay(1000);
  Serial.println("2");
  delay(1000);
  Serial.println("1");
  delay(1000);
  Serial.println(mex);



Go Up