Control RGB LED from PC

Equipment:

  1. Arduino nano
  2. Gravitech GERORA (Neopixel workalike)
  3. HC-05 bluetooth module

เป้าหมายคือการตั้งค่าสีของ RGB LED แบบไร้สายด้วยเครื่องคอมพิวเตอร์ผ่าน bluetooth ฝั่ง PC จะใช้ Processing สร้างวงล้อสีขึ้นมา เมื่อผู้ใช้งานเลือกสีแล้วก็จะส่งค่าสีด้วยโปรโตคอล Firmata ผ่าน serial port ไปยัง Arduino

ตอนนี้สั่งค่าสีผ่านสาย usb ได้แล้ว รอซื้อ bluetooth module ก่อน
processing_rgbled

Code for Processing
[code language=”java”]
import processing.serial.*;
import cc.arduino.*;

byte R_PIN = 3;
byte G_PIN = 5;
byte B_PIN = 6;
byte LED_PIN = 13;
Arduino arduino;

PFont f;
boolean inCircle = false;
int mR,mG,mB;
color c;

void setup(){
arduino = new Arduino(this, Arduino.list()[1], 57600);
arduino.pinMode(R_PIN, Arduino.OUTPUT);
arduino.pinMode(G_PIN, Arduino.OUTPUT);
arduino.pinMode(B_PIN, Arduino.OUTPUT);
arduino.pinMode(LED_PIN, Arduino.OUTPUT);
f = loadFont("ArialMT-20.vlw");
background(255,255,255);
colorMode(HSB);
size(512,562);
translate(256,306);
smooth();
noStroke();
saturationChanger(128,256);
colorMode(RGB);
}
void saturationChanger(int i, int initial){
if(i > 0){
colorTriangle(256,0,initial,initial);
saturationChanger(i-1, initial-2);
}
}
void colorTriangle(int iteration, int h, int s,int height){
if(iteration > 0){
fill(h%256,s,256);
triangle(0,0,128*tan(radians(5.625/4)),height,-128*tan(radians(5.625/4)),height);
rotate(radians(5.625/4));
colorTriangle(iteration-1, h+1, s, height);
}
}

void draw() {
fill(255);
rect(0,0,512,40);
textFont(f,20);
fill(0);
text("R:",10,30);
text("G:",150,30);
text("B:",300,30);
if (inCircle) {
text(mR,40,30);
text(mG,180,30);
text(mB,330,30);
fill(c);
rect(420,5,30,30);
}
}

void mouseClicked() {
// Get mouse x,y
// mouseX mouseY
// Check if in circle
// center of circle is 256,306
if (dist(mouseX,mouseY,256,306) < 255) {
print("in circle ");
c = get(mouseX,mouseY);
mR = c >> 16 & 0xFF;
mG = c >> 8 & 0xFF;
mB = c & 0xFF;
inCircle = true;
arduino.analogWrite(R_PIN,mR);
arduino.analogWrite(G_PIN,mG);
arduino.analogWrite(B_PIN,mB);
arduino.digitalWrite(LED_PIN,1);
} else {
inCircle = false;
mR = 0;
mG = 0;
mB = 0;
arduino.analogWrite(R_PIN,mR);
arduino.analogWrite(G_PIN,mG);
arduino.analogWrite(B_PIN,mB);
arduino.digitalWrite(LED_PIN,0);
}
print(mouseX);
print(",");
println(mouseY);
}
[/code]

code for Arduino
[code language=”cpp”]
#include "FastLED.h"
#include <Firmata.h>

// How many leds in your strip?
#define NUM_LEDS 1

// For led chips like Neopixels, which have a data line, ground, and power, you just
// need to define DATA_PIN. For led chipsets that are SPI based (four wires – data, clock,
// ground, and power), like the LPD8806 define both DATA_PIN and CLOCK_PIN
#define DATA_PIN 2
#define CLOCK_PIN 13

#define R_PIN 3
#define G_PIN 5
#define B_PIN 6

// Define the array of leds
CRGB leds[NUM_LEDS];
int mR=255,mG=0,mB=0;
unsigned long lastLEDChanged;
unsigned long blinkPeriod = 500;

void analogWriteCallback(byte pin, int value)
{
switch (pin){
case R_PIN:
mR = value;
break;
case G_PIN:
mG = value;
break;
case B_PIN:
mB = value;
break;
}
}

void digitalWriteCallback(byte pin, int value)
{
pinMode(pin,OUTPUT);
digitalWrite(pin, value);
}

void setup() {
FastLED.addLeds<NEOPIXEL, DATA_PIN>(leds, NUM_LEDS);
leds[0].setRGB(mR,mG,mB);
FastLED.show();
lastLEDChanged = millis();

Firmata.setFirmwareVersion(0, 1);
Firmata.attach(ANALOG_MESSAGE, analogWriteCallback);
Firmata.attach(DIGITAL_MESSAGE, digitalWriteCallback);
Firmata.begin();
}

void loop() {
while(Firmata.available()) {
Firmata.processInput();
}
if (millis() – lastLEDChanged > blinkPeriod) {
if (!(leds[0])) {
leds[0].setRGB(mR,mG,mB);
} else {
leds[0] = CRGB::Black;
}
FastLED.show();
lastLEDChanged = millis();
}
}
[/code]

Facebooktwittergoogle_plusredditpinterestlinkedinmail

Leave a Reply

Your email address will not be published. Required fields are marked *