Tuesday, February 19, 2008

[prototype03_microcontroller to processing]

this week we made the micro controller communicate with the computer using Processing(rather than the computer sending the micro controller information).
using the photo resistor, a series of squares dance on the screen when exposed to light. when covered, the squares stop.
this test set up basic communication and understanding of code. 

next week we plan to import data into processing and make the micro controller react (basically opposite of todays video).
if the micro controller can respond to lists of data then we can begin to import live air quality updates.
import processing.serial.*;

int bgcolor; // Background color
color fgcolor; // Fill color
Serial port; // The serial port
int[] serialInArray = new int[100]; // Where we'll put what we receive
int serialCount = 0; // A count of how many bytes we receive
float xpos, ypos; // Starting position of the ball
boolean firstContact = false; // Whether we've heard from the microcontroller

void setup() {
size(256, 256); // Stage size
noStroke(); // No border on the next thing drawn

// Set the starting position of the ball (middle of the stage)
xpos = width;
ypos = height;

// Print a list of the serial ports, for debugging purposes:
println(Serial.list());


port = new Serial(this, "COM3", 9600);
port.write(65); // Send a capital A to start the microcontroller sending
}

void draw() {
background(bgcolor);

noStroke();
rect(xpos, ypos, 20, 20);
rect(xpos/2, ypos/2, 20, 20);
rect(xpos/4, ypos/4, 20, 20);
stroke(255);
//line(xpos, ypos, xpos*6, ypos*6);
// Get any new serial data
while (port.available() > 0) {
serialEvent();
// Note that we heard from the microntroller:
firstContact = true;
}
// If there's no serial data, send again until we get some.
// (in case you tend to start Processing before you start your
// external device):
if (firstContact == true) {

fill(162,211,177);

delay(200);
port.write(65);
}
}

void serialEvent() {
processByte((char)port.read());
}

void processByte(char inByte) {
// Add the latest byte from the serial port to array:
serialInArray[serialCount] = inByte;
serialCount++;
// If we have 3 bytes:
if (serialCount >2 ) {
xpos = (float)serialInArray[0];
ypos = (float)serialInArray[2];
fgcolor = (int)serialInArray[2];
// Send a capital A to request new sensor readings:
port.write(65);
// Reset serialCount:
serialCount = 0;
}

}


Labels: ,


Comments: Post a Comment



<< Home

This page is powered by Blogger. Isn't yours?