Using Serial.read() with Arduino | Part 2

preview_player
Показать описание
🤩 FREE Arduino Crash Course 👇👇

Want to learn more? Check out our courses!

***Get the code, transcript, challenges, etc for this lesson on our website***

We designed this circuit board for beginners!

SHOP OUR FAVORITE STUFF! (affiliate links)
---------------------------------------------------

Get your Free Trial of Altium PCB design Software
We use Rev Captions for our subtitles

Arduino UNO R3:

Budget Arduino Kits:

Multimeter Options:

Helping Hands:

Soldering Stations:

AFFILIATES & REFERRALS
---------------------------------------------------

FOLLOW US ELSEWHERE
---------------------------------------------------

Now let’s tackle the first step of our algorithm – we create a character array to hold the incoming message and a position variable to help us move through each element in the array. We’ll also create a constant to hold the max length of our message and use this to initialize the character array.

const unsigned int MAX_MESSAGE_LENGTH = 12;

Now we need to check to see if the byte we read is a terminating character or not… We can use an if-else statement for that. If it’s not a terminating character we’ll do one thing, and if it is a terminating character we’ll do something else.

Now if we do get the terminating character that means we have received our entire message and we can actually do something with the message or data we received. In this case we’ll print the message to the Serial Monitor window. We’ll also need to reset our character array to prepare for the next message.

Before we can call this complete, we need to enforce the max message length in the protocol. This will prevent us from exceeding the space that we allotted in our character array. We can add this guard to our existing if statement.

if ( inByte != '\n' && (message_pos - MAX_MESSAGE_LENGTH - 1) )
FULL SERIAL.READ() CODE

//Many thanks to Nick Gammon for the basis of this code
const unsigned int MAX_MESSAGE_LENGTH = 12;

void setup() {
}

void loop() {

//Check to see if anything is available in the serial receive buffer
{
//Create a place to hold the incoming message
static char message[MAX_MESSAGE_LENGTH];
static unsigned int message_pos = 0;

//Read the next available byte in the serial receive buffer

//Message coming in (check not terminating character) and guard for over message size
if ( inByte != '\n' && (message_pos - MAX_MESSAGE_LENGTH - 1) )
{
//Add the incoming byte to our message
message[message_pos] = inByte;
message_pos++;
}
//Full message received...
else
{
//Add null character to string
message[message_pos] = '\0';

//Print the message (or do other things)

//Reset for the next message
message_pos = 0;
}
}
}

OK. This feels like a ton – I know!

CONTINUED…
Рекомендации по теме
Комментарии
Автор

one of the best tutorials ive ever seen...The format, content, even the voice is cool ...Thank you

esmirhodzic
Автор

Thank you so much for these videos! I struggled so much trying to send serial data via hc-05 bluetooth module connection and this helped me immensely. I tried quite a few other tutorials but none of them broke down serial communication the way yours did. Very well done and very much appreciated!

davesee
Автор

It's impossible not to subscribe after this smooth tutorial

blackartista
Автор

The best tutorial ever. I have watched a bunch of other videos, they all just throw codes at me without really get to understand the fundamentals. This video explained every bit of the code "bit by bit". Definately filled all the questions marks I had previously!

jezzeryuen
Автор

Very good tutorial, and helped me get started on Serial for sure. I did find one thing that might need to be tweaked. Every loop if you exceed the MAX_MESSAGE_SIZE you will lose 1 char out of the buffer. This is because we call Serial.read() before checking the max length of the array. There are many ways to solve this, I ended up doing the check for the max message size before a serial read to prevent losing one position out of the buffer. Using the identical else to clean up anre print the buffer, so I didn't read an extra byte from the buffer. Thanks again for the great content!

ericspooner
Автор

Parts 1 & 2 have cleared up hours of head scratching after trying to read tutorials on sending data via serial in Stackoverflow and Arduino forums. I come from a JavaScript background, but the devil is in some of the minor details which go unexplained in a lot of forums out there (probably because prior knowledge of arduino programming and concepts is assumed).

Those minor details given here made it much easier to understand code elsewhere that had me flummoxed. Thank you; you've earned my sub.

TinyMaths
Автор

I'm having trouble understanding Serial Communication. This is a life saver! Thankyou!

noellorenzopagatpat
Автор

The quality of your tutorials is incredible. Thank you!

MathiasBacher
Автор

Dude, you are a wicked good teacher. One of the best I've seen in 30 years in the electronics game.

robertcameronjones
Автор

Thank you for the simple to understand and detailed explanation of this. It's super helpful.

jonvannatto
Автор

Hi, it is the best teaching I ever watch. One question, why did you add '\0' at the end of the message?

fnkho
Автор

Thank you so much!
with this I can control a servo wirelessly using node-red serial out
you just saved my thesis dear sir!

TheInfinitymijinion
Автор

very informative to understand Serial. thanks

EDLAM
Автор

It was a great explanation. Thank you so much.

suheylk
Автор

Super Great Video! but I dont see anything in your code to indicate which pin incoming data would coming in?
(also i'm kind of a noob)

Thomas-bsem
Автор

I have a q. Are you sure we have to put the char message and message_pos inside the while loop?

periklisdrakousis
Автор

Great. now how do I compare the “message” to a constant in an if statement to actually use it?

themecue
Автор

9600 bits per second og bytes per second. At about 3:10 in the video you said one thing and wrote the other in the subtitle?

ProcrastinatingGameCat
Автор

how to send data from arduino uno to my esp8266 nodemcu and how to connect the to components ?

icjay
Автор

so i can create a while loop to read msg.payload from node red before running the rest of the sketch ?

terryterry