Project 2.01 Talking to the Board

In the last project the MC Trainer sent messages to the computer using Serial. In this project we will do the opposite! We can also use the Serial Port to send messages to the MC Trainer! These messages will pop up in the Serial port when you type them in and send them.

Project Code:

////////////////////////////////////////////
// 2.01 - Talking to the board

void setup() {
  Serial.begin(9600);
}

void loop() {
  while (Serial.available() == 0) {
    ;
  }

  byte messageSize = Serial.available();
  char message[messageSize];

  for (byte i = 0; i < messageSize; i++) {
    message[i] = Serial.read();
  }

  Serial.print("Message sent: "); Serial.println(message);
}
////////////////////////////////////////////

*If you’re copying and pasting the code, or typing from scratch, delete everything out of a new Arduino sketch and paste / type in the above text.

There are a few simple steps when we want to use the computer to communicate to the MC Trainer. Firstly, we must wait for Serial data to be sent from the Serial Port. The Serial.available() method will return the amount of data available from the Serial Port.

Let’s take a look at another kind of loop. In this program we use what’s called a while-loop. This kind of continuously runs while the conditional is true. See the table below for a breakdown of the while-loop used in the program:

Type while-loop
Conditional Serial.available() == 0
Code executed ;

The goal for the following lines of code is to keep us waiting in one spot in the program until we receive Serial data from the computer.

Serial.available() returns the amount of Serial data available to read from the computer. When it returns 0, that means there is no data to read. So, while there is no data to read, execute the code inside of the brackets “{}”.

The code inside of the brackets Is just a semi-colon. This essentially does nothing but take up some time. It’s an easy way to wait until something happens to move on. We are stuck in this while-loop (doing nothing) until our condition is met.

  while (Serial.available() == 0) {
    ;
  }

The next thing we need to do (After we’re out of the while-loop, meaning a message has been sent from the Serial port) is create a variable that can hold the incoming message. This will be an array of variable type char because char type variables hold a character. Using an array will allow us to store multiple characters in a convenient way.

How will we know how to size our array? In other words, how will we know how many spots our array needs for the incoming message? For that, we can use Serial.available() again. Remember that Serial.available() returns the amount of Serial data that is available to read. We can assign this returned value to a byte type for later use. 

byte messageSize = Serial.available();

Once we know the size of the message, we create a character array to hold that message. First, we make the array type “char”, then name it “message”, then we size it with the variable we made earlier “[messageSize]”.

char message[messageSize];

Let’s do an example. Let’s say our message is “Hello”. What you have to know is that although it looks like there are only 5 characters that make up the word “Hello”, there are really six when it is sent over from the computer. There is ‘H’, ‘e’, ‘l’, ‘l’, ‘o’, and ‘\0’. The ‘\0’ is called a NULL terminator and signifies the end of a string of text.

Serial.available() will return the number 6 (Indicating that there are 6 characters to be read) and we can now create a variable to hold all of the characters.

Now we have a character array with 6 empty spots. Next, we need to use the Serial.read() method to read the Serial data. This method will read one byte (character) at a time sequentially. That means that calling it once will return the first character of what is being sent over, calling it again will read the second, and so on. As the incoming message is being read it needs to be stored in the array we created earlier.

Since we stored the length of the message in the messageSize variable, we can tell a for-loop how many times it needs to loop to read the entire message. The for-loop loops as many times as the message is long, and puts the characters read into the message array. We use the variable i to keep track of our spot in the array.

for (byte i = 0; i < messageSize; i++) {
    message[i] = Serial.read();
}

Visualized is the character array below when the for-loop finishes with the example “Hello” (Arrays start at Index 0):

Index 0 1 2 3 4 5
Character H e l l o /0

Once the message is stored into the array, it is printed.

  Serial.print("Message sent: "); Serial.println(message);
}
////////////////////////////////////////////

To send data to the MC Trainer you first have to open the Serial monitor. Once the monitor is open, the text box at the top of it can be used to send a message. All you need to do is type in a message and hit the enter button. The computer will then attempt to send the message over Serial.

Previous
Previous

Project 3.00 Read Input

Next
Next

Project 2.00 Serial Printing