Monday, March 25, 2013

Tricolor LED Project 1: Color Picker

I'm using Processing and Arduino to make some interactive light projects with a tricolor LED with some nice results. The first is an app that shows how digital colors mix. 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



//COMMON ANODE TO 5V
import processing.serial.*; //for serial communication with any device
import cc.arduino.*; //for Arduino library methods
Arduino arduino; //create Arduino object from library
Circle circle;
Button[] button = new Button[3];
Led[] led = new Led[3];
boolean r_on,g_on,b_on;

void setup() {
  println(Arduino.list());//show me which COM port Arduino is using
  arduino = new Arduino(this, Arduino.list()[4], 57600);
  size(500,500);
  smooth();
  noStroke();
  circle = new Circle();
  for(int i = 0; i < button.length; i++) {
    button[i] = new Button();
    led[i] = new Led();
  }//end for
}//end setup

void draw() {
  background(100);
  if(r_on && !g_on && !b_on) {
    circle.setColor(255,0,0);
    for(int i = 0;i < button.length; i++) {
      button[i].redOn();
      led[i].redOn();
    }//endfor
  } else if(!r_on && g_on && !b_on) {
    circle.setColor(0,255,0);
    for(int i = 0;i < button.length; i++) {
      button[i].greenOn();
      led[i].greenOn();
    }//endfor
  } else if(!r_on && !g_on && b_on) {
    circle.setColor(0,0,255);
    for(int i=0; i < button.length; i++) {
      button[i].blueOn();
      led[i].blueOn();
   }//endfor
  } else if(r_on && g_on && !b_on) {
    circle.setColor(255,255,0);
    for(int i=0; i < button.length; i++) {
      button[i].yellowOn();
      led[i].yellowOn();
    }//endfor
  } else if(r_on && !g_on && b_on) {
    circle.setColor(255,0,255);
    for(int i=0; i < button.length; i++) {
      button[i].magentaOn();
      led[i].magentaOn();
    }//endfor
  } else if(!r_on && g_on && b_on) {
    circle.setColor(0,255,255);
    for(int i=0; i < button.length; i++) {
      button[i].cyanOn();
      led[i].cyanOn();
    }//endfor
  } else if(r_on && g_on && b_on){
    circle.setColor(255,255,255);
    for(int i=0; i < button.length; i++) {
      button[i].whiteOn();
      led[i].whiteOn();
    }//endfor
  } else {
    circle.setColor(0,0,0);
    for(int i = 0; i < button.length; i++) {
      button[i].blackOn();
      led[i].blackOn();
    }//endfor
  }//endelse
  circle.display();
}//end draw

void mousePressed() {
  if(dist(mouseX,mouseY,width/4,height/5*4) < 25) {
    r_on = !r_on;
  }
  if(dist(mouseX,mouseY,width/2,height/5*4) < 25) {
    g_on = !g_on;
  }
  if(dist(mouseX,mouseY,width/4*3,height/5*4) < 25) {
    b_on = !b_on;
  }
}//end void
Circle class:
class Circle {
  int rC = 0;
  int gC = 0;
  int bC = 0;
  PFont f;
  
  Circle() {
    f = createFont("Arial", 30,true);
    textFont(f);
  }//end const
  
  void setColor(int red,int green, int blue) {
    rC = red;
    gC = green;
    bC = blue;
  }//end void
  
  void display() {
    fill(rC,gC,bC);
    ellipse(width/2,height/2-50,250,250);
    textAlign(CENTER);
    text("(" + rC + "," + gC + "," + bC + ")", width/2,40);
  }//end void  
}//end class

Button class:
class Button {
  //arrays for r,g,b values for 3 buttons
  int[] r = new int[3];
  int[] g = new int[3];
  int[] b = new int[3];
  int[] xpos = new int[3];

  Button() {
  xpos[0] = width/4;
  xpos[1] = width/2;
  xpos[2] = width/4*3;
}
  
  void display(int br, int bg, int bb, int x) {
    fill(br,bg,bb);
    ellipse(x,height/5*4,50,50);
  }
  
  void redOn() {
    for(int i=0; i < button.length; i++) {
      if(i == 0) {
        r[i] = 255;
      } else {
        r[i] = 0;
      }
      g[i] = 0;
      b[i] = 0;
      button[i].display(r[i],g[i],b[i],xpos[i]);
    }
  }
  
  void greenOn() {
    for(int i=0; i < button.length; i++) {
      if(i == 1) {
        g[i] = 255;
      } else {
        g[i] = 0;
      }
      r[i] = 0;
      b[i] = 0;
      button[i].display(r[i],g[i],b[i],xpos[i]);
    }//endfor
  }//endvoid

