Monday, October 8, 2007

Lab_05 | Open the Serial Port


Application ->processing->library->serial->macosx_setup.command


Lab_05 | Processing code

import processing.serial.*;

int bgcolor; // Background color
int fgcolor; // Fill color
Serial port; // The serial port
int[] serialInArray = new int[3]; // Where we'll put what we receive
int serialCount = 0; // A count of how many bytes we receive
int 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/2;
ypos = height/2;

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

// I know that the first port in the serial list on my mac
// is always my Keyspan adaptor, so I open Serial.list()[0].
// On Windows machines, this generally opens COM1.
// Open whatever port is the one you're using.
port = new Serial(this, Serial.list()[1], 9600);
port.write(65); // Send a capital A to start the microcontroller sending
}

void draw() {
background(bgcolor);
fill(fgcolor);
// Draw the shape
ellipse(xpos, ypos, 20, 20);

// If no serial data has beeen received, send again until we get some.
// (in case you tend to start Processing before you start your
// external device):
if (firstContact == false) {
delay(300);
port.write(65);
}
}

void serialEvent(Serial port) {
// if this is the first byte received,
// take note of that fact:
if (firstContact == false) {
firstContact = true;
}
// Add the latest byte from the serial port to array:
serialInArray[serialCount] = port.read();
serialCount++;

// If we have 3 bytes:
if (serialCount > 2 ) {
xpos = serialInArray[0];
ypos = serialInArray[1];
fgcolor = serialInArray[2];

// print the values (for debugging purposes only):
println(xpos + "\t" + ypos + "\t" + fgcolor);

// Send a capital A to request new sensor readings:
port.write(65);
// Reset serialCount:
serialCount = 0;
}
}

Lab_05 | Programming Arduino

int firstSensor = 0; // first analog sensor
int secondSensor = 0; // second analog sensor
int thirdSensor = 0; // digital sensor
int inByte = 0; // incoming serial byte

void setup()
{
// start serial port at 9600 bps:
Serial.begin(9600);
pinMode(thirdSensor, INPUT);
}

void loop()
{
// if we get a valid byte, read analog ins:
if (Serial.available() > 0) {
// get incoming byte:
inByte = Serial.read();
// read first analog input, divide by 4 to make the range 0-255:
firstSensor = analogRead(0)/4;
// delay 10ms to let the ADC recover:
delay(10);
// read second analog input, divide by 4 to make the range 0-255:
secondSensor = analogRead(1)/4;
// read switch, multiply by 255
// so that you're sending 0 or 255:
thirdSensor = 255 * digitalRead(2);
// send sensor values:
Serial.print(firstSensor, BYTE);
Serial.print(secondSensor, BYTE);
Serial.print(thirdSensor, BYTE);
}
}

Lab_05 | My second breadboard with an IR sensor and a force sensor

I connected sensors in the same way with my first breadboard like these pictures.

Lab_05 | IR sensor

And then, I also tried to use an infrared ranger as one of analog sensors. Because I want to use this sensor for my midterm project.

Lab_05 | Connecting sansors

I connected my two analog sensors to analog pins 0 and 1 and a digital switch to digital pin 2.

Lab_05 | Two Analog Sensors

At first, I chose two analog sensors: a pot and a infrared ranger sensor.