MHD Jafar Mortada
Published © MIT

Controlling RGB LED Using Processing IDE

Using Hexabitz RGB H01R00 LED and Processing IDE you can illuminate the led with 6 colors without the need to change the firmware.

BeginnerProtip15 minutes661
Controlling RGB LED Using Processing IDE

Things used in this project

Hardware components

Hexabitz RGB LED Module (H01R0x)
Hexabitz RGB LED Module (H01R0x)
×1
Wired Kelvin Clamp
×1
USB-UART Prototype Cable
×1

Software apps and online services

Processing
The Processing Foundation Processing

Story

Read more

Code

sketch_190502a.pde

Java
open it using Processing IDE, remember to change portName in the 10th line
import processing.serial.*; //import the serial library
Serial myPort;  // Create object from Serial class
int redCircleX, redCircleY;  // Position of red circle button
int greenCircleX, greenCircleY;  // Position of green circle button
int blueCircleX, blueCircleY;  // Position of blue circle button
int whiteCircleX, whiteCircleY;  // Position of white circle button
int yellowCircleX, yellowCircleY;  // Position of yellow circle button
int magentaCircleX, magentaCircleY;  // Position of magenta circle button
int circleSize = 93;   // Diameter of circles
String portName = "COM31"; //change this
color red, green, blue, white, yellow,magenta;
color lightRed, lightGreen, lightBlue, gray, lightYellow,lightMagenta;
boolean redCircleOver,greenCircleOver,blueCircleOver,whiteCircleOver,yellowCircleOver,magentaCircleOver;
void setup()
  {
  myPort = new Serial(this, portName, 921600);
  myPort.write("\r");
  redCircleOver = greenCircleOver = blueCircleOver = whiteCircleOver = yellowCircleOver = magentaCircleOver = false;
  size(640, 360);
  noStroke();
  background(13, 28, 30);
  red = color(255,0,0);
  green = color(0,255,0);
  blue = color(0,0,255);
  white = color(255,255,255);
  magenta = color(255,0,255);
  yellow = color(255,255,0);
  
  gray = color(211,211,211);
  lightRed = color(255,100,100);
  lightGreen = color(190,255,190);
  lightBlue = color(190,190,255);
  lightYellow = color(190,190,0);
  lightMagenta = color(255,100,255);
  
  redCircleY = greenCircleY = blueCircleY = height/3;
  whiteCircleY = yellowCircleY = magentaCircleY = 2*height/3;
  redCircleX = whiteCircleX = circleSize + 20;
  greenCircleX = yellowCircleX = redCircleX + 200;
  blueCircleX = magentaCircleX = greenCircleX + 200;
  ellipseMode(CENTER);
  }

void draw()
  {
    stroke(0);
    update(mouseX, mouseY);
    if (redCircleOver){
    fill(red);
  } else {
    fill(lightRed);
  }
     ellipse(redCircleX, redCircleY, circleSize, circleSize);
    if (greenCircleOver){
    fill(green);
  } else {
    fill(lightGreen);
  }
    ellipse(greenCircleX, greenCircleY, circleSize, circleSize);
    if (blueCircleOver){
    fill(blue);
  } else {
    fill(lightBlue);
  }
     ellipse(blueCircleX, blueCircleY, circleSize, circleSize); 
     
    if (whiteCircleOver){
    fill(white);
  } else {
    fill(gray);
  }
  ellipse(whiteCircleX, whiteCircleY, circleSize, circleSize); 
    
    if (yellowCircleOver){
    fill(yellow);
  } else {
    fill(lightYellow); }
    ellipse(yellowCircleX, yellowCircleY, circleSize, circleSize); 

   if (magentaCircleOver){
    fill(magenta);
  } else {
    fill(lightMagenta); }
    ellipse(magentaCircleX, magentaCircleY, circleSize, circleSize); 
 
  
  }
  
  
  void update(int x, int y) {
  if ( overCircle(redCircleX, redCircleY, circleSize) ) {
    redCircleOver = true;
    greenCircleOver = false;
    blueCircleOver = false;
    whiteCircleOver = false;
    yellowCircleOver = false;
    magentaCircleOver = false;
  }
  else  if ( overCircle(greenCircleX, greenCircleY, circleSize) ) {
    redCircleOver = false;
    greenCircleOver = true;
    blueCircleOver = false;
    whiteCircleOver = false;
    yellowCircleOver = false;
    magentaCircleOver = false;}
  else if( overCircle(blueCircleX, blueCircleY, circleSize) ) {
    redCircleOver = false;
    greenCircleOver = false;
    blueCircleOver = true;
    whiteCircleOver = false;
    yellowCircleOver = false;
    magentaCircleOver = false;}
  else if( overCircle(whiteCircleX, whiteCircleY, circleSize) ) {
    redCircleOver = false;
    greenCircleOver = false;
    blueCircleOver = false;
    whiteCircleOver = true;
    yellowCircleOver = false;
    magentaCircleOver = false;}
  else if( overCircle(yellowCircleX, yellowCircleY, circleSize) ) {
    redCircleOver = false;
    greenCircleOver = false;
    blueCircleOver = false;
    whiteCircleOver = false;
    yellowCircleOver = true;
    magentaCircleOver = false;}
  else if( overCircle(magentaCircleX, magentaCircleY, circleSize) ) {
    redCircleOver = false;
    greenCircleOver = false;
    blueCircleOver = false;
    whiteCircleOver = false;
    yellowCircleOver = false;
    magentaCircleOver = true;}
  else {
    redCircleOver = false;
    greenCircleOver = false;
    blueCircleOver = false;
    whiteCircleOver = false;
    yellowCircleOver = false;
    magentaCircleOver = false;
    }
  }
boolean overCircle(int x, int y, int diameter) {
  float disX = x - mouseX;
  float disY = y - mouseY;
  if (sqrt(sq(disX) + sq(disY)) < diameter/2 ) {
    return true;
  } else {
    return false;
  }
}
void sendMSG(String msg){
  int strLenght = msg.length();
  for (int i = 0; i<strLenght; i++)
  {
    myPort.write(msg.charAt(i));
    delay(20);
  }
  myPort.write("\r");
}

void mousePressed() {
  if (redCircleOver) {
    sendMSG("color red 100");
  }
  if (greenCircleOver) {
    sendMSG("color gree 100");
  }
  if (blueCircleOver) {
    sendMSG("color blue 100");
  }
  if (yellowCircleOver) {
    sendMSG("color yellow 100");
  }
  if (magentaCircleOver) {
    sendMSG("color magenta 100");
  }
   if (whiteCircleOver) {
    sendMSG("color white 100");
  }
  
}

Credits

MHD Jafar Mortada

MHD Jafar Mortada

10 projects • 4 followers

Comments