Assignment 5.4

posted Aug 15, 2010, 3:35 PM by Unknown user
Arduino Code:

char val;                          // Data received from the serial port
 int ledPin = 2;                  // Set the pin to digital I/O 2
 int ledPin2 = 4;                //set the pin to digital I/O 4
 
 void setup() 
 {
   pinMode(ledPin, OUTPUT);        // Set pin as 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(ledPin, HIGH); // turn LED1 on
   } 
   Untitled Post
   else if (val == 'K') {        //If K was recieved
     digitalWrite(ledPin2, HIGH);//turn LED2 on
   }
   
     else if (val == 'P') {
       digitalWrite(ledPin, HIGH); //If P was received 
       digitalWrite(ledPin2, HIGH);//turn both LEDs on
     }
   
   else if (val == 'L') {
     digitalWrite(ledPin, LOW);  // If is received
     digitalWrite(ledPin2, LOW); //turn both LEDs off
   }
   delay(10);                   // Wait 10 milliseconds for next reading
 }

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(255);
  if ((mouseX >= 50) && (mouseX <= 150) && (mouseY >= 50) && (mouseY <= 150)) {  
                                  // If mouse is over the first square,
    fill(225, 0, 0);              // change color and
    myPort.write('H');            // send an H to indicate mouse location

else if ((mouseX >= 200) && (mouseX <= 300) && (mouseY >= 50) && (mouseY <= 150)) {
                                   // If mouse is over the second square,
  fill(0, 255, 0);                 //change color and 
  myPort.write('K');               // send a K to indicate mouse location
}

else if ((mouseX >= 50) && (mouseX <=150) && (mouseY >= 250) && (mouseY <= 350)) {
                                  //If mouse is over the third square,
  fill (0, 0, 255);               // change the color and
  myPort.write('P');              // send a P to indicate mouse location
}

  else {                          // If mouse is not over any square,
    fill(255, 255, 0);            // change color and
    myPort.write('L');            // send an L otherwise
  }
  
  rect(50, 50, 100, 100);         // Draw the first square
  rect(200, 50, 100, 100);        // Draw the second square
  rect(50, 250, 100, 100);        // Draw the third square
  
}
Comments