In this post we are going to learn how to use the USB serial port to communicate between your computer and the controller board. Let me put it another way: transferring information and commands to your board via USB cable.
Communication always consists of two parts, a transmitter and a receiver. In order to do this, we will need two things:
- A program on the controller board that sends/receives data. (We will be using bitbloq)
- A program on the computer that sends/receives data. (We will use the Arduino IDE Serial Monitor)
“Robonaut calling Eco Base, do you receive?”
WRITING VIA SERIAL PORT
The next example sends the word ‘BLACK‘ from the controller board when an IR sensor detects this colour. Connect that sensor to digital pin 3:
Part 1: Transmitter – Programming with bitbloq
To program the board to send data in bitbloq, all you need to do is use the Serial Print block.
This makes it very simple to create a program that compares the value of the sensor each second (1000 ms) and if correct, it sends the word. Load this program on your board:
Part 2: RECEIVER – Using the Arduino IDE Serial Monitor
As we said earlier, we will be using the Arduino IDE Serial Monitor. With the board connected, open the Arduino program and close bitbloq.
Now select the controller board under Tools/Board. If you are using the ZUM BT-328 select “Arduino BT w/ATmega328” or if you are using FreaduinoUNO, select “Arduino UNO“.
Select the port that the board is connected to under Tools/Port:
We can now open the Serial Monitor under Tools/Serial Monitor.
This window will pop up where we need to make sure that the 9600 baud rate is selected.
If the infrared sensor of our example starts to detect repeatedly, the word `BLACK´will appear repeatedly on the monitor:
To see more organised results, we can use the Serial PrintIn” block:
This will give us a clearer response:
READING THE SERIAL PORT
Part 1: RECEIVER – Programming with bitbloq
In this example, the receiver is the controller board. For everything received via the board, we will use the Bluetooth Serial Available do block to check that something has arrived via the serial port:
If so, we will read the received data and save it as a variable:
Now we can compare it and act according to the said data. For example, if we receive an upper case A, we will light up the LED on the board (D13). Or if any other data is received, we will switch off the LED. The program will look like this:
“But wait, have you compared the data to a 65?! –” Of course, dear robonaut, because bitbloq doesn´t have a block for comparing char text variables at the moment, (it´s only possible with strings). Don´t worry if this all sounds like Greek to you, all you need to know is that we are using numbers to identify letters in this example.
Letter A corresponds to decimal number 65 in ASCII code (see table at end of post).
Part 2: TRANSMITTER – Using the Arduino IDE Serial Monitor
Now the board is ready to receive. Close bitbloq and open Arduino IDE Serial Monitor again. This time make sure you select the “No line ending” option as well as the 9600 baud rate.
Enter an upper case A and hit Send. Does the LED light up? Have a go at sending any other command to switch off LED D13.
Here I will leave you with the ASCII table so that you can compare the letters and characters that pass via this serial port: