Skip to content

Instantly share code, notes, and snippets.

@zigen
Created June 29, 2015 09:35
Show Gist options
  • Save zigen/a17c0ed953a8b2e9945b to your computer and use it in GitHub Desktop.
Save zigen/a17c0ed953a8b2e9945b to your computer and use it in GitHub Desktop.
arduino調光器GUI
import controlP5.*;
import processing.serial.*;
ControlP5 cp5;
Knob[] colorKnob = new Knob[3];
Serial arduino;
color ledColor;
int r=0, g=0, b=0;
void setup()
{
size(700, 255);
String portName = Serial.list()[2];
cp5 = new ControlP5(this);
print(Serial.list()[2]);
arduino = new Serial(this, portName, 9600);
colorKnob[0] = cp5.addKnob("redKnob")
.setRange(0, 254)
.setValue(50)
.setPosition(100, 70)
.setRadius(50)
.setDragDirection(Knob.VERTICAL)
;
colorKnob[1] = cp5.addKnob("greenKnob")
.setRange(0, 254)
.setValue(50)
.setPosition(300, 70)
.setRadius(50)
.setDragDirection(Knob.VERTICAL)
;
colorKnob[2] = cp5.addKnob("blueKnob")
.setRange(0, 254)
.setValue(50)
.setPosition(500, 70)
.setRadius(50)
.setDragDirection(Knob.VERTICAL)
;
}
void draw() {
background(ledColor);
if (arduino.available()>0) {
String buf = arduino.readStringUntil(10);
if (buf != null) {
int[] color_buf = int(split(buf, ','));
ledColor = color(color_buf[0], color_buf[1], color_buf[2]);
println(buf);
}
}
}
void redKnob(int val) {
r = val;
send();
}
void greenKnob(int val) {
g = val;
send();
}
void blueKnob(int val) {
b = val;
send();
}
void send() {
arduino.write(0);
arduino.write(r);
arduino.write(g);
arduino.write(b);
}
void mouseClicked(){
send();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment