Thursday, March 22, 2012

Challenge Labs!

IR Remote Challenge Lab!

The components:




Processing Lab


Code for the Arduino:

void setup() {
Serial.begin(9600);
}
void loop() {
Serial.println(analogRead(A0));
delay(10);
}



// Graphing sketch
// This program takes ASCII-encoded strings
// from the serial port at 9600 baud and graphs them. It expects 
values in the
// range 0 to 1023, followed by a newline, or newline and 
carriage return
// This example code is in the public domain.
import processing.serial.*;
Serial myPort;  // The serial port
int xPos = 1;  // horizontal position of the graph
void setup () {
// set the window size:
size(400, 300); 
// List all the available serial ports
println(Serial.list());
myPort = new Serial(this, Serial.list()[1], 9600);
character:
myPort.bufferUntil('\n');
// set inital background:background(0);
}
void draw () {
// everything happens in the serialEvent()
}
void serialEvent (Serial myPort) {
// get the ASCII string:
String inString = myPort.readStringUntil('\n');
if (inString != null) {
// trim off any whitespace:
inString = trim(inString);
// convert to an int and map to the screen height:
float inByte = float(inString);
inByte = map(inByte, 0, 1023, 0, height);
// draw the line:
stroke(127,34,255);
line(xPos, height, xPos, height - inByte);
// at the edge of the screen, go back to the beginning:
if (xPos >= width) {
xPos = 0;
background(0);
}
else {
// increment the horizontal position:
xPos++;
}
}
}


And the color mixing code for the Arduino:

const int redPin = A0; // sensor to control red color
const int greenPin = A1; // sensor to control green color
const int bluePin = A2; // sensor to control blue color
void setup()
{
Serial.begin(9600);
}
void loop()
{
Serial.print(analogRead(redPin));
Serial.print(",");
Serial.print(analogRead(greenPin));
Serial.print(",");
Serial.println(analogRead(bluePin));


And finally, more code for Processing to run:


import processing.serial.*;
float redValue = 0;  // red value
float greenValue = 0;  // green value
float blueValue = 0;  // blue value
Serial myPort;
void setup() {
size(200, 200);
println(Serial.list());
myPort = new Serial(this, Serial.list()[1], 9600);
// don't generate a serialEvent() unless you get a newline 
character:
myPort.bufferUntil('\n');
}
void draw() {
// set the background color with the color values:
background(redValue, greenValue, blueValue);
}
void serialEvent(Serial myPort) {
String inString = myPort.readStringUntil('\n');
if (inString != null) {
// trim off any whitespace:
inString = trim(inString);
// split the string on the commas and convert the
// resulting substrings into an integer array:
float[] colors = float(split(inString, ","));
// if the array has at least three elements, you know
// you got the whole thing.  Put the numbers in the
// color variables:
if (colors.length >=3) {
// map them to the range 0-255:
redValue = map(colors[0], 0, 1023, 0, 255);
greenValue = map(colors[1], 0, 1023, 0, 255);
blueValue = map(colors[2], 0, 1023, 0, 255);
}
}





We found that each potentiometer "controls" the amount of one primary color (red, green, or blue) present in the Processing display window. By turning them in various combinations, we could produce every color from black to white to periwinkle blue and auburn red. I think we had the most fun with this challenge lab, if only because could think of a lot of interesting and fun potential applications and it was relatively straightforward to implement (doesn't it feel nice when you run things and they all work the first time?). ^^