View difference between Paste ID: ZLhcUJWJ and 2zkJMNiU
SHOW: | | - or go back to the newest paste.
1-
import processing.serial.*;
1+
import processing.serial.*;
2-
2+
3-
Serial myPort;
3+
Serial myPort;
4-
4+
5-
int windowWidth = 1280;
5+
int windowWidth = 1280;
6-
int windowHeight = 700;
6+
int windowHeight = 700;
7-
7+
8-
int upperValue = 700;
8+
int upperValue = 700;
9-
9+
10-
int[] valueArray = new int[windowWidth];
10+
int[] valueArray = new int[windowWidth];
11-
11+
12-
void setup() {
12+
void setup() {
13-
  size(1280, 700);
13+
  size(1280, 700);
14-
  println(Serial.list());
14+
  println(Serial.list());
15-
15+
16-
  myPort = new Serial(this, Serial.list()[2], 9600);
16+
  myPort = new Serial(this, Serial.list()[2], 9600);
17-
  myPort.bufferUntil('\n');
17+
  myPort.bufferUntil('\n');
18
}
19-
19+
20-
void draw() {
20+
void draw() {
21-
  background(0);
21+
  background(0);
22-
  if (valueArray != null) {
22+
  if (valueArray != null) {
23-
    for (int i=0; i<valueArray.length; i++) {
23+
    for (int i=0; i<valueArray.length; i++) {
24-
      stroke(127, 34, 255);
24+
      stroke(127, 34, 255);
25-
      line(i, height, i, height - valueArray[i]);
25+
      line(i, height, i, height - valueArray[i]);
26-
    }
26+
    }
27-
  }
27+
  }
28-
  
28+
  
29-
  textSize(10);
29+
  textSize(10);
30-
  for (int i=0; i<height; i++) {
30+
  for (int i=0; i<height; i++) {
31-
    if (i % (50) == 0) {
31+
    if (i % (50) == 0) {
32-
      text(int((map(i, 0, height, upperValue, 0))) + " W", 10, i);
32+
      text(int((map(i, 0, height, upperValue, 0))) + " W", 10, i);
33-
      stroke(60);
33+
      stroke(60);
34-
      line(0, i, width, i);
34+
      line(0, i, width, i);
35-
    }
35+
    }
36-
  }
36+
  }
37
}
38-
38+
39-
void serialEvent(Serial myPort) {
39+
void serialEvent(Serial myPort) {
40-
  String inString = myPort.readStringUntil('\n');
40+
  String inString = myPort.readStringUntil('\n');
41-
  println(inString);
41+
  println(inString);
42-
  if (inString != null) {
42+
  if (inString != null) {
43-
    if (inString.startsWith("W ")) {
43+
    if (inString.startsWith("W ")) {
44-
      println("Watt value!");
44+
      println("Watt value!");
45-
      String val = inString.substring(2);
45+
      String val = inString.substring(2);
46-
      val = trim(val);
46+
      val = trim(val);
47-
47+
48-
      float inByte = float(val); 
48+
      float inByte = float(val); 
49-
      inByte = map(inByte, 0, upperValue, 0, height);
49+
      inByte = map(inByte, 0, upperValue, 0, height);
50-
      
50+
      
51-
      // Add last value to array
51+
      // Add last value to array
52-
      shiftAndAdd(valueArray, int(inByte));
52+
      shiftAndAdd(valueArray, int(inByte));
53-
    }
53+
    }
54-
  }
54+
  }
55
}
56-
56+
57-
void shiftAndAdd(int a[], int val) {
57+
void shiftAndAdd(int a[], int val) {
58-
  int a_length = a.length;
58+
  int a_length = a.length;
59-
  System.arraycopy(a, 1, a, 0, a_length-1);
59+
  System.arraycopy(a, 1, a, 0, a_length-1);
60-
  a[a_length-1] = val;
60+
  a[a_length-1] = val;
61
}