I wanted to use my ADCInputBoard from week 11 to read the magnatic field from the hall sensor I have attached to the board and to produce a visual output.
For this assigment I used Processsing.
As a basis I used this simple Graph example, by David A. Mellis, Tom Igoe and Scott Fitzgerald
However, I did modifications to make it work and to improve. First, I had to change the baud rate to 4800.
I also wanted to visualize not just a value read from the serial input, I also wanted to distingush between the orientation of the magnet (north and south pole). Thus, I added some conditions and changed the output graph.
/* Processing example for serial communication and visual output // This program takes ASCII-encoded strings // from the serial port at 4800 baud and graphs them. It expects values in the // range 0 to 1023, followed by a newline, or newline and carriage return // Original by David A. Mellis, modified by Tom Igoe and Scott Fitzgerald // and finally modified by Karsten Nebe for FabAcademy 2016 // This example code is in the public domain. */ import processing.serial.*; Serial myPort; // The serial port int xPos = 1; // horizontal position of the graph float inByte = 0; void setup () { // set the window size: size(800, 400); // List all the available serial ports // if using Processing 2.1 or later, use Serial.printArray() println(Serial.list()); // I know that the first port in the serial list on my mac // is always my Arduino, so I open Serial.list()[0]. // Open whatever port is the one you're using. myPort = new Serial(this, Serial.list()[1], 4800); // don't generate a serialEvent() unless you get a newline character: myPort.bufferUntil('\n'); // set inital background: background(0); } void draw () { // draw the line (in differnt colors for N/S): if ( inByte > height/2 ){ stroke(127, 34, 255); // blue line(xPos, height/2 , xPos, height - inByte); } else{ stroke(255, 0, 0); // red line(xPos, height - inByte, xPos, height/2 ); } // at the edge of the screen, go back to the beginning: if (xPos >= width) { xPos = 0; background(0); } else { // increment the horizontal position: xPos++; } } void serialEvent (Serial myPort) { // get the ASCII string: String inString = myPort.readStringUntil('\n'); if (inString != null) { // trim off any whitespace: inString = trim(inString); // convert to an int and map to the screen height: inByte = float(inString); inByte = map(inByte, 0 , 1023, 0, height); println(inByte); } }
Using serial communication in processing is surprisingly easy - it is almost similar to the way you do it in the Arduino environment (using standard libraries).