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.