Interface and Application Programming

This week assignment I want to use it for My Final Project to start learning how to create an interface and application in the near future. One of my personal approaches with the project is to control it with a smartphone application so is great for me.

I am going to use Processing for the interface and for create an application that let me change the colors of the Light. So first I looked for button examples on processing libraries and I adjusted for what I need, basically I defined the geometry which is square, the number of buttons which are three and the size of them. Also I give them the red, green and blue colors meaning that each button turns on the corresponding light color.

Get all the files here

Code


/*
interactive rgb concrete lamp interface and application

by Juan Esteban Vallejo / 2014 (with help from Andrew Leek)
*/

import processing.serial.*;

// The serial port:
Serial myPort;

int redRectX, redRectY; // Position of square button
int greenRectX, greenRectY; // Position of square button
int blueRectX, blueRectY; // Position of square button

int rectSize = 200; // Diameter of rect
int circleSize = 93; // Diameter of circle
color redColor, greenColor, blueColor, baseColor;
color currentColor;

boolean redRectOver = false;
boolean greenRectOver = false;
boolean blueRectOver = false;

void setup() {
size(600, 200);
redColor = color(255,0,0);
greenColor = color(0,255,0);
blueColor = color(0,0,255);
baseColor = color(255);

redRectX = 0;
redRectY = 0;
greenRectX = 200;
greenRectY = 0;
blueRectX = 400;
blueRectY = 0;

// List all the available serial ports:
println(Serial.list());

// Open the port you are using at the rate you want:
myPort = new Serial(this, "/dev/tty.usbserial-A100RTDG", 4800);
}

void draw() {
update(mouseX, mouseY);
background(baseColor);

fill(redColor);
rect(redRectX, redRectY, rectSize, rectSize);

fill(greenColor);
rect(greenRectX, greenRectY, rectSize, rectSize);

fill(blueColor);
rect(blueRectX, blueRectY, rectSize, rectSize);
}

void update(int x, int y) {
if ( overRect(redRectX, redRectY, rectSize, rectSize) ) {
redRectOver = true;
greenRectOver = false;
blueRectOver = false;
} else if ( overRect(greenRectX, greenRectY, rectSize, rectSize) ) {
redRectOver = false;
greenRectOver = true;
blueRectOver = false;
} else if ( overRect(blueRectX, blueRectY, rectSize, rectSize) ) {
redRectOver = false;
greenRectOver = false;
blueRectOver = true;
} else {
redRectOver = greenRectOver = blueRectOver = false;
}
}

void mousePressed() {

if (redRectOver) {
//currentColor = circleColor;
myPort.write("2,900");
}

if (greenRectOver) {
myPort.write("4,900");
}

if (blueRectOver) {
myPort.write("0,900");
}

}

boolean overRect(int x, int y, int width, int height) {
if (mouseX >= x && mouseX <= x+width &&
mouseY >= y && mouseY <= y+height) {
return true;
} else {
return false;
}
}