Pages: [1]   Go Down
Author Topic: How to write in Serial Monitor what I enter?  (Read 727 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 17
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi, how are you?

Well, I need enter with numbers in Serial Monitor and do some calculations with that number. So, when I enter, I take to print it, but when it's printed, the character come in ASCII and separated, for example:

1 - I enter with numbers: 345
2 - Print thus:   50
                       51
                       52


Then, I'd like to print what I entered, like:
1 - I enter with number: 345
2 - Print thus: 345

Someone knows how to do it?

Thanks so much.
Logged

0
Offline Offline
Tesla Member
***
Karma: 116
Posts: 8944
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The problem is with your code, which you have failed to post (use the code tag # in the tool bar when posting). Perhaps you are using Serial.write instead of Serial.print. Below is some simple serial test code.

Code:
// zoomkat 7-30-11 serial I/O string test
// type a string in serial monitor. then send or enter
// for IDE 0019 and later

String readString;

void setup() {
  Serial.begin(9600);
  Serial.println("serial test 0021"); // so I can keep track of what is loaded
}

void loop() {

  while (Serial.available()) {
    delay(2);  //delay to allow byte to arrive in input buffer
    char c = Serial.read();
    readString += c;
  }

  if (readString.length() >0) {
    Serial.println(readString);

    readString="";
  }
}

Logged

Consider the daffodil. And while you're doing that, I'll be over here, looking through your stuff.   smiley-cool

Grand Rapids, MI, USA
Offline Offline
Sr. Member
****
Karma: 6
Posts: 256
Yes, I remember
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

When you talk to the board with the serial monitor it sends characters, you need to parse these characters back into a number on the Arduino.  The standard C atoi() function does this nicely.  I think that needs an stdlib.h header file, but don't take my word for that.  If you give that function a c-string (char array) of numerals it will spit out an integer.  There are other flavors if you want something other than an int. 

For small values (0-255) that would fit in a byte variable you can also send characters who's ASCII value is equal to the number you want.  This takes some work with the Arduino serial monitor, but it is an option for some other terminals like Realterm.  http://realterm.sourceforge.net/
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 17
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Ok, It's works zoomkat. But it keep printing separated.

For example, I enter with number 259, but print one under the other, like: 2
                                                                                                         5
                                                                                                         9

I like to print in the same line, like 259.
What should I do?

Thanks.
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 549
Posts: 46113
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
What should I do?
Use some code that understands end-of-packet markers, and use them!

The serial monitor can send nothing, CR, or CR/LF, as the end of packet marker, depending on what you set using the drop-down menu in the lower, right corner. Then, you could modify this code:
Code:
#define SOP '<'
#define EOP '>'

bool started = false;
bool ended = false;

char inData[80];
byte index;

void setup()
{
   Serial.begin(57600);
   // Other stuff...
}

void loop()
{
  // Read all serial data available, as fast as possible
  while(Serial.available() > 0)
  {
    char inChar = Serial.read();
    if(inChar == SOP)
    {
       index = 0;
       inData[index] = '\0';
       started = true;
       ended = false;
    }
    else if(inChar == EOP)
    {
       ended = true;
       break;
    }
    else
    {
      if(index < 79)
      {
        inData[index] = inChar;
        index++;
        inData[index] = '\0';
      }
    }
  }

  // We are here either because all pending serial
  // data has been read OR because an end of
  // packet marker arrived. Which is it?
  if(started && ended)
  {
    // The end of packet marker arrived. Process the packet

    // Reset for the next packet
    started = false;
    ended = false;
    index = 0;
    inData[index] = '\0';
  }
}

Remove the started variable, and all the places it is used. Change the EOP value to \n or \r, depending on what you set in the serial monitor. Where it says "Process the packet", your "processing" consists of printing it.
Logged

0
Offline Offline
Tesla Member
***
Karma: 116
Posts: 8944
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Ok, It's works zoomkat. But it keep printing separated.

For example, I enter with number 259, but print one under the other, like: 2
                                                                                                         5
                                                                                                         9

I like to print in the same line, like 259.
What should I do?

Thanks.

Did you type in 259 and hit enter, or did you type 2, hit enter, type 5, hit enter, type 9, hit enter? You can add an end of packet delimiter (the default serial monitor settings do not add one) like a comma in the below code. The delimiter indicates the end of the character string being sent and starts processing of the code.

Code:
//zoomkat 3-5-12 simple delimited ',' string parse
//from serial port input (via serial monitor)
//and print result out serial port

String readString;

void setup() {
  Serial.begin(9600);
  Serial.println("serial delimit test 1.0"); // so I can keep track of what is loaded
}

void loop() {

  //expect a string like wer,qwe rty,123 456,hyre kjhg,
  //or like hello world,who are you?,bye!,
 
  if (Serial.available())  {
    char c = Serial.read();  //gets one byte from serial buffer
    if (c == ',') {
      //do stuff
      Serial.println(readString); //prints string to serial port out
      readString=""; //clears variable for new input     
     } 
    else {     
      readString += c; //makes the string readString
    }
  }
}

Logged

Consider the daffodil. And while you're doing that, I'll be over here, looking through your stuff.   smiley-cool

Pages: [1]   Go Up
Jump to: