Skip to content

Physical Computing Week 2: Analog!

Hooking up the following stuff according to the diagram was easy and quick. And a resister of 220 10K Om is working ideally to create an ouput range of 0 – 930 (from total darkness to direct shine of torchlight) for the photocell.

I changed the switch from last week to a much smaller one which could be inserted right into the breadboard, to get rid of the mess of the hookup wires. The output was quite straightforward in the arduino console window, so I decided to connect the output to the processing.

This is how it works:

Physical Computing Week 2 from Leejay Xia on Vimeo.

(Quick and Dirty) Code for Processing, it’s based on the example on their site

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import processing.serial.*;

// The serial port:
Serial myPort;
int window = 400;
int[] values = new int[window - 100];
int index = 0;

void setup()
{
  size(window,window);
  background(0);
  smooth();
  // List all the available serial ports:
  println(Serial.list());
  myPort = new Serial(this, Serial.list()[0], 9600);
  for (int i = 0;i<window - 100;i++)
  {
    values[i] = 0;
  }
}

void draw()
{
  if (myPort.available() > 0)
  {
    String inBuffer = myPort.readString();
    String[] vals = splitTokens(inBuffer);

    for (int i = 0;i<vals.length;i++)
    {
      values[index] = int(vals[i]);
      //println(index+":"+values[index]+"/"+vals[i]);
      index++;
      if (index >= window - 100)
      {
        index = index - (window - 100);
      }
    }
    background(0);
    for (int i = 0; i < window - 100;i++)
    {
      int j = i + index;
      if (j >= (window - 100))
      {
        j = j - (window - 100);
      }
      stroke(255,0,0,90);
      line(50 + i, (window - 50), 50 + i, (window - 50) - (values[j] * (window - 100) / 1024));
      stroke(255,0,0,100);      
      point(50 + i, (window - 50));
      point(50 + i, (window - 50) - (values[j] * (window - 100) / 1024));
    }
  }
}

Notes-taking:

  • I tried also the Thermistors but have not yet find a right resister to tune its output range.
  • In the current codes, the arduino board must be plugged before the processing applet runs.
    This is because the processing init code cannot find a usable serial port to initialize the object. And so far I didn’t see a possible way to initialize it during the draw() loop due to the lack of a try/catch machanism. More exploration to go!
  • The serial port transmits data much faster than the draw() function loops. Thus everytime we arrive in the draw() loop there might be a bunch of values waited in the buffer. These values need to be splited and stored accordingly. Of course a more complex protocal could be created to sample specific data or behavior.

More Photos:

One Trackback/Pingback

  1. [...] I revisited the processing codes I wrote for week 2, which now seems to be a total mess. I didn’t know about the serialEvent callback then so I [...]

Post a Comment

Your email is never published nor shared. Required fields are marked *
*
*