// -------------------------------------- // Filename : TemperatureGrapher.pde // Author : Sven Maerivoet // Last modified : 13/09/2015 // Target : C++ (Arduino UNO) // // All rights reserved. // -------------------------------------- /* Copyright 2015 Sven Maerivoet Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ import processing.serial.*; final int kMaxTemp = 35; Serial myPort; int xPos = 1; int lastxPos = 1; int lastheight = 0; // ---------------------- // Initialise the program // ---------------------- void setup() { fullScreen(); frameRate(100); // check the listed serial ports in your machine // and use the correct index number in Serial.list()[] myPort = new Serial(this,Serial.list()[0],9600); background(0); clear(); } // ---------------- // Prepare the grid // ---------------- void clear() { PFont f = createFont("Arial",16,true); textFont(f,12); fill(255); // fine degree lines stroke(128,0,0); strokeWeight(1); for (int degrees = 0; degrees <= kMaxTemp; degrees += 1) { int y = (int) map(degrees,0,kMaxTemp,0,height); line(0,y,width,y); int textY = y - 5; if (degrees == 0) { textY = y + 5; } text(String.valueOf(kMaxTemp - degrees) + " °C",10,textY); } // thicker degree lines stroke(255,0,0); strokeWeight(2); for (int degrees = 0; degrees <= kMaxTemp; degrees += 10) { int y = (int) map(degrees,0,kMaxTemp,0,height); line(0,y,width,y); } } // -------------------------------------------- // Continuously drawing the graph on the screen // -------------------------------------------- void draw() { if (myPort != null) { if (myPort.available() > 0) { strokeWeight(1); String tempS = myPort.readStringUntil('\n'); if (tempS != null) { tempS = trim(tempS); float temp = float(tempS); temp = (int) map(temp, 0, kMaxTemp, 0, height); //map to the screen height. strokeWeight(4); line(lastxPos, lastheight, xPos, height - temp); lastxPos= xPos; lastheight = height- (int) temp; // at the edge of the window, go back to the beginning: if (xPos >= width) { xPos = 0; lastxPos= 0; stroke(random(255),random(255),random(255)); } else { xPos++; } } } } }