Using Serial.read() with Arduino | Part 2

👇 Download the Arduino Q Bridge Code👇 https://bit.ly/448L6OH Want to learn more? Check out our courses! https://bit.ly/33qhxbY **Get the code, transcript, challenges, etc for this lesson on our website** https://bit.ly/3eo4iyB We designed this circuit board for beginners! Kit-On-A-Shield: https://amzn.to/3lfWClU SHOP OUR FAVORITE STUFF! (affiliate links) --------------------------------------------------- Get your Free Trial of Altium PCB design Software https://www.altium.com/yt/programming... We use Rev Captions for our subtitles https://bit.ly/39trLeB Arduino UNO R3: Amazon: https://amzn.to/37eP4ra Newegg: https://bit.ly/3fahas8 Budget Arduino Kits: Amazon:https://amzn.to/3C0VqsH Newegg:https://bit.ly/3j4tISX Multimeter Options: Amazon: https://amzn.to/3rRo3E0 Newegg: https://bit.ly/3rJoekA Helping Hands: Amazon: https://amzn.to/3C8IYXZ Newegg: https://bit.ly/3fb03X1 Soldering Stations: Amazon: https://amzn.to/2VawmP4 Newegg: https://bit.ly/3BZ6oio AFFILIATES & REFERRALS --------------------------------------------------- ►Audible Plus Free trial: https://amzn.to/3j5IGrV ►Join Honey- Save Money https://bit.ly/3xmj7rH ►Download Glasswire for Free:https://bit.ly/3iv1fql FOLLOW US ELSEWHERE --------------------------------------------------- Facebook:   / programmingelectronicsacademy   Twitter:   / progelecacademy   Website: https://www.programmingelectronics.com/ 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 if any bytes are available in the serial receive buffer and while there are we need to read in the bytes in and save them to a temporary variable. We can use a while loop, Serial.available, Serial.read() to make this happen. 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 Here is the complete code to use Serial.read() to read in the entire message: //Many thanks to Nick Gammon for the basis of this code //http://www.gammon.com.au/serial const unsigned int MAX_MESSAGE_LENGTH = 12; void setup() { Serial.begin(9600); } void loop() { //Check to see if anything is available in the serial receive buffer while (Serial.available() _ 0) { //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 char inByte = Serial.read(); //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) Serial.println(message); //Reset for the next message message_pos = 0; } } } OK. This feels like a ton – I know! CONTINUED… https://bit.ly/3eo4iyB