[p-com wk6] Serial communication

Finally, serial communication! This week’s labs are a lot to digest, but it’s very exciting to learn about how Arduino can communicate with other devices.

Going into the labs, there were several key concepts I learned through class and homework materials, but still have some questions about.

  • ASCII encoding: Raw binary value (10 bits?), ASCII-encoded binary value (8 bits in binary), and decimal value
    • Example: 0011 0001 in decimal is 49, and its ASCII character is 1
    • What are the extra 2 bits before ASCII encoding?
  • A 4-digit number in the Serial Monitor has 6 bytes: 4 numbers, return (\r), and new line (\n)
    • Does it just lose 2 bytes when it’s only 2 digits?
  • Difference between Serial.println() and Serial.write()
    • Serial.println() : formats the bytes as ASCII characters, gives out a decimal number
    • Serial.write() : gives out a raw binary value; 0 – 1023 (10 bits) when it’s an analog output, which is more than a byte (8 bits). Needs to be mapped into 0 – 255.
    • But why does Serial.write() return garbage characters in serial monitor? When do I use Serial.write() ?

Lab 1: Intro to Asynchronous Serial Communications

I have a lot of questions regarding accelerometers. For now, I used potentiometers as my analog inputs, and a push button for digital input.

Using the serial monitor, I was able to map the raw binary reading from the potentiometers into 8 bits, and print out different formats. I still don’t quite understand what Hex and Octal values are, but we don’t seem to use them often?

The punctuation method is very helpful in separating different input values, and later on would benefit putting these input into an array in p5. I also finally understood that Serial.println() returns a new line, and Serial.print() doesn’t.

Code looks like this:

const int switchPin = 2;
void setup() {
   Serial.begin(9600);
   pinMode(switchPin, INPUT);
}
 
void loop() {
   for (int thisSensor = 0; thisSensor < 3; thisSensor++) {
   // sensor 1
   int sensorValue = analogRead(A0);
   Serial.print(sensorValue);
   Serial.print(",");

   // sensor 2
   sensorValue = analogRead(A1);
   Serial.print(sensorValue);
   Serial.print(",");
 
   // switch, new line
   int sensorValue = digitalRead(switchPin);
   Serial.println(sensorValue);
   }
}

It then printed out results in this order: analog1, analog2, switch

I also tried connecting Arduino to TouchDesigner just for fun. The import was successful but I didn’t know how to interpret those data. More things to be discovered in the future.

For now, that’s it for this lab. After finishing the p5 input lab, I also referred to the Two-Way (Duplex) Serial Communication lab attempting to use these inputs to control a p5 sketch, as well as trying out other sensors. I will come back to it later.

Lab 2: Serial Input to P5.js

This lab is pretty straightforward: connect Arduino via a serial control app to p5, include a serialport.js file in the html, and then… code. I’ve never wrote a callback function in p5 before, so that was pretty cool to learn.

A callback function is a function that is passed as an argument to a second function, and that runs after the second function has completed.

For example, serial.on('data', serialEvent); is in setup, but will be “called back” later in the code when function(serialEvent) runs.

In this case, I would put in setup callbacks for open, close, error, an event that opens the correct port, as well as the most important serialEvent in which I would later include incoming data.

Attaching video evidence of this experiment. My p5 file can be found here.

Discovery: Analog input data is very noisy. When the potentiometer stays still or untouched the numbers jump up and down (though not by much) quite aggressively. By adding a delay in Arduino it gets less noisy, but more jagged. How can I smooth it out?

Also, when the serial control app is open I can’t seem to upload Arduino code, and p5 gets significantly slow. I try not to let it run in background at all.

Oh well! I didn’t know the answers yet so I just moved on. I wanted to utilize the 2 analog, 1 digital input setup from the first lab, so I referred back to the first p5 sketch I made the second week in ICM, hoping to control it now with Arduino.


cute lil egg man

The only things I needed to do was:

  • Changing the parameters of my circle position variables, map them from sensor readings to width or height position range, put in serialEvent
  • Changing all the mouse click functions and conditions into button presses (button reading == 1)

The game play is a tad wonky due to input noise, and also a tad slow in response time. P5 code can be found here.

Leave a Reply

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