/* /* receive 2 sensors values via SoftwareSerial * based on Y. Tsuchiya, Fab Academy 2019 week16 "Interface and application programming" * http://fabacademy.org/2019/labs/kamakura/students/tsuchiya-yosuke/assignments/week16/ * ( port number etc. modified. ) */ import processing.serial.*; Serial port; boolean DEBUG = false; int[][] values = new int[2][100]; color[] colors = {color(0, 255, 255), color(255, 255, 0), color(255, 0, 255)}; void setup() { size(800, 600); frameRate(50); String[] ports = Serial.list(); if (DEBUG) { for (int i = 0; i < ports.length; i++) { println(i + ": " + ports[i]); } } else { println("Available serial ports:"); // if using Processing 2.1 or later, use Serial.printArray() printArray(Serial.list()); port = new Serial(this, ports[5], 9600); } } void draw() { background(color(30, 30, 30)); stroke(255, 255, 255); textSize(16); text("Input Device Value", 10, 20); for (int i=0; i<2; i++) { stroke(colors[i]); for (int j=0; j<99; j++) { float a = map(values[i][j], 0, 1023, 0, 512); float b = map(values[i][j + 1], 0, 1023, 0, 512); line(8 * j, 512-a, 8* (j + 1), 512-b); } } } void serialEvent(Serial p) { if (p.available() > 0) { try { String input = p.readStringUntil('\n'); if (input != null) { input = trim(input); String [] value = split(input, ','); println(value[0] +","+ value[1] ); for (int i = 0; i < 2; i++) { values[i] = append(subset(values[i], 1), int(value[i])); } } } catch (RuntimeException e) { } } }