Serial Monitor Tutorial - masonandrewmann/MEAP GitHub Wiki
The serial monitor is the M.E.A.P. board's most powerful debugging tool. It allows the board to send messages to a computer. We can use these messages to get a glimpse into the state of the program running within the board. We can use it to read the state of variables, see whether a button has been pressed, monitor the state of running algorithms and much more. When the M.E.A.P. board prints to the serial monitor, it is sending text data to your computer through the USB cable, which can be read within the Arduino IDE.
Viewing the Serial Monitor
If you have code that is already printing to the Serial Monitor (such as the Hardware_Test.ino example code), we just need to do a few things to read it.
-
First we need to open the Serial Monitor within the Arduino IDE. You can do this by clicking Tools > Serial Monitor or by pressing the magnifying glass symbol in the top right of your code window. This opens a new window which will print data received over the Serial port we are currently connected to.
-
When you first open this window you may see random characters being printed. This is because we need to be sure the Serial Monitor is running at the same Baud rate as the program on the M.E.A.P. board (Baud rate can be thought of similarly to the sample rate of an audio file, it is the speed at which data is transferred between devices in bits per second.) Check the .ino file uploaded to the board to determine it's baud rate. There should be a line within the setup() function of the .ino that reads Serial.begin(BAUD_RATE) where BAUD_RATE will be some number. All M.E.A.P. examples use a baud rate of 115200, however code from other repositories sometimes use other rates, often 9600. Using the dropdown menu in the bottom right of the Serial Monitor window, change the baud rate to match the baud rate of your program.
-
You should now be receiving Serial messages properly! If you are running the Harware_Test.ino program, try moving one of the DIP switches or pressing a capacitive touch pad and you should see a message telling you what you pressed.
Printing to the Serial Monitor
-
Printing messages to the Serial monitor is fairly straightforward, there are just a few functions we need to familiarize ourselves with.
-
Serial.begin(115200); is a function that will need to be run just once in setup(). It tells the M.E.A.P. that we are going to be using the serial port in this program and that we want to send data at a baud rate of 115200 bits/second. If you want to use a different baud rate, you can just replace 115200 with a different number, however for the purposes of this class, 115200 should be fine for every program.
-
Once you have set up the serial port, we can now send messages whenever we want. We have two main functions for this.
-
Serial.println("Hello world!"); will print the phrase "Hello world!" followed by a line break to the serial port whenever this line is run. If you want to print different text simply replace the text within the quotation marks.
-
You can also use this function to print the state of a variable. In this case remove the quotation marks and type the variable's name within the parentheses instead. The function will print whatever data the variable contains to the serial monitor. For example if you have a variable named decayTime that you have assigned the value 500 to, the line Serial.println(decayTime); will print the number "500" to the serial port
-
Serial.print(); functions exactly the same as Serial.println(); except that it doesn't print a line break at the end of your message, allowing you to print multiple messages to one line using subsequent Serial.print(); calls.