Lab 4: Communicating

In this lab, you’ll explore serial communication by talking with Processing, the programming language and development environment that provided the original inspiration for the Arduino IDE. Processing is a very useful partner for Arduino — you’ll use it again in a few weeks.

1. Download Processing. If you need to look up any of the commands you’re using in Processing, check the Library and Reference sections.

2. Do this tutorial from Sparkfun. Please type in each line of code yourself, rather than copying and pasting the code. Doing so will help you understand the code much more deeply. It will also make you aware of your typos, which will help you learn to write less buggy code.

A few things to watch out for in this tutorial:

The first chunk of Processing code that begins with “String portName = …” should be inside of the setup() function, which is not said very clearly.

When setting your port, try [0] for Macs, as the tutorial suggests. For Windows, try [1].  You’ll know you’re accessing the wrong port if you get an error. Once you figure out which number to use, use the same number every time you need to access the port in the code (at one point in the sample code, the value inexplicably changes to [4] — you should not do that).

Be sure to load your Arduino sketch, and then run your Processing sketch. If you run Processing first, you’ll get an error that says your serial port is busy.

For the lucky ones, connecting Processing to your serial port will be a breeze. For those of you with grouchy computers, this might be a pain. The issue is that a serial port cannot be accessed by two programs simultaneously. If you’re being told that the serial port is busy, even though you’re sure you uploaded Arduino first and then ran Processing second, it likely means that whatever program was last using the serial port did not properly close the port, locking you out. To fix this, you’ll have to reboot your computer. (Or use a program like Terminal or PuTTY, but rebooting is probably simpler if you’re not already familiar with those programs.) Also, be aware that if you’re using serial communication somewhere else (e.g., some other device plugged into your computer), that other device can occupy your serial port.

When you’re done, spend a little time reflecting on how you could incorporate sensor data into your sketch, in lieu of “Hello, world!”

3. Submit a video of your final project, when both sides are “shaking hands” (14 points).

4. In your Arduino code you saw a while loop. What is a while loop and how does it differ from a for loop? Thinking back on past sketches, when did you use a for loop? (2 points)

5. In the final code, when you click the mouse, the light turns on. When you click again, the light turns off. How does this differ from your first mouse-clicking/light-blinking sketch? Where do you see this difference manifest in the code? (4 points)

6. Please submit your video and answers to the questions on bSpace under Lab 4.

2 thoughts on “Lab 4: Communicating”

  1. Hi hackers! My “handshakes” seems to lag a bit – I have to click the mouse a few times to turn on the LED and to turn it off again. My baud rates match, and as far as I can tell, the code is sound. Anyone else run into this bug?

    1. Mine did, too. I’m not totally sure why…I spent some time tracing the delays around the code, but that doesn’t seem to explain things. Anyone else have ideas?

Leave a Reply

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