Recitation 8. Serial Communication by Frances (Fan Yuan)

Instructor: Marcela

Exercise 1: Making a Processing Etch A Sketch

Steps:

  1. Build a circuit with two potentiometers and Arduino
  2. Use Arduino to read the values from potentiometers and send them serially
  3. Use Processing to draw an ellipse whose x and y values are based on the input from Arduino.

We were already experienced in building circuits and drawing ellipses, so working separately on Arduino and Processing was not hard. The more difficult part was to connect them together. We were provided example codes to start with. First, I modified the number of sensors and values to let it match the circuit I built. Then I followed the instructions in the code and changed the PORT_INDEX into the port index of my laptop. Then, after uploading the code to Arduino, I can now see the number that is sent to Processing. The numbers were stored in an array called sensorValues, so I put the corresponding values at the positions of x and y in ellipse function and it worked.

However, the ellipse sometimes disappeared from the canvas, because the range of the values sent from Arduino was 0 ~ 1023, but the canvas size was 500. To solve that problem, I used map function to change its range to 0 ~ 500.

Exercise 2: Make a musical instrument with Arduino

Steps:

  1. Use Processing to send values to Arduino based on mouse’s x and y positions and other keyboard/mouse interactions
  2. Build a circuit with Arduino and a buzzer
  3. Use Arduino to read the serial values from Processing and translate them into frequency and duration for a tone, which will be sounded by the buzzer 

For this exercise, I decided to set the frequency based on the x position of the mouse. I first changed the code in Processing to send the value of mouseX to Arduino and connect the circuit with buzzer and set the tone function’s second parameter to the value read from Processing. When testing, the buzzer did make sound accordingly, but it won’t stop, only if I disconnect it from the power. So I set the buzzer to turn on and off by clicking the mouse. I send value 1 to Arduino if mousePressed is true and 0 if it is false, and turn the buzzer on and off according to that value. I also tried to use the keyboard to control the buzzer, but I found moving the mouse and clicking it a better combination. To optimize user experiences, I also added an ellipse to make the position of the mouse more straightforward.

Processing:

Arduino:

Leave a Reply