The only difference between Serial.print() and Serial.println() is that line break, which can make it easier to read the information in the serial monitor. The line break means that the next time we print something to the serial monitor (including the next time through our loop code) the printed serial information will appear on a new line. We learned that Serial.println() automatically adds a line break how does this affect the way our code appears in the Serial monitor? We have seen that our final line of serial code uses Serial.println() rather than Serial.print(). Remember, the values for sensorValue and outputValue you see in your serial monitor will look different than in this screenshot, and will change as you turn your potentiometer. We’ll look at this more closely on the next page. But instead of Serial.print(), we use Serial.println(), which will print a line break. The fourth line prints the value of our outputValue variable. However, we also have a new symbol: What does that “ \t” mean? The \t tells the Arduino serial monitor to include a tab, a set of spaces, in your printed output. The third line for our serial loop code uses quotation marks again, so we know that we are going to print a string. Without quotation marks, the Arduino will print out the numeric value stored in the variable, rather than the name of the variable. The second line of serial.print() will print out the value of the variable sensorValue, which is a number. The first line of the code prints out the string sensor = (including the spaces before and after the equals sign. Let’s look at how these work together with Serial.print()Įverything inside the quotation marks will be printed to the serial monitor including spaces and punctuation We also see our variables referenced in the code. We know that any text inside of quotation marks is a string and will be represented as text. Now we’ll take a closer look at how each line of code prints to the Serial monitor. Any alphanumeric character, including spaces and punctuation marks, are represented by strings. Text is represented by strings in our code. The quotation marks enclose the full group of characters including all of the letters, the spaces, and the punctuation. How do we use strings in our code? We place quotation marks around a string to identify it. We will see how this works as we look more closely at our code in the next couple of pages. Sometimes you need to use text in your code, to pass along textual information, or to give context to other data. Why do we need strings? Computers normally only work with the value of numbers. Any letters, numbers or other alphanumeric characters (including spaces and punctuation marks) are represented by strings in our code. In order to send words to our serial monitor, we use something called a string.Ī string is a representation of text in a programming language. If we look at the first serial.print line, we see words and quotation marks around them. Sending words to the serial monitor: Strings Here’s an example of a line of output this code will display in the serial monitor. The serial code in our loop includes the four lines below:Īll four of these lines of code together print the single line in the serial monitor which includes the words “sensor =”, the value of our sensor, a tab, the text “output = “ and the mapped value. Our sketch uses both of these functions in order to format the information on the computer screen. The Serial library has 2 functions for sending information to our computer: Serial.print() and Serial.println(). Now that we understand where to find our serial monitor, we can talk about our use of serial in the loop code. Here’s what the serial monitor window looks like when running this sketch: The Arduino and computer must use the same rate of communication: the value set in Serial.begin() By default, the baud rate of your serial monitor will be set to 9600, which matches the value we set in the serial.begin() function in our setup code, so you shouldn’t have to make any adjustments. As we said before, the baud rate is the rate of communication that the computer and Arduino use to talk to each other. and a drop down menu that controls the rate of communication, or baud rate, between our computer and the Arduino. When we open the serial monitor, we see a window that displays responses from the Arduino. To open the Serial Monitor, click the button at the top of the Arduino IDE. It is helpful for de-bugging and for learning what values a sensor or variable resistor produces. The serial monitor is a feature of the Arduino IDE that shows us information sent from the Arduino.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |