Arduino LESSON 12: Simple and Easy Way to Read Strings, Int’s and float’s Over Arduino Serial Port
Arduino LESSON 12: Simple and Easy Way to Read Strings, Int’s and float’s Over Arduino Serial Port
One of my major annoyances in programming is the ambiguity around serial port data input. There are several Arduino tutorials that make things far more difficult than it has to be. There are several tough and sophisticated methods for reading data from a serial port, however there is one that is simple. While we briefly discussed this in previous lessons, it is such an essential topic that I want to focus on it in this session.
To read data from a serial port, you must first establish what sort of data you will be expecting. Three basic data types can meet almost all of your requirements. Strings, Floats, and Ints can usually be used to make almost any project work. As a result, you must understand how to read these three data formats through the serial port. The Serial.readString() call is the simplest way to read a text string. The Serial.parseFloat() command is the simplest method to read a Float, while the Serial.parseInt() command is the simplest way to read an Int.
Let's put this into practise by doing the following. Create a program that prompts the user for his age, weight, and height in feet. Then, in a beautifully structured output, convey the information back to the user.
To achieve this, we must first select what type of variables to utilise for each of these three bits of data. Because a name is a string of text, you would naturally use a String for it. We would almost always round to the nearest whole number when storing the weight, thus it makes logical to use an Int for this. When it comes to height, we would not typically round to the closest foot... we may say that someone was 5.5 feet tall. Because we want decimal values, we should use a float in this situation. So we'd define these variables as the appropriate type and then read them in with the appropriate instructions.
Also, keep in mind that while reading data from the serial port, you must perform three things: 1) Instruct the user to enter data, 2) wait for him to do so, and 3) read the data. This should be a review, as we've done it in numerous previous classes.
Try writing the programme yourself, but if you get stuck, you may look at the code I wrote for this problem below.
String myName; //Declare a String variable to hold your name
int age; //Declare an Int variable to hold your age
float height; //Declare a float variable to hold your height
void setup() {
Serial.begin(9600); // turn on Serial Port
}
void loop() {
Serial.println("Please enter your name: "); //Prompt User for input
while (Serial.available()==0) { //Wait for user input
}
myName=Serial.readString(); //Read user input into myName
Serial.println("How old are you? "); //Prompt User for input
while (Serial.available()==0) {
}
age=Serial.parseInt(); //Read user input into age
Serial.println("How tall are you? "); //Prompt User for input
while (Serial.available()==0) {
}
height=Serial.parseFloat(); //Read user input into height
Serial.print("Hello "); //Print out nicely formatted output.
Serial.print(myName);
Serial.print(", you are ");
Serial.print(age);
Serial.println(" years old,");
Serial.print("and you are ");
Serial.print(height);
Serial.println(" feet tall.");
Serial.println("");
}
OK, hopefully you were successful in getting this to work. It's now up to you to go it alone and create your own programme.
Inquire about the user's name and weight in pounds. Then double it by his weight in ounces, grammes, and carats. It's worth noting that that's carats, as in diamond carats, not carrots, as in carrots you eat.
After doing these computations, send a message to the user addressing him by name and informing him of his weight in pounds, ounces, grams, and carats.
You will need to conduct some study to figure out how to perform the conversions for this project.
What's Your Reaction?