http://blog.bsoares.com.br/processing/controlling-rgb-led-with-arduino-and-processing
Lista de Materiais:
IDE Arduino 1.0.6
IDE Processing 3 3.0b6 Alpha
1x Board Arduino NANO
1x Protoboard 830 furos
1x Cabo USB-A male to USB-mini male
1x Conj. de fios jumper p/ protoboard
1x Bolinha de ping-pong
1x Led RGB - Catodo comum
2x Resistor 90R 1/4w
1x Resistor 150R 1/4w
Montagem:
Firmware - Arduino:
#define START_COLOR_CHAR '^'
#define END_COLOR_CHAR '$'
#define COLOR_SIZE 8
#define PIN_RED 9
#define PIN_GREEN 11
#define PIN_BLUE 10
//******************************************************************************* char serialMessage[COLOR_SIZE];
unsigned int readChar;
unsigned int count;
unsigned long color;
unsigned int r;
unsigned int g;
unsigned int b;
boolean readingSerial;
//******************************************************************************* void setup() {
Serial.begin(9600);
readingSerial = false;
}
//******************************************************************************* void loop() {
if (Serial.available() > 0 && !readingSerial) {
if (Serial.read() == START_COLOR_CHAR) {
serialReadColor();
}
}
}
//******************************************************************************* void serialReadColor() {
readingSerial = true;
count = 0;
iniReading:
if (Serial.available() > 0) {
readChar = Serial.read();
if (readChar == END_COLOR_CHAR || count == COLOR_SIZE) {
goto endReading;
} else {
serialMessage[count++] = readChar;
goto iniReading;
}
}
goto iniReading;
endReading:
readingSerial = false;
serialMessage[count] = '\0';
setColor(serialMessage);
}
//******************************************************************************* void setColor(char* value)
{
// Convert Char* to Long
color = atol(value);
// Extract RGB
r = color >> 16 & 0xFF;
g = color >> 8 & 0xFF;
b = color >> 0 & 0xFF;
// Send values to analog pins
analogWrite(PIN_RED, r);
analogWrite(PIN_GREEN, g);
analogWrite(PIN_BLUE, b);
}
Firmware - Processing:
import processing.serial.*;
Serial port;
//******************************************************************************* void setup() {
size(100, 150);
noStroke();
// Background
colorMode(HSB, 100);
for (int i = 0; i < 100; i++) {
for (int j = 0; j < 100; j++) {
stroke(i, j, 100);
point(i, j);
}
}
// Select port
println(Serial.list());
//port = new Serial(this, Serial.list()[1], 9600);
port = new Serial(this, "COM16", 9600);
}
//******************************************************************************* void draw() {
// Only to enable the method mouseDragged
}
//******************************************************************************* void mouseClicked() {
processColor();
}
//******************************************************************************* void mouseDragged() {
processColor();
}
//*******************************************************************************
void processColor() {
color c = get(mouseX, mouseY);
noStroke();
fill(c);
rect(0, 100, 100, 50);
sendColorToSerial(c);
}
//******************************************************************************* void sendColorToSerial(color colour) {
// Get HEX
String hexColor = hex(colour, 6);
// Convert HEC to Number
long numColor = unhex(hexColor);
// Send color number to serial port
port.write("^" + numColor + "$");
}
Download: https://processing.org
Nenhum comentário:
Postar um comentário