View on GitHub

oddgui

Physcomp Lab6 - Serial Communication

Using Serial Communication with Arduino and Processing was not too bad.

The arduino code is simple. We are using a Pot put in pin0 and doing a basic analogread. The import things about this whole lab is that you make sure you always close the serial monitor, because only one application could read the serial stream.

int analogPin = 0; int analogValue = 0; voidsetup() { // start serial port at 9600 bps:Serial.begin(9600); } voidloop() { // read analog input, divide by 4 to make the range 0-255: analogValue = analogRead(analogPin); analogValue = analogValue / 4; Serial.print(analogValue, BYTE); // pause for 10 milliseconds:delay(10); }

Once you upload close the Arduino application. So you avoid any serial issues. Open processing. I simply altered some code and made a sort of paint brush. Here is the code.
import processing.serial.*;

Serial myPort; // The serial port
int graphXPos = 1; // the horizontal position of the graph:
float randNum = 0;
void setup () {
size(640, 480); // window size

// List all the available serial ports
println(Serial.list());
// I know that the fisrt port in the serial list on my mac
// is usually my Arduino module, so I open Serial.list()[0].
// Open whatever port is the one you’re using.
myPort = new Serial(this, Serial.list()[0], 9600);

// set inital background:
background(255);
smooth();
}
void draw () {
// nothing happens in draw. It all happens in SerialEvent()
}

void serialEvent (Serial myPort) {
// get the byte:
int inByte = myPort.read();
randNum = random(255);
// print it:
println(inByte);
// set the drawing color. Pick a pretty color:
fill(randNum, randNum);
noStroke();
// draw the ellipse:
ellipse(graphXPos, inByte*2, 100, 100);

// at the edge of the screen, go back to the beginning:
if (graphXPos >= width) {
graphXPos = 0;

}
else {
// increment the horizontal position for the next reading:
graphXPos++;
}
}

</param></param></param></embed>

Physcomp Lab6 – Serial Communication from Zeven Rodriguez on Vimeo.