Real Time Data Plotting

Similar documents
Introduction to Processing

Transform 1: Translate, Matrices

EXAMINATIONS 2016 TRIMESTER 2

EXAMINATIONS 2017 TRIMESTER 2

CS110 Introduction to Computing Fall 2016 Practice Exam 1 -- Solutions

Solution Notes. COMP 151: Terms Test

Sten-SLATE ESP. Graphical Interface

Review Cellular Automata The Game of Life. 2D arrays, 3D arrays. Review Array Problems Challenge

Iteration in Programming

Question 1 (10 points) Write the correct answer in each of the following: a) Write a Processing command to create a canvas of 400x300 pixels:

EGG 101L INTRODUCTION TO ENGINEERING EXPERIENCE

To specify the dimensions of the drawing canvas use the size statement: ! size( 300, 400 );

SERIAL COMMUNICATION. _creates a data stream by sending one bit at a me _occurs sequen ally H...E...L...L...O

EGG 101L INTRODUCTION TO ENGINEERING EXPERIENCE

We will start our journey into Processing with creating static images using commands available in Processing:

Graphics. HCID 520 User Interface Software & Technology

Graphics. HCID 520 User Interface Software & Technology

Processing Transformations. Affine Transformations

Arduino. AS220 Workshop. Part III Multimedia Applications Lutz Hamel

Old 257 Exam #2s for Practice

mith College Computer Science CSC103 How Computers Work Week 7 Fall 2017 Dominique Thiébaut

CISC 1600 Lecture 3.1 Introduction to Processing

Design Programming DECO2011

CSE120 Wi18 Final Review

Building a GUI From Scratch

Interaction Design A.A. 2017/2018

CISC 1600, Lab 3.1: Processing

Using Methods. Methods that handle events. Mairead Meagher Dr. Siobhán Drohan. Produced by: Department of Computing and Mathematics

CS110 Introduction to Computing Fall 2016 Practice Exam 1

Lecture 7. Processing Development Environment (or PDE)

2D Shapes. Creative Coding & Generative Art in Processing 2 Ira Greenberg, Dianna Xu, Deepak Kumar

CISC 1600, Lab 2.1: Processing

+ Inheritance. Sometimes we need to create new more specialized types that are similar to types we have already created.

Using Methods. More on writing methods. Dr. Siobhán Drohan Mairead Meagher. Produced by: Department of Computing and Mathematics

GRAPHICS PROGRAMMING. LAB #3 Starting a Simple Vector Animation

import processing.serial.*; //import the Serial library import controlp5.*; Serial myport; //the Serial port object

This Week Assignment 1 Arrays Time Casting Reading code Project 1

CISC 1600, Lab 2.3: Processing animation, objects, and arrays

Interaction Design A.A. 2017/2018

Chapter 2 Surfer Tutorial

DEC HEX ACTION EXTRA DESCRIPTION

CISC 1600, Lab 2.2: Interactivity in Processing

Graphics: Legacy Library

Connecting Arduino to Processing a

Basic Computer Programming (Processing)

Computer Graphics with OpenGL ES (J. Han) Chapter XI Normal Mapping

Introduction to Processing. Sally Kong

PROJECT FINAL: WEARABLE

CS 105: Introduction to Computer Programming (using JavaScript) Quiz: Khan Academy: Intro to While Loop

1 Getting started with Processing

Guided Problem Solving

Topics for section today. Homework 10 functions for loops and loading fonts

SFPL Reference Manual

Connecting Arduino to Processing

Getting Started in Java CIS 110

CS12020 for CGVG. Practical 1. Jim Finnis

Class #1. introduction, functions, variables, conditionals

[ the academy_of_code] Senior Beginners

Loops. Variable Scope Remapping Nested Loops. Donald Judd. CS Loops 1. CS Loops 2

CSC 120 Introduction to Creative Graphical Coding, Fall 2015

Mirage. Language Reference Manual. Image drawn using Mirage 1.1. Columbia University COMS W4115 Programming Languages and Translators Fall 2006

COPYRIGHTED MATERIAL. Elements of the Language. C h a p t e r

ISL RGB Sensor Tutorial By: Sabrina Jones

