I'd like to ask you about serial communications.

I'll upload it again because my previous writing has been erased.

Hello. I'd like to ask you about serial communications.

I'm trying to print the value of the current ADC on a serial monitor using the Serial.print() function.

I'll google it. Serial.From Begin (speed, config), I found that config's default settings are 8data, no parity, and 1 stop bit.
I understand that when you print out a value on a serial monitor, you can only express it up to eight bits.
If it's eight bits, you'll only be able to print numbers from 0 to 256 in the Serial.print function, but the short type and the range of values are the same. After 32,767, there was an overflow, and any negative value was output.
I really want to know why. It's so strange that I think I misunderstood the data on the Internet.

And...
The current code receives the ADC value of 10 bits, and stores the ADC value in the float type variable and outputs it on the serial monitor.
I know that we need to send 8 bits for serial communication, so I'm going to change the float type to the 8-bit data type, the Char type.
When I googled it, is it possible to change it from float type to char type, or is it a problem that can be solved by just changing the shape?
I have a question here. If the ADC value is 1023, is it printed as 255 instead of 1023 when the shape is changed to Char?

Thank you for reading it. Please give me an answer.
// Here's what I wrote in my previous article.


// looked at the subsequent answers and thought.
This is what I thought.
Currently, ADC is being imported into the upper two bits, lower eight bits, by manipulating the register.
You told me to use the Serial.println() and Serial.write() functions confused, but first we looked at Serial.readStringUntil.

Put the value of the top 2 bits into the write() function and the value of the bottom 8 bits into the println() function.
I don't care what you put in the println() function here.
The problem is, if the top two bits are 11, it's actually 768, but the write() function can only store up to 127.I don't know what to do.
Should I use the hexadecimal number of the Aski code?

This is about AsAWOL's answer.
The reason why ADC values are stored in float type variables is that the current code receives 10 ADC values, obtains the average values, and displays them on the serial monitor. But to get the average, we had to use division, but we needed a decimal point, so we used the float type.

While I was writing, I suddenly remembered something.
The Serial.print () function originally could only represent eight bits of data, but in fact, it could represent up to a 16-bit representation of 16 bits.
So, will the problem be solved if I save the ADC value in short form?
But I'm not sure how Serial can express up to 16 bits. etc.) should be written on the PPT.
I've been looking for it through Googleing, but I don't think it's coming out well.
How come the code Serial.println (32767) doesn't go wrong? (The cereal monitor shows 32767 as normal.)

I'm sorry for the long text. Thank you for reading it, and I look forward to hearing from you.

You can use SerialTransfer.h to automatically packetize and parse your data for inter-Arduino communication without the headace. The library is installable through the Arduino IDE and includes many examples.

Here are the library's features:

This library:

  • can be downloaded via the Arduino IDE's Libraries Manager (search "SerialTransfer.h")
  • works with "software-serial" libraries
  • is non blocking
  • uses packet delimiters
  • uses consistent overhead byte stuffing
  • uses CRC-8 (Polynomial 0x9B with lookup table)
  • allows the use of dynamically sized packets (packets can have payload lengths anywhere from 1 to 254 bytes)
  • can transfer bytes, ints, floats, and even structs!!

Example TX Arduino Sketch:

#include "SerialTransfer.h"

SerialTransfer myTransfer;

void setup()
{
  Serial.begin(115200);
  Serial1.begin(115200);
  myTransfer.begin(Serial1);
}

void loop()
{
  float floatVal = 5.67;

  myTransfer.txObj(floatVal, sizeof(floatVal));
  myTransfer.sendData(sizeof(floatVal));
  delay(100);
}

Example RX Arduino Sketch:

#include "SerialTransfer.h"

SerialTransfer myTransfer;

void setup()
{
  Serial.begin(115200);
  Serial1.begin(115200);
  myTransfer.begin(Serial1);
}

void loop()
{
  if(myTransfer.available())
  {
    float floatVal;
    
    myTransfer.rxObj(floatVal, sizeof(floatVal));
    
    Serial.println("New Data: ");
    Serial.println(floatVal);
  }
  else if(myTransfer.status < 0)
  {
    Serial.print("ERROR: ");

    if(myTransfer.status == -1)
      Serial.println(F("CRC_ERROR"));
    else if(myTransfer.status == -2)
      Serial.println(F("PAYLOAD_ERROR"));
    else if(myTransfer.status == -3)
      Serial.println(F("STOP_BYTE_ERROR"));
  }
}

