12_interface and application programming
I used the input from modules 10 input devices to display serial data from Arduino on my computer screen using processing
1. Cut and paste the code below in a processing sketch
2. connect your arduino and check what is the serial port it connects too.
3. In the processing code look for this line
myPort = new Serial(this, “/dev/tty.usbmodemfd121″, 9600);
replace this “/dev/tty.usbmodemfd121″ with the name of your com port (osx) or its number (windows)
press run to visualize the code.
//PROCESSING CODE
// visualize a string of data coming from a serial port
// use arduino with a photocell to send data to serial port
import processing.serial.*;
Serial myPort; // The serial port
int xPos = 1; // horizontal position of the graph
int val = 0;
void setup () {
size(800 , 800); // the window size
//println(Serial.list()); // List all the available serial ports
myPort = new Serial(this, “/dev/tty.usbmodemfd121″, 9600);
myPort.bufferUntil(‘\n’); // don’t generate a serialEvent() unless you get a newline character:
background(51); // set inital background:
}
void draw () {
// everything happens in the serialEvent()
}
void serialEvent (Serial myPort) {
String inString = myPort.readStringUntil(‘\n’); // get the ASCII string:
if (inString != null) {
inString = trim(inString);
int inByte = int(inString);
int val = 255 – inByte;
background(val, 0, 0);
draw () ;
fill(0, 340, 340);
//ellipse(400, 400, inByte, inByte);
rectMode (CENTER);
rect(400, 400, inByte, inByte);
}
}