PComp Lab: Serial Out

Serial Output

The purpose of this week’s lab is to graph the data received by an analog sensor, rather than just an output on the microcontroller. I chose a photoresister (http://www.radioshack.com/product/index.jsp?productId=2062590 Cadmium Sulfide photo cell).
Setup:

Light vs Darkness

Results:

Photoresistor readings while moving hand over photo cell

 

[expand title=”More”]
Here is the functional code for the displayed graph:

import processing.serial.*; //grab the serial library

Serial myPort; //create a serial port object
float xPos = 0;

void setup(){
//list serial ports
println(Serial.list());

String portName = Serial.list()[0]; //take the first item in the serial array list called by myPort
myPort = new Serial(this, portName, 9600); //communicate that info on the serial monitor at 9600 baud

size(600, 600);
background(0, 100, 120);
}

void draw(){
}

void serialEvent (Serial myPort){

// print the info coming in through chosen serial port
int inString = myPort.read();
//optional: remap to a wider range if incoming data is a small range
//map(inString, 0, 138, 0, 1000); for instance

println(inString);

float yPos = height – inString; //yPos is the height of the graphed lines

stroke(255, 10, 0);
line(xPos, height, xPos, yPos);

// at the edge of the screen, go back to the beginning:
if (xPos >= width) {
xPos = 0;
// clear the screen by resetting the background:
background(0, 100, 120);
}
else {
// increment the horizontal position for the next reading:
xPos++;
}
}

Here is the non-functional code. Can you tell the difference? It took me awhile to debug.

//see the Graph tutorial on arduino for this code

import processing.serial.*; //grab the serial library

Serial myPort; //create a serial port object
float xPos = 0;

void setup(){
//list serial ports
println(Serial.list());

size(600, 600);
background(0, 100, 120);
}

void draw(){
}

void serialEvent (Serial myPort){

String portName = Serial.list()[0]; //take the first item in the serial array list called by myPort
myPort = new Serial(this, portName, 9600); //communicate that info on the serial monitor at 9600 baud
// print it:
int inString = myPort.read();
// inString = inString/4;
println(inString);

float yPos = height – inString;

stroke(255, 10, 0);
line(xPos, height, xPos, yPos);

// at the edge of the screen, go back to the beginning:
if (xPos >= width) {
xPos = 0;
// clear the screen by resetting the background:
background(0, 100, 120);
}
else {
// increment the horizontal position for the next reading:
xPos++;
}
}

I will mention that I briefly attempted to run this code in Arduino, because I was not paying attention. You cannot import a processing library into Arduino.

The debug:
yPos must be a float
portName and myPort constructors must run in setup()

 
[/expand]

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s