Assignment 5.4

posted Jun 30, 2010, 8:35 PM by Unknown user
Here is the Arduino code:
 
char val;                          // Data received from the serial port
 int ledPin4 = 4;                   // Set the pin to digital I/O 4
 int ledPin12 = 12;
 
 void setup()
 {
   pinMode(ledPin4, OUTPUT);        // Set pin as OUTPUT
   pinMode(ledPin12, OUTPUT);
   Serial.begin(9600);             // Start serial communication at 9600 bps
 }
 
 void loop()
 {
   if (Serial.available())
   {                              // If data is available to read,
     val = Serial.read();         // read it and store it in val
   }
   if (val == 'H')
   {                             // If H was received
     digitalWrite(ledPin4, HIGH); // turn the LED on
     digitalWrite(ledPin12, HIGH);
   }
   else if (val == 'J') {
     digitalWrite(ledPin12, HIGH);
     digitalWrite(ledPin4, LOW);
   }
   else if (val == 'K') {
     digitalWrite(ledPin4, HIGH);  // If L turn it OFF
     digitalWrite(ledPin12, LOW);
   }
   else if (val == 'L') {
     digitalWrite(ledPin4, LOW);
     digitalWrite(ledPin12, LOW);
   }
  
   delay(10);                   // Wait 10 milliseconds for next reading
 }
 
and the Processing code:
 
import processing.serial.*;
Serial myPort;  // Create object from Serial class
int val;        // Data received from the serial port
void setup()
{
  size(400, 400);
  String portName = Serial.list()[0];
  myPort = new Serial(this, portName, 9600);
}
void draw() {
  background(0);
  if ((mouseX >= 175) && (mouseX <= 225) && (mouseY >= 175) && (mouseY <= 225)) { 
                                  // If mouse is over the red square,
    stroke(0);
    fill(0, 0, 225);                     // draw this and
    rect(50, 50, 300, 300);   
    fill(0, 225, 0);           
    rect(125, 125, 150, 150);     
    fill(225, 0, 0);
    rect(175, 175, 50, 50);
    myPort.write("H");            // send an H
}
  else if((mouseX >= 125) && (mouseX <= 275) && (mouseY >= 125) && (mouseY <= 275))
  { 
    stroke(0);                      // If mouse is over the green square,
    fill(0, 0, 225);                      // draw this and
    rect(50, 50, 300, 300);    
    fill(0, 225, 0);                   
    rect(125, 125, 150, 150);      
    fill(0, 0, 0, 0);
    stroke(225, 0, 0);
    rect(175, 175, 50, 50);
    myPort.write("J");            // send a J
  }
  else if((mouseX >= 50) && (mouseX <= 350) && (mouseY >= 50) && (mouseY <= 350))
  {                                // If mouse is over the blue square
    stroke(0);
    fill(0, 0, 225);                      // draw this and
    rect(50, 50, 300, 300);    
    fill(0, 0, 0, 0);
    stroke(0, 225, 0);
    rect(125, 125, 150, 150);
    stroke(225, 0, 0);
    rect(175, 175, 50, 50);
    myPort.write("K");            // send a K
  }
  else                           // If mouse is not over any square
  {
    fill(0, 0, 0, 0);            // draw this and
    stroke(0, 0, 225);
    rect(50, 50, 300, 300);
    stroke(0, 225, 0);
    rect(125, 125, 150, 150);
    stroke(225, 0, 0);
    rect(175, 175, 50, 50);
    myPort.write("L");            // send an L
  }
   
}
 
 
This code expands on the basic code given. Instead of having only one square, there are 3. When the cursor is moved into the first (blue) square, the LED connected to pin 4 is turned on and the square is filled in. When the cursor is moved into the second (green) square, the LED attached to pin 12 is turned on and the first two squares are filled in. When the cursor is moved into the third (red) square, both LEDs are turned on and all three squares are filled in.
Comments