[Resolved] Help with questions and answers on serial monitor

So basically, I'm doing a school project where I have to create a radio working on RTTY, I work with an arduino nano and an AD9850 for the frequency generator.

We have all working, we can put a message and it will transmits and we can also receive it.

But now we want to simplify eveything so we wanted to set up the frequency and the messages we want in the serial monitor, but I can't figure out how to.

What I want is : The serial monitor asks "what frequency do you want", then I type it, it says "Your frequency is...." and store the information in my frequency variable (I know how to do that) to tranceive it.

And then, the same thing with the message we want to communicate.

AND basically I want to set up the frenquency only one time, because once I set it up, I want to send multiple messages.

So, my loop look like this, I hope someone will understand what I want and could help, thanks.

void loop()
{
  String msg;
  
      if (Serial.available() > 0) 
{
  Serial.println("Message à communiquer : \n");
}


      if (Serial.available() > 0) 
{
  msg = Serial.readString();
  Serial.print("Message :");
  Serial.println(msg);
  rtty_txstring(msg);
  AD9850.wr_serial(0x04, 14000000); 
}
}

You can use Serial.parseInt() or Serial.parseFloat() to read a number typed into Serial Monitor. They have a one-second timeout so it is best to wait until Serial.available() returns a value greater than 0 before trying to parse the input. If a timeout occurs you will get an answer of 0 (or 0.0).

1 Like

Yes, I know how to read what I type in the serial monitor, it isn't the problem, I want to read 2 different informations in a row, I want the program to print the information for each one, but I don't want it to be spammed in the serial monitor.

I want something like that :
What is your value :
"answer 1"
Your value is : 1
(Then it stores the value in a variable)
What is your string :
"abc"
Your string is : abc
(Then stores the string to a variable)

And if possible, I want the

What is your string :
"abc"
Your string is : abc
(Then stores the string to a variable)

to be repeated so I can send several messages and the

I want something like that :
What is your value :
"answer 1"
Your value is : 1

Only asked one time

Check if the following sketch does what you want to achieve. Select Newline option in the Line ending tab of Serial Monitor.

char myData[20];

void setup()
{
  Serial.begin(9600);
}

void loop()
{
  Serial.println("What is your value?");
  while (Serial.available() == 0)
  {
    ;
  }
  int value = Serial.parseInt();
  Serial.print("Your value is: "); Serial.println(value);
  //--------------------------------
  while(Serial.available() != 0)
  {
    (void)Serial.read(); //remove Newline charcater from buffer
  }
  Serial.println("What is your string?");
  while (Serial.available() == 0)
  {
    ;
  }
  byte m = Serial.readBytesUntil('\n', myData, 20);
  myData[m] = '\0';  //insert null-charcater
  Serial.print("Your string is: "); Serial.println(myData);
  Serial.println();
}

Output:

What is your value?
Your value is: 123
What is your string?
Your string is: Arduino
1 Like

Oh my, It looks exactly like what I want to achieve, I will test with my program at the university with the radio tomorrow and I'll tell you if it works, but thanks a lot it looks perfectly like what I was thinking of

1 Like
char myData[20];
 byte m = Serial.readBytesUntil('\n', myData, 20);
  myData[m] = '\0';  //insert null-charcater

If the user enters 20 bytes then which level of the array will the '\0' be put into ?

Out of array bound; therefore, the user must enter at best 19 characters. Am I correct?

You helped me a lot, I immediately tested it this morning, it wasn't working at first because I wanted to set a frequency in MHz so I turned the int into a float etc, I changed a bit some things but you had the key, thanks, my problem is solved, if you want me to send you my code let me know

You are correct and your code should be written to prevent it happening

1 Like

Hopefully, the following changes would do the job:

  byte m = Serial.readBytesUntil('\n', myData, sizeof myData-1);
  myData[m] = '\0';  //insert null-character

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.