Here's my questions: Call-and-Response is always better than punctuation, right?
Punctuation or Call-and-Response
During the labs, I made some mistakes. They seem very easy to fix, but also very easy to make.
1. When use CoolTerm, make sure choose the right port: USB, otherwise CoolTerm can't receive the data. There's also bluetooth port in the "serial port" folder.
2. The note says the port number 12 means “/dev/tty.usbmodem1421″number. I didn't understand why. Later I figured out it's just a sequence of ports in one computer ranging from 0 - 12. Every computer have a different port list.
3. Connect the components correctly. I was careless to put the potentiometer power leg to ground, and the potentiometer control the shape in processing in a wired way.
I kept some notes about some knowledge mentioned in the labs.
1. what's serialEvent?
The serial library has a special method called serialEvent(). Every time a new byte arrives in the serial port, serialEvent() is called. That way we needn't call serialEvent function in draw() or in setup().
2. What's the difference between Serial.println and Serial.write?
For example, imagine that analogValue = 32:
- Serial.println(analogValue) results in “32” with a linefeed and carriage return
- Serial.write(analogValue) results in ” “, the space character, which has the ASCII value 32.
3. How many bytes does Serial.println(analogValue) send when analogValue = 32?
Serial.println(analogValue) actually sends FOUR bytes! It sent a byte to represent the 3, a byte to represent the 2, a byte to tell the Monitor to move the cursor down a line (newline), and a byte to move the cursor all the way to the left (carriage return). The raw binary values of those four bytes are 51 (ASCII for “3”), 50 (ASCII for “2”), 10 (ASCII for “newline”), and 13 (ASCII for “carriage return”).
4. What's trim() mean?
There’s a command that removed whitespace from a string, called trim().
The first trims the whitespace characters off. The second splits the string into three separate strings at the commas, the converts those strings to integers:
This line tells the sketch not to call serialEvent() unless an ASCII newline byte (value 10) comes in the serial port. It will save any other bytes it gets in the serial buffer, so you can read them all at once when the newline arrives.
CoolTerm screenshot showing incoming bytes as hexadecimal values
Here's code in Arduino, receiving potentiometer value from the circuit.
Here's code in processing. Imported processing.serial library. Found the right port in the Serial.list. Set up a object named myPort in the Serial class. In the serialEvent() function, used myPort.read to get the data, and then draw a line corresponding to the incoming data.
This is my video:
This is the code in Arduino, receiving the data from A0, A1,D2 -- acceleromete's X, Y coordinates and a switch state.
This is the code in processing. Save the serial buffer in mystring, use trim to delete space, use split to get sensor, sensor, sensor, and draw ellipse depending on these three numbers.
This is my video: