Lab 2

ldevendorf's picture

Description

Use an Arduino and serial ports to control Red, Green and Blue LEDs.

 

Components Used

  • 3 Light Emitting Diode (LED)
  • 3 Resistors
  • 1 Paper Cone diffuser

Arduino Code



/*
 * Serial RGB LED
 * ---------------
 * RGB values change based color under mouse.
 * Interfaces with Processing to read the color the mouse is hovering over
 * and sends the value to the arduino board.
 */



char colorCode;
int rValue,gValue,bValue;

int redPin   = 9;   // Red LED,   connected to digital pin 9
int greenPin = 10;  // Green LED, connected to digital pin 10
int bluePin  = 11;  // Blue LED,  connected to digital pin 11


void setup() {
  pinMode(redPin,   OUTPUT);   // sets the pins as output
  pinMode(greenPin, OUTPUT);   
  pinMode(bluePin,  OUTPUT);

  Serial.begin(9600);
  analogWrite(redPin, 0);
  analogWrite(greenPin, 0);
  analogWrite(bluePin, 0);

}

void loop () {

  // see if there's incoming serial data:
  if (Serial.available() > 0) {
    // read the oldest byte in the serial buffer:
    rValue = Serial.read();
    gValue = Serial.read();
    bValue = Serial.read();
    Serial.flush();
  }

  analogWrite(redPin, rValue);
  analogWrite(greenPin, gValue);
  analogWrite(bluePin, bValue);

  delay(100);  // wait a bit, for serial data


}

///////// Begin Processing Code /////////
import processing.serial.*;

Serial port;
color  hoverColor;
PImage image;
void setup() {
  image = loadImage("gs.jpg");
  size(image.width, image.height);



    // List all the available serial ports in the output pane.
    // You will need to choose the port that the Arduino board is
    // connected to from this list. The first port in the list is
    // port #0 and the third port in the list is port #2.
    println(Serial.list());

    // Open the port that the Arduino board is connected to (in this case #0)
    // Make sure to open the port at the same speed Arduino is using (9600bps)
    port = new Serial(this, Serial.list()[0], 9600);
}

void draw()
{
  image(image, 0, 0);
  hoverColor = image.get(mouseX, mouseY);
  port.write(int(red(hoverColor)));
  port.write(int(green(hoverColor)));
  port.write(int(blue(hoverColor)));

 
}

Board with No Diffuser
Board with Diffuser
0
Your rating: None