[p-com wk7] Duplex serial, project 2 proposal

Last week, I started using Arduino to control an old p5 sketch, and have received minor success. After testing out with potentiometers, I moved on to a joystick.

Two problems occurred: the pushbutton’s data jumped back and forth when not being pressed; x and y axises worked but had noisy data.

A productive office hour solved both. For the noise, Tom suggested reducing 2 bits of unnecessary data, dividing each analogRead by 4. When the number is smaller, noise also become much less significant. The pushbutton needed a INPUT_PULLUP in pinMode , for its lack of resistor connection made its idle state hard to detect. We also added a small delay in between two analogRead in order to stabilize both inputs.

Final code, including flow control:

const int joyX = A0;
const int joyY = A1;
const int switchPin = 2;
 
void setup() {
  Serial.begin(9600);
  pinMode(switchPin, INPUT_PULLUP);
// flow control
  while (Serial.available() <= 0) {
    Serial.println("hello"); // send a starting message
    delay(300);              // wait 1/3 second
  }
}
 
void loop() {
  if (Serial.available() > 0) {
    int  xValue = analogRead(joyX)/4;
    delay(1);
    int  yValue = analogRead(joyY)/4;
    int switchValue = digitalRead(switchPin);
 
    Serial.print(xValue);
    Serial.print(",");
    Serial.print(yValue);
    Serial.print(",");
    Serial.println(switchValue);
  }
}

The Arduino code worked! However, when using the p5 code from last week, the control was not quite right.

I didn’t do a good job demonstrating, but it’s noticeable that whenever the control became idle, the ball returned to the same spot. What I wanted was that ball position should follow control when triggered, and should be able to stay at last position.

In my original code, I mapped the ball’s position directly from input data.

circleX = map(sensors[0], 0, 255, 0, width);
circleY = map(sensors[1], 0, 255, 0, height);

This should be replaced by loops where the variables add or subtract in response to data input. I also found out, from play testing, that people only like to push the joystick to its four extreme positions. In this case, it’s completely unnecessary to follow all the intricate changes in both axises.

If else statements work well here: if joystick position is near the far right end, ball moves right 1px; if joystick position is near the far left end, ball moves left 1px. Up and down etc. Considering it’s unpleasant when ball goes out of the constraint of the canvas, I added and gates to each if statement to define the ends of ball movements.

  let inString = serial.readStringUntil('\n');
  if (inString.length > 0 ) {
    if (inString !== 'hello') {
      let sensors = split(inString, ','); 
      if (sensors.length > 2) {
        if (sensors[0] > 250 && circleX < width) {
          circleX += 1;
        } else if (sensors[0] < 5 && circleX > 0) {
          circleX -= 1;
        }
        if (sensors[1] > 250 && circleY < height) {
          circleY += 1;
        } else if (sensors[1] < 5 && circleY > 0) {
          circleY -= 1;
        }
        inData = sensors[2];
        console.log(circleX,circleY,inData);
      }

Project 2 proposal

There have been a couple of different ideas I explored over the past weeks, but one thought captured me. During a recent trip into the mountains, I started noticing things in nature that have complex systems and vaguely resemble circuitry: moss, tree, fire, millipedes… This could be backwards thinking, as I already knew that programmers like to simulate natural systems in code (hi Shiffman), but as someone new to both programming and hardware, it was still an exciting discovery to me.

For this project, I would like to reinvent simple scientific phenomenon causality, creating a physical interaction that would trigger a simulation of the physical effect digitally. For example, physics says that when you hold a block of ice, it will slowly start to melt — while the holding of something (that resembles ice) happens tangibly, what if the melting occur through a digital medium?

Relationship: enclosure in Resin and a ecosphere, hardware that looks like nature and encased “nature” that looks like circuitry.

A question I’d like to tackle is how directly translatable the physical interaction would be to the media. When people see a translucent sphere / cube, what would be their first instinct? Would they know to hold and shake if no explicit language is used to instruct? Would they know to hold it for a little while for something to happen?

I’m not exactly sure how to conduct interview play tests at this stage of creation, and I’d seek some feedback in class and during office hour this week.

Leave a Reply

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