Skip to content

PCom: Serial Input in Processing

I do enjoy the force sensor I got in week 5′s application class so I’ll definitely use it more in the assignment!

First 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 parsed the serial input in the draw loop, and I didn’t use BYTE output in the arduino board so I need to parse it as string. It worked pretty well but the revised the version is much better.

Codes:

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
import processing.serial.*;

Serial myPort;
int window = 800;
int[] values = new int[window - 100];
int index = 0;
int input = 0;

void setup()
{
  size(window,window);
  background(0,30);
  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()
{
  background(0,20);
  for (int i = 0; i < window - 100;i++)
  {
    int j = i + index;
    if (j >= (window - 100))
    {
      j = j - (window - 100);
    }

    stroke(255,0,0,80);
    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));
  }
}

void serialEvent(Serial p) {
  input = p.read();
  println("input:" + input);
  values[index] = input * 4;
  index++;
  if (index >= window - 100)
  {
    index = index - (window - 100);
  }
}

Then I thought about the nature of the force sensor and really wished I could visualize the “force” in the processing. So I adopted the “string” example bundled in the processing application and integrated it with the serial input.

Codes:

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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import processing.serial.*;
// The serial port:
Serial myPort;
int input = 0;
int lastInput = -1;

// Spring drawing constants for top bar
int s_height = 16;     // Height
int left = 150;         // Left position
int right = 250;       // Right position
int max = 300;         // Maximum Y value
int min = 20;          // Minimum Y value
boolean over = true;  // If mouse over
boolean move = false;  // If mouse down and over

// Spring simulation constants
float M = 0.8;   // Mass
float K = 0.2;   // Spring constant
float D = 0.92;  // Damping
float R = 60;    // Rest position

// Spring simulation variables
float ps = 60.0; // Position
float vs = 0.0;  // Velocity
float as = 0;    // Acceleration
float f = 0;     // Force


void setup()
{
  size(400, 400);
  rectMode(CORNERS);
  noStroke();
  myPort = new Serial(this, Serial.list()[0], 9600);
}

void draw()
{
  background(102);
  updateSpring();
  drawSpring();
}

void drawSpring()
{
  // Draw base
  fill(0.2);
  float b_width = 0.5 * ps + -8;
  rect(width/2 - b_width, ps + s_height, width/2 + b_width, 350);
}


void updateSpring()
{
  // Update the spring position
  if(!move) {
    f = -K * (ps - R);    // f=-ky
    as = f / M;           // Set the acceleration, f=ma == a=f/m
    vs = D * (vs + as);   // Set the velocity
    ps = ps + vs;         // Updated position
  }
  if(abs(vs) < 0.1) {
    vs = 0.0;
  }

  // Test if mouse is over the top bar
  /*
  if(mouseX > left && mouseX < right && mouseY > ps && mouseY < ps + s_height) {
    over = true;
  } else {
    over = false;
  }
  */

 
  // Set and constrain the position of top bar
  if(move) {
    ps = map(input,0,255,min,max) - s_height/2;
    if (ps < min) { ps = min; }
    if (ps > max) { ps = max; }
  }
}

void serialEvent(Serial p) {
  input = p.read();
  println("input:" + input);
 
  if (input > lastInput)
  {
    move = true;
  }
  else if (lastInput > input + 10 || input < 10)
  {
    move = false;
  }
 
  lastInput = input;
}

Post a Comment

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