Assignment 5.1

Post date: Jun 30, 2010 5:53:20 AM

Arduino code:

const int switchPin5 = 5; // the number of the pushbutton pin

const int ledPin11 = 11; // the number of the LED pin

const int switchPin4 = 4;

const int ledPin12 = 12;

int val = 0;

void setup() {

// initialize the LED pin as an output:

pinMode(ledPin11, OUTPUT);

// initialize the pushbutton pin as an input:

pinMode(switchPin5, INPUT);

pinMode(ledPin12, OUTPUT);

pinMode(switchPin4, INPUT);

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

}

void loop()

{

if (digitalRead(switchPin4) == HIGH && digitalRead(switchPin5) == LOW)

// if switchPin4 is pressed AND switchPin5 is not pressed, then

{

val=1;

Serial.print(1, BYTE); // send 1 to Processing

digitalWrite(ledPin12, HIGH); // turn on ledPin12

digitalWrite(ledPin11, LOW); // turn off ledPin11

}

else if (digitalRead(switchPin4) == LOW && digitalRead(switchPin5) == HIGH)

// if switchPin4 is not pressed AND switchPin5 is pressed, then

{

val=0;

Serial.print(0, BYTE); // send 0 to Processing

digitalWrite(ledPin12, LOW); // turn off ledPin12

digitalWrite(ledPin11, HIGH); // turn on ledPin11

}

else if (digitalRead(switchPin4) == HIGH && digitalRead(switchPin5) == HIGH)

// if both switchPin4 and switchPin5 are pressed, then

{

val=10;

Serial.print(10, BYTE); // send 10 to Processing

digitalWrite(ledPin12, HIGH); // turn on both ledPins

digitalWrite(ledPin11, HIGH);

}

else // if nothing is pressed

{

val=11;

Serial.print(11, BYTE); // send 11 to Processing

// the above snippet of code borrowed from the bottom of the SimpleRead example sketch

digitalWrite(ledPin12, LOW); // turn off ledPins

digitalWrite(ledPin11, LOW);

}

delay(100);

}

Processing code:

import processing.serial.*;

Serial myPort; // Create object from Serial class

int val; // Data received from the serial port

void setup()

{

size(200, 200);

String portName = Serial.list()[0];

myPort = new Serial(this, portName, 9600);

}

void draw()

{

if ( myPort.available() > 0) { // If data is available,

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

}

background(255); // Set background to white

if (val == 1) { // If the serial value is 0,

fill(0, 225, 0); // set fill to black

}

else if (val == 0) { // If the serial value is not 0,

fill(0, 0, 225); // set fill to blue

}

else if (val == 10) {

fill(225, 0, 0); // set fill color to red

}

else {

fill(0); // set fill color to green

}

rect(50, 50, 100, 100);

}

This code both changes the color of the black rectangle in Processing and lights up the LEDs connected to pins 11 and 12 when the push buttons are pressed. The push buttons are connected to pins 4 and 5. When the button connected to pin 5 is pressed, it lights up the LED connected to pin 11 and changes the black rectangle to blue. When the button connected to pin 4 is pressed, it lights up the LED connected to pin 12 and changes the black rectangle to green. When both buttons are pressed, both LEDs light up and he black rectangle changes to red.