How to Connect and Control an Arduino Board with Cascades, Part II

IMG_20140220_124315

In this second part I want to explain, how to control a connected Arduino via a Z30 device. Before you can do this, the Arduino has to be discovered by the device. The necessary steps are documented here in the first part.

After setting up a successful connection between Z30 and Arduino the path, which is saved in our Peripheral class (see blog part I) needs to be opened to perform any communication. I use a button to open the communication, because the Arduino needs some time to boot and I like to do it simple :-) If you try to open the path too early it might not work.

When the button is pressed, a method is called, which starts the communication. The variable m_serial is used to check, if a serial connection is already open and avoid us to open the path on each button click. In this case, the function returns immediately. Otherwise the path is read out and opened for read/write mode with open(). Don’t forget to check if the path is valid and return if it is not.

The next lines are to set the attributes for communication: The baude rate and the mode, which are saved in the  struct termios. The baude rate sets the communication rate and must be the same as for the Arduino. Look up which one you used for your Arduino program (here 9600), otherwise it won’t work. The TRANSNOW mode is used to transmit the message immediately:

To check if the steps above were successful, we write the first command, to switch the LED on:

The variable m_value sets the intensity and should be between 0 and 255 (0 is off). m_lastWrite contains the last value which was send to the Arduino. This way we avoid useless re-sending of the same value again and again .

Now we need a little more UI beside the button to be able to change the LED intensity. I decided to use a radio group to select different brightnesses (off, middle, full). Create them in the qml File an use an invoked method, when the onSelectedIndexChanged signal is received:

Each time the user select a different button writeSerial() is called. In this method the first thing is to verify which index was selected and set the intensity accordingly. I checked it with a switch-case-instruction and changed the intensity manually.

Because we only want to send the value to the Arduino, when it is connected and the path is open, the variable m_serial need to be checked. As you might remember from the part I of this blog m_serial is set to -1 in the constructor and after each disconnection. If the Arduino is connected, we send the new brightness value by calling write().

Now you learned how to connect the Arduino to your BlackBerry 10 device and how to control it. With that you can write an own little protocol which the Arduino can interpret. Further information can be found here at the website.

Download the whole project here.

One thought on “How to Connect and Control an Arduino Board with Cascades, Part II

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">