Monday, March 25, 2013

Tricolor LED Project 3: Color mixing and Crossfades

This project just uses Arduino and the tricolor LED to make random mixed colors and crossfades. The LED is wired like in this diagram:  http://arduino.cc/en/uploads/Tutorial/readASCIIString_bb.png
Program code is below:

video



Program code:
int r,g,b;
int LEDs[] = {r,g,b};

int brightness = 3; 
int fadeAmount = 3; 
int pick1,pick2,pick3;
int PICKS[] = {pick1,pick2,pick3};

void setup()  { 
  for(int i = 0;i < 3;i++) {
    LEDs[i] = i+9;
    pinMode(LEDs[i],OUTPUT);
    PICKS[i] = random(3);
  }
  //Serial.begin(9600);
} 

void loop()  { 
  if(brightness == 3) {
    for(int i = 0;i < 3;i++) {
      PICKS[i] = random(3);
    }//end for
  }//endif
  for(int i = 0;i < 3;i++) {
      switch(PICKS[i]) {
      case 0:
        analogWrite(LEDs[i], brightness);
        break;
      case 1:
        analogWrite(LEDs[i], -brightness);
        break;
      case 2:
        analogWrite(LEDs[i], 255);
        break;
      }//end switch
  }//end for
  brightness += fadeAmount;
  if (brightness < 4 || brightness == 255) {
    fadeAmount = -fadeAmount ; 
  }     
  delay(30);                            
}


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

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