Recitation 8: Serial Communication by Sheldon Chen

In the first exercise, I had to edit on both the processing and Arduino file. In the Arduino file, I had to use analog read to receive values from potentiometer, before mapping the value into the screen size in processing. Then, I need to use Serial.println to “print” their values. To let the values flow into processing correctly, I need to put Serial.println() at the end of the code. In the processing file, since the reading function was already completely, I only need to implement the function to read data from the Arduino, which is sent into processing in the form of arrays. However, at first, the circles simply couldn’t be drawn on the screen. It was later found by Young, that I didn’t put the number of values processing is receiving into the variable, NUM_OF_VALUES. Everything worked normally once I altered the number to two.

In the second exercise, I need to alter the code in processing, such that when “z”, “x”, “c”, “v”, “b”, “n”, “m” on the keyboard are pressed, the speaker would play tones ranging from C(4) to C(5). To achieve this, I need to set seven different conditions, as well as an array at the length of seven. The setup values of all positions in the array are 0. When any of the keys above is stroke, its corresponding value will be altered to 1. The processing would, in turn, receive the value and check through whether there are 1s in the array. If so, it will play the tone. Despite the logic seems pretty sound, tones couldn’t be played correctly. Only C, D, B were making sound. Later, Leon noticed the logic in the Arduino file is not very correct. Since number in the array has an if-else condition to check, noTone() function will be triggered once any of the key is not pressed. This would make it execute a play tone for once and seven no tone for six times. After placing all conditions as else if, it worked better than before. But it still doesn’t seem to function completely.

In this recitation, I’ve learned that it is very important to alter the number of values in the processing in accordance with your actual need if you are trying to send something from Arduino to processing. The noTone() function in Arduino is also something to take notice of. Since it would be executed all the time.

Schematic
Schematic

Leave a Reply