  void blueOn() {
    for(int i=0; i < button.length; i++) {
      if(i == 2) {
        b[i] = 255;
      } else {
        b[i] = 0;
      }
      r[i] = 0;
      g[i] = 0;
      button[i].display(r[i],g[i],b[i],xpos[i]);
    }//endfor
  }//endvoid

  void yellowOn() {
    for(int i=0; i < button.length; i++) {
       if(i == 0) {
        r[i] = 255;
      } else {
        r[i] = 0;
      }
       if(i == 1) {
        g[i] = 255;
      } else {
        g[i] = 0;
      }
      b[i] = 0;
      button[i].display(r[i],g[i],b[i],xpos[i]);
    }//endfor
  }//endvoid

  void magentaOn() {
    for(int i=0; i < button.length; i++) {
       if(i == 0) {
        r[i] = 255;
      } else {
        r[i] = 0;
      }
       if(i == 2) {
        b[i] = 255;
      } else {
        b[i] = 0;
      }
      g[i] = 0;
      button[i].display(r[i],g[i],b[i],xpos[i]);
    }//endfor
  }//endvoid

  void cyanOn() {
    for(int i=0; i < button.length; i++) {
       if(i == 1) {
        g[i] = 255;
      } else {
        g[i] = 0;
      }
       if(i == 2) {
        b[i] = 255;
      } else {
        b[i] = 0;
      }
      r[i] = 0;
      button[i].display(r[i],g[i],b[i],xpos[i]);
    }//endfor
  }//endvoid

  void whiteOn() {
    for(int i=0; i < button.length; i++) {
       if(i == 0) {
        r[i] = 255;
      } else {
        r[i] = 0;
      }
       if(i == 1) {
        g[i] = 255;
      } else {
        g[i] = 0;
      }
       if(i == 2) {
        b[i] = 255;
      } else {
        b[i] = 0;
      }
      button[i].display(r[i],g[i],b[i],xpos[i]);
    }//endfor
  }//endvoid

  void blackOn() {
    for(int i=0; i < button.length; i++) {
      button[i].display(0,0,0,xpos[i]);
    }//endfor
  }//endvoid
}//end class

Led class:
class Led{
  int rd,blu,grn;
  int colors[] = {rd,blu,grn};//array for LED colors
  
  Led() {
    for(int i = 0; i < colors.length; i++) {
      colors[i] = i+9;//assign arduino pins
      arduino.pinMode(colors[i], Arduino.OUTPUT);//pins to output
    }
  }
  
  void redOn() {
    for(int i=0; i < button.length; i++) {
      //array is "rd,blu,grn", LOW is on, HIGH is off!
      if(i == 0) {
        arduino.digitalWrite(colors[i], Arduino.LOW);
      } else {
        arduino.digitalWrite(colors[i], Arduino.HIGH);
      }
    }//endfor
  }//endvoid
  
  void greenOn() {
    for(int i=0; i < button.length; i++) {
      if(i == 2) {
        arduino.digitalWrite(colors[i], Arduino.LOW);
      } else {
        arduino.digitalWrite(colors[i], Arduino.HIGH);
      }
    }//endfor
  }//end void

  void blueOn() {
    for(int i=0; i < button.length; i++) {
      if(i == 1) {
        arduino.digitalWrite(colors[i], Arduino.LOW);
      } else {
        arduino.digitalWrite(colors[i], Arduino.HIGH);
      }
    }//endfor
  }//end void
  
  void yellowOn() {
    for(int i=0; i < button.length; i++) {
      if(i == 1) {
        arduino.digitalWrite(colors[i], Arduino.HIGH);
      } else {
        arduino.digitalWrite(colors[i], Arduino.LOW);
      }
    }//endfor
  }//end void
  
  void magentaOn() {
    for(int i=0; i < button.length; i++) {
      if(i == 2) {
        arduino.digitalWrite(colors[i], Arduino.HIGH);
      } else {
        arduino.digitalWrite(colors[i], Arduino.LOW);
      }
    }//endfor
  }//end void

  void cyanOn() {
    for(int i=0; i < button.length; i++) {
      if(i == 0) {
        arduino.digitalWrite(colors[i], Arduino.HIGH);
      } else {
        arduino.digitalWrite(colors[i], Arduino.LOW);
      }
    }//endfor
  }//end void

  void whiteOn() {
    for(int i=0; i < button.length; i++) {
      arduino.digitalWrite(colors[i], Arduino.LOW);
    }//endfor
  }//end void

  void blackOn() {
    for(int i=0; i < button.length; i++) {
      arduino.digitalWrite(colors[i], Arduino.HIGH);
    }//endfor
  }//end void
}//endclass

No comments :