Simple serial Communication
There are two important functions related to the serial input in the code hereinafter, and that's Serial.available() and Serial.read().- Serial.available() returns the number of characters (i.e. bytes of data) which have arrived in the serial buffer and that are ready to be read.
- Serial.read() returns the first (oldest) character in the buffer and removes that byte of data from the buffer.
Let us begin. The following code will make the Arduino ECHO anything you send to it. Therefore, if you type a 3, the Arduino will send back a 3. If you type a letter F, the Arduino will send back a letter F.
Enter the following code into your Arduino IDE and upload it to your Arduino.
/* Simple Serial ECHO script */ /* Use a variable called byteRead to temporarily store the data coming from the computer */ byte byteRead; void setup() { // Turn the Serial Protocol ON Serial.begin(9600); } void loop() { /* check if data has been sent from the computer: */ if (Serial.available()) { /* read the most recent byte */ byteRead = Serial.read(); /*ECHO the value that was read, back to the serial port. */ Serial.write(byteRead); } }
- Once the Arduino sketch has been uploaded to the Arduino. Open the Serial monitor, which looks like a magnifying glass at the top right section of the Arduino IDE. Please note, that you need to keep the USB connected to the Arduino during this process, as the USB cable is your communication link between your computer and the Arduino.
- Type anything into the top box of the Serial Monitor and press
on your keyboard. This will send a series of bytes to the Arduino. The Arduino will respond by sending back your typed message in the larger textbox.
Here we’ve introduced the readStringUntil() function, which makes it possible to combine all the characters in the sent message into a single Arduino string. In this case we’re waiting for the \n character, which is the newline character that comes at the end of a string sent in the Arduino serial monitor.
String my_name; void setup() { Serial.begin(9600); delay(2000); Serial.println("What is your name?"); } void loop() { if(Serial.available()){ my_name = Serial.readStringUntil('\n'); Serial.println("Nice to meet you, " + my_name + "!"); } }
Sending Commands.
A more usable scenario could be to send commands to the Arduino. Depending on what you send, the Arduino will perform different task. Here’s a somewhat abstract example on how to do this:String command; void setup() { Serial.begin(9600); } void loop() { if(Serial.available()){ command = Serial.readStringUntil('\n'); if(command.equals("init")){ initialize(); } else if(command.equals("send")){ send_message(); } else if(command.equals("data")){ get_data(); } else if(command.equals("reboot")){ reboot(); } else{ Serial.println("Invalid command"); } } }Notice the use of the equals() function. This is a function in the Arduino String class which returns true if the string in question is equal to the parameter string.
Post A Comment:
0 comments:
Post a Comment