For theory behind robust serial communication, check out the tutorials Serial Input Basics and Serial Input Advanced.

Thank you for your answer.

Does Power_Brocker mean that using SerialTransfer.h, data ranges of type int or float can also be printed on the serial monitor?
Or do you mean that the int type can be printed if you set it up?

I'll try downloading the header file and find out.
If you can't tell at all, I'll ask you a question. Thank you!

Serial.write(value); sends a single 8-bit character (byte) to Serial Monitor.

Serial.print(value); sends one or more 8-bit characters (bytes) to Serial Monitor. The characters it sends depend on the type of variable.

For an integer value, Serial.print(value); will convert the value into decimal digits. For example if the value is 1234, the Serial Monitor will be sent the four characters "1234".

For a floating-point value, Serial.print(value); will convert the value into digits with (by default) two digits after the decimal point. For example if the value is 1234.567 the Serial Monitor will be sent the seven characters "1234.56". You can specify the number of digits after the decimal point: Serial.print(123.4567, 3) would send "1234.567".

Does that help explain how you can display values greater than 255 using 8-bit characters?

01037839648:
Does Power_Brocker mean that using SerialTransfer.h, data ranges of type int or float can also be printed on the serial monitor?
Or do you mean that the int type can be printed if you set it up?

The library is used for reliably transferring information via serial UART ports. Once you receive a datapacket, you can then print whatever pieces of data you want to the serial monitor regardless of type. Does that answer your question?

01037839648:
I'll upload it again because my previous writing has been erased.

Was this your previous writing?

1. Serial.write(arg);
(1) The arg is an 8-bit value.
(2) The write() method puts the value of Step-1(1) onto Transmit Buffer of UNO and then onto Transmit Register of the MCU and then onto the OutputBox of Serial Monitor (Fig-1). If the arrived 8-bit value matches with the ASCII Code (Fig-2) of a printable character (a-z, A-Z, 0-9, special character like $ and others. punctuation marks like ! and others), then that character will appear on the Serial Monitor. For example: execution of 'Serial.write(0x41);' will show A on the Serial Monitor.


Figure-1:


Figure-2:

2. Serial.print(arg);
(1) Serial.print('A'); //A appears on Serial Monitor
==> Serial.write(0x41); //A appears on Serial Monitor

(2) Serial.print("CD"); //CD appears on Serial Monitor
==> Serial.print('C');
==> Serial.write(0x43); //C appears on Serial Monitor
//------------------------------------------------------------
==> Serial.print('D');
==> Seria;.write(0x44); //D appears on Serial Monitor

(3) Serial.print(0x41); //65 appears on Serial Monitor
==> Serial.print(0x41, DEC); //DEC = decimal base = base 10 is understood
==> Serial.print("65"); //the ecimal value of 0x41 is 65 (4x161 + 1x160)
==> Serial.print('6');
==> Serial.write(0x36); //6 appears o Serial Monitor
//----------------------------------------------------------------
==> Serial.print('5');
==> Serial.write(0x35); //5 appears on Serial Monitor

(4) Serial.print(0x34, HEX); //34 appears on Serial Monitor; HEX = hexadecimal base = base 16
==> Serial.print("34");
==> Serial.print('3');
==> Serial.write(0x33); //3 appears on Serial Monitor
//---------------------------------------------------------------
==> Serial.print('4');
==> Serial.write(0x34); //4 appears on Serial Monitor

I understand that when you print out a value on a serial monitor, you can only express it up to eight bits.

You seem to misunderstand the difference between the inherent value of a number (like -1, 12, or 3.1416) and its visual representation like "-1", "XII", or "π"
When you use Serial.print(aNumber), the value of the number is converted to a series of printing characters that have standardized values that are all less than 8bits, so it doesn't matter that the Serial port itself only sends 8bit characters.

The same thing is relevant in the reverse direction. If you type "12345" into the serial monitor and Serial.read() it on the Arduino, you'll get 5 separate characters reflecting the printed representation (49 50 51 52 53), rather than anything that directly resembles the value.