I want to make a serial program with input from the serial monitor and show each character with its binary and ASCII codes.

At the end of the program, I want to show the whole message sent from the serial monitor but I don't know how to stop the loop. Can someone help me please?

Here's my program:

String dataST;

void setup() {
  Serial.println("Message sent by serial monitor : ");

void loop() {
  for (int i = 0; i < 400 ; i++) {
    if (Serial.available()) {
      char dataOK = Serial.read();
      dataST += dataOK;
      Serial.print(" = ");
      for (int i = 7; i >= 0; i--) {
        byte bytes = bitRead(dataOK, i);
        Serial.print(bytes, BIN);
      Serial.print(" = ");
      int dataAS = dataOK;
      Serial.println(" ");
      Serial.println(" ");
  Serial.println("Message : ");
  dataST = "";

The result from the program above is:

Message sent by serial monitor :
Message :
Message :
Message :

And the result I want is:

Message sent by serial monitor:
H = 01001000 = 72 

e = 01100101 = 101 

y = 01111001 = 121 

Message :

Please help me.


Please take a look at the documentation of SerialEvent.

This might be exactly what you are looking for (after several small tweaks)

Code from the example:

  Serial Event example

  When new serial data arrives, this sketch adds it to a String.
  When a newline is received, the loop prints the string and clears it.

  A good test for this is to try it with a GPS receiver that sends out
  NMEA 0183 sentences.

  NOTE: The serialEvent() feature is not available on the Leonardo, Micro, or
  other ATmega32U4 based boards.

  created 9 May 2011
  by Tom Igoe

  This example code is in the public domain.


String inputString = "";         // a String to hold incoming data
bool stringComplete = false;  // whether the string is complete

void setup() {
  // initialize serial:
  // reserve 200 bytes for the inputString:

void loop() {
  // print the string when a newline arrives:
  if (stringComplete) {
    // clear the string:
    inputString = "";
    stringComplete = false;

  SerialEvent occurs whenever a new data comes in the hardware serial RX. This
  routine is run between each time loop() runs, so using delay inside loop can
  delay response. Multiple bytes of data may be available.
void serialEvent() {
  while (Serial.available()) {
    // get the new byte:
    char inChar = (char)Serial.read();
    // add it to the inputString:
    inputString += inChar;
    // if the incoming character is a newline, set a flag so the main loop can
    // do something about it:
    if (inChar == '\n') {
      stringComplete = true;

An embedded system is never meant to stop. If/when your loop() function exits (returns), it will be immediately called again.

If you need a process that runs to some state of completion and than stops, you're better off to leave loop() empty and write your own function that does what you need it to do, and call it at the end of setup().

You can either put an infinite loop at the bottom of your function:


is one way to do it, or just let it return to setup(). Then setup() will exit, your empty loop() function will be called (infinitely), which accomplishes the same thing.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.