Assignment 7.3

Post date: Jul 08, 2010 3:10:57 AM

The Arduino portion of this code is almost identical to the one in the example of the Punctuation method given, except I changed the names of the variables for funzies:

int potOne = 1; // set up variables

int potTwo = 2;

int potThree = 3;

int val = 0;

void setup() {

Serial.begin(9600); // begin serial communication at 9600 bps

}

void loop() {

val = analogRead(potOne); // read the potentiometer and store it in val

Serial.print(val, DEC); // print the value of val, and

Serial.print(","); // separate them with a comma

val = analogRead(potTwo);

Serial.print(val, DEC);

Serial.print(",");

val = analogRead(potThree);

Serial.println(val, DEC);

}

The Processing portion of the code required a couple new parts, though it is still very similar to the Punctuation method example:

import processing.serial.*; // import Processing library

Serial myPort; // set up variables

float redColor;

float greenColor;

float blueColor;

void setup() {

size (600, 600); // sets window size

background(0); // sets background color

myPort = new Serial(this, Serial.list()[0], 9600);

myPort.bufferUntil('\n'); // read bytes into a buffer until

} // you get a linefeed (ASCII 10)

void draw() {

// twiddle your thumbs

}

void serialEvent(Serial myPort) { // this method is run automatically when the buffer reaches the byte value set in the bufferUntil method

String myString = myPort.readStringUntil('\n'); // read the serial buffer

println(myString); // print the value of myString

if (myString != null); { // if myString is not null,

myString = trim(myString);

int sensors[] = int(split(myString, ',')); // split the string at the commas and convert the sections into integers

for (int sensorNum = 0; sensorNum < sensors.length; sensorNum++) {

print("Sensor " + sensorNum + "; " + sensors[sensorNum] + "\t"); // print the values of the new integers

}

if (sensors.length > 1) { // add a linefeed after all the sensor values are printed

redColor = map(sensors[0], 0, 1023, 0, 225); // sets and maps variables so that the values can

greenColor = map(sensors[1], 0, 1023, 0, 225); // be read as color values

blueColor = map(sensors[2], 0, 1023, 0, 225);

}

}

fill(redColor, greenColor, blueColor); // sets fill color according to new variables' values

ellipseMode(CENTER); // sets ellipse mode to center

ellipse(width/2, height/2, 500, 500); // draws ellipse

}

I tried to run this sketch without the serialEvent method, but that didn't work. So I added that method and things went splendidly from there. I guess there would be two ways to adjust the sensor values so that they can be read as color values. The first way is the way I did it, through the map function. The second would be through the colorMode function, by adjusting the color values to read 0 - 1023.

This sketch uses three potentiometers attached to analog pins 1, 2, and 3. I used such a big screen size because I like the look of the bigger circle, and I felt the smaller circles didn't show the color changes as well.