Due to popular demand, here is the source code for my blog entry, “PC to Arduino to LED Control.”
First the Processing Source code:
// ButtonProc
// Make a button on the screen that can be clicked
// and cause an LED to turn on and off in synch on
// the Arduino. Used in conjunction with ButtonArd.
//
import processing.serial.*;
Serial myPort; // Create object from Serial class
int val; // Data sent to the serial port
// set ButtonValueY = 0
int ButtonValueY = 0;
int ButtonValueR = 0;
int ButtonValueG = 0;
int ButtonValueB = 0;
// set clickflag = 0
int clickflag = 0;
void setup() {
size(480, 500);
smooth();
String portName = Serial.list()[0];
myPort = new Serial(this, portName, 9600);
fill(150,150,0);
rect(100, 100, 50, 50);
fill(150,0,0);
rect(100,200, 50,50);
fill(0,150,0);
rect(100,300,50,50);
fill(0,0,150);
rect(100,400,50,50);
}
void draw() {
// IF the mouse not pressed
// THEN clickflag = 0 and return
if (mousePressed) {
clickflag = 0;
return;
}
//for when the mouse is pressed:
// IF clickflag = 1
// THEN return
if (clickflag == 1)
{
return;
}
// for the first time that we read the button is pressed:
// clickflag = 1
clickflag = 1;
// Yellow LED: Is MouseX and MouseY within boundaries?
if ((mouseX > 100) && (mouseX < 150) && (mouseY > 100) && (mouseY < 150))
{
// Is ButtonValueY = 0?
if (ButtonValueY == 0) {
ButtonValueY = 1;
fill(255,255,0);
myPort.write('A');
rect(100, 100, 50, 50);
} else {
fill(150,150,0);
ButtonValueY = 0;
myPort.write('B');
rect(100, 100, 50, 50);
} // end of ButtonValueY check
}
// Red LED: Is MouseX and MouseY within boundaries?
if ((mouseX > 100) && (mouseX < 150) && (mouseY > 200) && (mouseY < 250))
{
// Is ButtonValueR = 0?
if (ButtonValueR == 0) {
ButtonValueR = 1;
fill(255,0,0);
myPort.write('C');
rect(100, 200, 50, 50);
} else {
fill(150,0,0);
ButtonValueR = 0;
myPort.write('D');
rect(100, 200, 50, 50);
} // end of ButtonValueR check
}
// Green LED: Is MouseX and MouseY within boundaries?
if ((mouseX > 100) && (mouseX < 150) && (mouseY > 300) && (mouseY < 350))
{
// Is ButtonValueG = 0?
if (ButtonValueG == 0) {
ButtonValueG = 1;
fill(0,255,0);
myPort.write('E');
rect(100, 300, 50, 50);
} else {
fill(0,150,0);
ButtonValueG = 0;
myPort.write('F');
rect(100, 300, 50, 50);
} // end of ButtonValueG check
}
// Blue LED: Is MouseX and MouseY within boundaries?
if ((mouseX > 100) && (mouseX < 150) && (mouseY > 400) && (mouseY < 450))
{
// Is ButtonValueB = 0?
if (ButtonValueB == 0) {
ButtonValueB = 1;
fill(0,0,255);
myPort.write('G');
rect(100, 400, 50, 50);
} else {
fill(0,0,150);
ButtonValueB = 0;
myPort.write('H');
rect(100, 400, 50, 50);
} // end of ButtonValueG check
}
}
And here is the Arduino source code:
int incomingByte = 0; // for incoming serial data
void setup() {
Serial.begin(9600); // opens serial port, sets data rate to 9600 bps
// initialize the digital pin as an output.
pinMode(13, OUTPUT);
pinMode(12, OUTPUT);
pinMode(11, OUTPUT);
pinMode(10,OUTPUT);
}
void loop() {
// act on data only when you receive data:
if (Serial.available() < 1) {
return;
}
// read the incoming byte:
incomingByte = Serial.read();
//yellow LED
if (incomingByte == 65){
digitalWrite(13, HIGH); // set the LED on
}
if (incomingByte == 66) {
digitalWrite(13, LOW); // set the LED off
}
//red LED
if (incomingByte == 67) {
digitalWrite(12, HIGH); // set the LED on
}
if (incomingByte == 68) {
digitalWrite(12, LOW); // set the LED off
}
//green LED
if (incomingByte == 69) {
digitalWrite(11, HIGH); // set the LED on
}
if (incomingByte == 70) {
digitalWrite(11, LOW); // set the LED off
}
//blue LED
if (incomingByte == 71) {
digitalWrite(10, HIGH); // set the LED on
}
if (incomingByte == 72) {
digitalWrite(10, LOW); // set the LED off
}
}
You should be able to copy and paste this into the Processing and Arduino IDEs without any trouble. I’ll try to provide support, but I apologize in advance for being distracted with other stuff at the moment.