void set_target(unsigned char servo, unsigned int pozitie) { //merge la pozitia respectiva

StenBOT Robot Kit. Stensat Group LLC, Copyright 2018

2IS45 Programming

CS 106 Winter 2016 Craig S. Kaplan. Module 08 Randomness and noise Topics

Chapter 8: Implementation- Clipping and Rasterization

Pick a number. Conditionals. Boolean Logic Relational Expressions Logical Operators Numerical Representation Binary. CS Conditionals 1

Creating Icons for Leopard Buttons

Variables One More (but not the last) Time with feeling

Recall that creating or declaring a variable can be done as follows:

GIMP WEB 2.0 ICONS. GIMP is all about IT (Images and Text) OPEN GIMP

(a) Assume that in a certain country, tax is payable at the following rates:

PieNum Language Reference Manual

CS1950U Setup Spring 2018

B Additional Recipes

Name CMPS 5J Final March 17, 2010 This is a closed notes, closed book exam. Each problem is worth 1 point unless indicated otherwise.

The NGT30 is an easy to use, 5v compatible propeller graphics device. It communicates simply with UART communication.

In this lecture we will briefly examine a few new controls, introduce the concept of scope, random numbers, and drawing simple graphics.

CISC 1600, Lab 3.2: Interactivity in Processing

Programming: You will have 6 files all need to be located in the dir. named PA4:

Basic Input and Output

L E S S O N 2 Background

mith College Computer Science CSC103 How Computers Work Week 6 Fall 2017 Dominique Thiébaut

A B C D CS105 03a Interaction

Introduction to version Instruction date

Computer Graphics. Making Pictures. Computer Graphics CSC470 1

CMPSCI 119 LAB #2 Greebles / Anime Eyes Professor William T. Verts

The Processing language. Arduino and Processing.

CREATING DESMOS ETOOLS

Drawing Primitives. OpenGL basics

1. Complete these exercises to practice creating user functions in small sketches.

1 Getting started with Processing

Marionette nodes - Vol. 1

University of Cincinnati. P5.JS: Getting Started. p5.js

Functions. Functions. nofill(); point(20, 30); float angle = map(i, 0, 10, -2, 2); parameters return values

Processing & Arduino in Tandem. Creating Your Own Digital Art Tools

Fixed Perimeter Rectangles Geometry Creating a Document

Grove - Thumb Joystick

Transcription:

Real Time Data Plotting

Introduction This lesson will show how to write a program plot data on a X-Y graph. On the Arduino, write a program to sample a sensor and print the voltage to the Serial interface. Send the sample once a second. The plotting program will read the data from the serial interface and plot it. This plotting program will plot the data in real time. It will display 400 samples along the X axis and then repeat over writing the previous samples.

The Plot The plot is going to be made in a rectangle. The top left corner of the rectangle will be positioned at 50,50 pixel location. The rectangle will be 400 by 400 pixels. X and Y labels will be added along with tick marks. Data will be plotted inside the rectangle.

First, two variables will be declared. Declaring them outside any function will make them global meaning they can be used in any function. Variable integer i will be an index variable. Variable data_plot will be an floating point array of 400 elements. int i; int[] data_plot = new int[400];

Next is the setup() function. The window is created with the size() function. All the elements in the data_plot array are initialized to zero. Variables always need to be initialized. Since the variable i was used, it has to be reset to zero. After the for() loop, the variable i has a value of 400. int i; float[] data_plot = new float[400]; void setup() size(500,500); for(i=0;i<400;i++) data_plot[i] = 0.0; i = 0;

Last item is to set the text size in the plot. This isn't required and can be changed depending on the desired look of the plot. int i; float[] data_plot = new float[400]; void setup() size(500,500); for(i=0;i<400;i++) data_plot[i] = 0.0; i = 0; textsize(14);

Two functions are created. The plot_data() function will generate the plot with the values in the data_plot variable array. The draw() function repeatedly calls the plot_data() function. Before it is called, the background() function is called. The background() function clears the screen so the plot can be redrawn with new data. If it is not called, the display will just have plots placed on top of plots. int i; float[] data_plot = new float[400]; void setup() size(500,500); for(i=0;i<400;i++) data_plot[i] = 0.0; i = 0; textsize(14); void draw() background(0); plot_data();

In the plot_data() function, the fill color is set to a dark gray which will set the color of the rectangle. The rect() function draws the rectangle starting at pixel 50,50 with a size of 400 by 400 pixels. Remember the starting position of the rectangle. The 50,50 coordinate will be the offset for drawing in the rectangle. int i; float[] data_plot = new float[400]; void setup() size(500,500); for(i=0;i<400;i++) data_plot[i] = 0.0; i = 0; textsize(14); void draw() background(0); plot_data();

The program can be run now. It will open a window and draw a rectangle on a black window that is dark grey. From now on, each step will add to the plot. The program can be run each time to see what was added. The setup() function and the draw() function will not be shown to save space. int i; float[] data_plot = new float[400]; void setup() size(500,500); for(i=0;i<400;i++) data_plot[i] = 0.0; i = 0; textsize(14); void draw() background(0); plot_data();

Import the Serial library. Create an instance of Serial called port. In the setup function, open the serial connection. Change the COM port number as needed. import processing.serial.*; Serial port; int i; float[] data_plot = new float[400]; void setup() port = new Serial(this, COM3,9600); size(500,500); for(i=0;i<400;i++) data_plot[i] = 0.0; i = 0; textsize(14); void draw() background(0); plot_data();

This section of code checks if the serial port had data in its buffer. If data is available, it is read into the String variable a. The String variable a is checked to make sure it is not empty. If it is not empty, the data is processed. First the variable a has the carriage return and linefeed characters removed. It is then converted to a floating point number and stored in the data_plot array at the element pointed to by i. i is incremented an checked to make sure it does not reach 400. When it does, it is reset to 0. //Previous code not shown for space void draw() background(0); if(port.available() > 0) String a = port.readstringuntil(10); if(a!= null) a = a.replaceall( (\\r \\n), ); data_plot[i] = float.parsefloat(a); i++; if(i == 400) i = 0; plot_data();

The two new functions inserted to modify how the rectangle is drawn. strokeweight() sets the line thickness. stroke() sets the color which is white. // only the plot function shown here strokeweight(4); stroke(255);

A label is added to the X axis. strokeweight(4); stroke(255); text( X Axis,250,480);

The next addition is adding the Y axis label. The label is rotated and moved into position. The translate() function moves the coordinate system. The rotate() function rotates the coordinate system. The text() function for the y Axis is then rendered. In order to not have everything else rotated and translated, the reverse rotation and translation is executed. strokeweight(4); stroke(255); line(50,250,400,250); text( X Axis,250,480); translate(20,250); rotate( PI/2); text("y axis",0,0); rotate(pi/2); translate( 20, 250);

Now, the data is plotted. First, the plot color is set to purple. The line thickness is set to 1. strokeweight(4); stroke(255); line(50,250,400,250); text( X Axis,250,480); translate(20,250); rotate( PI/2); text("y axis",0,0); rotate(pi/2); translate( 20, 250); stroke(200,0,200); strokeweight(1);

This section draws the data plot. First, the beginshape() function is called. This indicates the start of vector drawing. The parameter makes a line drawn between vector coordinates. The for() loop sequences through the data_plot array and creates a line to each coordinate specified in the vertex() function. When plotting the data is completed, the function endshape() is called. Setting Up the Plot strokeweight(4); stroke(255); line(50,250,400,250); text( X Axis,250,480); translate(20,250); rotate( PI/2); text("y axis",0,0); rotate(pi/2); translate( 20, 250); stroke(200,0,200); strokeweight(1); beginshape(lines); for(int b = 0;b < 400;b++) int y =map(data_plot[b],0,1023,400,0); vertex(b+50,y); endshape();

The map() function is used to scale the data to fit within the rectangle area that is being used as the plot. The height of the rectangle is 400 pixels. The data, if it is analog data has a range of 0 to 1023. The map function will scale the analog data to the range of 400 to 0. Notice the 400 is before 0. This inverts the data plot since the screen coordinates start at the top left corner. strokeweight(4); stroke(255); line(50,250,400,250); text( X Axis,250,480); translate(20,250); rotate( PI/2); text("y axis",0,0); rotate(pi/2); translate( 20, 250); stroke(200,0,200); strokeweight(1); beginshape(lines); for(int b = 0;b < 400;b++) int y =map(data_plot[b],0,1023,400,0); vertex(b+50,y+50); endshape();

Notice in the vertex() function that 50 is added to the x coordinate. This is because the rectangle at the 50 x coordinate. 50 is also added to the Y coordinate for the same reason. You have to take in into account the position of the rectangle. Run the program and have the arduino processor board send data from the analog port with a sensor connected. Use the serial.println() function to send the data. strokeweight(4); stroke(255); line(50,250,400,250); text( X Axis,250,480); translate(20,250); rotate( PI/2); text("y axis",0,0); rotate(pi/2); translate( 20, 250); stroke(200,0,200); strokeweight(1); beginshape(lines); for(int b = 0;b < 400;b++) int y =map(data_plot[b],0,1023,400,0); vertex(b+50,250 data_plot[b]); endshape();

Notice in the vertex() function that 50 is added to the x coordinate. This is because the rectangle at the 50 x coordinate. 50 is also added to the Y coordinate for the same reason. You have to take in into account the position of the rectangle. Run the program and have the arduino processor board send data from the analog port with a sensor connected. Use the serial.println() function to send the data. strokeweight(4); stroke(255); line(50,250,400,250); text( X Axis,250,480); translate(20,250); rotate( PI/2); text("y axis",0,0); rotate(pi/2); translate( 20, 250); stroke(200,0,200); strokeweight(1); beginshape(lines); for(int b = 0;b < 400;b++) int y =map(data_plot[b],0,1023,400,0); vertex(b+50,250 data_plot[b]); endshape();

It is time to add more features. The for() loop inserted adds tick marks along both axis on the plot. The tick marks are separated by 50 pixels and are 5 pixels long. The X axis tick marks are labelled with the text statement. strokeweight(4); stroke(255); line(50,250,400,250); text( X Axis,250,480); translate(20,250); rotate( PI/2); text("y axis",0,0); rotate(pi/2); translate( 20, 250); for(int x=0;x<400;x = x + 50) text(x,x+50,465); line(x+50,445,x+50,450); line(50,x+50,55,x+50); stroke(200,0,200); strokeweight(1); beginshape(lines); for(int b = 0;b < 400;b++) int y =map(data_plot[b],0,1023,400,0); vertex(b+51,data_plot[b] + 251); endshape();

This lesson showed the basics of creating a plot and filling it with data. Done More can be done with the plot. The tick marks can be labelled with values. The tick marks can be extended to create a grid. The window can be made larger and a second plot added to plot more data.