Assignment 10.3

Post date: Aug 04, 2010 8:25:41 PM

I used keys on the keyboard to light up the RGB LED different colors when different keys are pressed. The keys used are "r" for the red LED, "g" for the green LED, "b" for the blue LED, and "y", "c", "m", and "w" to light up combinations of the three LEDs to produce yellow, cyan, magenta, and white, respectively. When the key is pressed in the Processing window, the background color is changed also. Here are my codes:

Arduino

int red = 11; // set up variables

int green = 3;

int blue = 9;

int val;

void setup() {

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

}

void loop() {

if (Serial.available()) { // if serial is available,

val = Serial.read(); // read it and store it in val

}

if (val == 'r') { // if val is this,

analogWrite(red, 255); // light up this LED and

delay(25); // delay for 25 millisceonds

}

else if (val == 'g') {

analogWrite(green, 255);

delay(25);

}

else if (val == 'b') {

analogWrite(blue, 255);

delay(25);

}

else if (val == 'y') {

analogWrite(red, 255);

analogWrite(green, 255);

delay(25);

}

else if (val == 'c') {

analogWrite(green, 255);

analogWrite(blue, 255);

delay(25);

}

else if (val == 'm') {

analogWrite(red, 255);

analogWrite(blue, 255);

delay(25);

}

else if (val == 'w') {

analogWrite(red, 255);

analogWrite(green, 255);

analogWrite(blue, 255);

delay(25);

}

else if (val == 'n') {

analogWrite(red, 0);

analogWrite(green, 0);

analogWrite(blue, 0);

}

}

Processing

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

Serial myPort; // set up variable

void setup() {

size(250, 250); // sets screen size to 250 x 250 pixels

myPort = new Serial(this, Serial.list()[0], 9600); // sets up serial port and begins serial communication at 9600 bps

}

void draw() {

background(0); // sets background color to black

}

void keyPressed() {

if (key == 'r') { // if key pressed is this key:

myPort.write('r'); // write r to the serial port,

background(255, 0, 0); // change the background color, and

delay(15); // delay for 15 milliseconds

}

if (key == 'g') {

myPort.write('g');

background(0, 255, 0);

delay(15);

}

if (key == 'b') {

myPort.write('b');

background(0, 0, 255);

delay(15);

}

if (key == 'y') {

myPort.write('y');

background(255, 255, 0);

delay(15);

}

if (key == 'c') {

myPort.write('c');

background(0, 255, 255);

delay(15);

}

if (key == 'm') {

myPort.write('m');

background(255, 0, 255);

delay(15);

}

if (key == 'w') {

myPort.write('w');

background(255);

delay(15);

}

if (key == 'x') { // I have no idea why I need this, but

myPort.write('n'); // if I don't have it the last color will

// stay on until the program is reset

}

else { // if any other key is pressed,

myPort.write('n'); // write n to the serial port

}

}

I tried to make it so that pressing a combination of keys would light up the same combination of LEDs, but it would either not turn off when the keys were released or would only light up one of the LEDs.