0

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.begin(9600);
 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(dataOK);
 Serial.print(" = ");
 delay(200);
 for (int i = 7; i >= 0; i--) {
 byte bytes = bitRead(dataOK, i);
 Serial.print(bytes, BIN);
 }
 Serial.print(" = ");
 int dataAS = dataOK;
 Serial.print(dataAS);
 Serial.println(" ");
 Serial.println(" ");
 }
 }
 Serial.println("Message : ");
 Serial.print(dataST);
 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 :
Hey

Please help me.

dda
1,5951 gold badge12 silver badges17 bronze badges
asked Dec 28, 2019 at 10:29
0

2 Answers 2

1

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.
 http://www.arduino.cc/en/Tutorial/SerialEvent
*/
String inputString = ""; // a String to hold incoming data
bool stringComplete = false; // whether the string is complete
void setup() {
 // initialize serial:
 Serial.begin(9600);
 // reserve 200 bytes for the inputString:
 inputString.reserve(200);
}
void loop() {
 // print the string when a newline arrives:
 if (stringComplete) {
 Serial.println(inputString);
 // 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;
 }
 }
}
answered Dec 28, 2019 at 11:51
0

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:

for(;;);

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.

answered Dec 28, 2019 at 17:59
1
  • Or sleep (most of the time)? Commented Sep 14, 2022 at 15:11

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.