Monday, March 25, 2013

Tricolor LED Project 2: Color Slider


Processing and Arduino project 2 uses a tricolor LED and a screen interface to mix colors with sliders. That's a ping pong ball on the LED. The LED is wired like in this diagram: http://arduino.cc/en/uploads/Tutorial/readASCIIString_bb.png. Remember when doing projects with Arduino thru Processing, follow these steps: http://playground.arduino.cc/interfacing/processing
The code is published below:

video


Main code:

//arduino begin
import processing.serial.*; //for serial communication with any device
import cc.arduino.*; //for Arduino library methods
Arduino arduino; //create Arduino object from library
int rd = 9; 
int blu = 10; 
int grn = 11; 
//arduino end
Circle circle;
Slider[] slider = new Slider[3];
int[] xpos = new int[3];
int[] ypos = new int[3];

void setup() {
  //arduino begin
  println(Arduino.list());//show me which COM port Arduino is using
  arduino = new Arduino(this, Arduino.list()[4], 57600);
  arduino.pinMode(rd, Arduino.OUTPUT);
  arduino.pinMode(blu, Arduino.OUTPUT);
  arduino.pinMode(grn, Arduino.OUTPUT);
  //arduino end
  size(500,530);
  smooth();
  for (int i = 0; i < xpos.length; i++) {
    xpos[i] = (i+1)*30;
  }
  noStroke();
  circle = new Circle();
  for(int i = 0; i < slider.length; i++) {
    slider[i] = new Slider();
    ypos[i] = height-10;
  }//end for
}//end setup

void draw() {
  background(100);
  circle.setColorVars();
  circle.display();
  for(int i = 0; i < slider.length; i++) {
    int[] r = {255,0,0};
    int[] g = {0,255,0};
    int[] b = {0,0,255};
    slider[i].displayRect(r[i],g[i],b[i],xpos[i]);
    slider[i].displayHandle(xpos[i],ypos[i]);
  }//end for
}//end draw

void mouseDragged() {
  for(int i = 0;i < slider.length; i++) {
    slider[i].dragging();
  }
}

Circle class:

class Circle {
  int r = 0;
  int g = 0;
  int b = 0;
  PFont f;
  
  Circle() {
    f = createFont("Arial", 30,true);
    textFont(f);
  }//end const
  
  void setColorVars() {
    r = 255 - ypos[0]/2+5;
    g = 255 - ypos[1]/2+5;
    b = 255 - ypos[2]/2+5;
  }

  void display() {
    fill(r,g,b);
    ellipse(width/2+50,height/2,250,250);
    textAlign(CENTER);
    text("(" + r + "," + g + "," + b + ")", width/2+50,40);
    arduino.analogWrite(rd,255-r);
    arduino.analogWrite(blu,255-b);
    arduino.analogWrite(grn,255-g);
  }//end void
}//end class
Slider class:

class Slider {
  int btm = height-10;
  int tp = 10;

  Slider() {
  }
  
  void displayRect(int r, int g, int b, int x) {
    fill(r,g,b);
    rectMode(CENTER);
    rect(x,height/2,20,510);
  }
  
  void displayHandle(int x, int y) {
    fill(0);
    rectMode(CENTER);
    rect(x,y,20,20);
  }
  
  void dragging() {
    if(mouseY >= tp && mouseY <= btm) {
      if(dist(mouseX,mouseY,xpos[0],ypos[0]) < 15) {
        ypos[0] = mouseY;
      }//endif
      if(dist(mouseX,mouseY,xpos[1],ypos[1]) < 15) {
        ypos[1] = mouseY;
      }//endif
      if(dist(mouseX,mouseY,xpos[2],ypos[2]) < 15) {
        ypos[2] = mouseY;
      }//endif
    }//endif
  }//end void
} //end class

No comments :