Skip to content

Instantly share code, notes, and snippets.

@koji
Last active September 26, 2016 23:16
Show Gist options
  • Save koji/79c940ad2be9a6c1a98e054197815ca5 to your computer and use it in GitHub Desktop.
Save koji/79c940ad2be9a6c1a98e054197815ca5 to your computer and use it in GitHub Desktop.
for arduino
import processing.serial.*;
Serial myPort;
String input;
int r = 0;
int g = 0;
int b = 0;
int ax = 0;
int ay = 0;
int sType =0;
void setup(){
size(800,800);
myPort = new Serial(this, "/dev/tty.usbmodem1421", 9600);
myPort.bufferUntil('\n');
}
void draw(){
background(r,g,b);
switch (sType) {
case 1:
ellipse(ax, ay,100,100);
break;
case 2:
rect(ax,ay, 100,100);
break;
case 3:
default :
//println("something wrong");
break;
}
}
void serialEvent(Serial p){
input = p.readString();
trim(input);
println(input);
String[] sensorReadings = split(input, ",");
int[] data = int(sensorReadings);
//print(sensorReadings);
//print(sensorReadings[0]);
//print(sensorReadings[2]);
//print(sensorReadings[3]);
//println(sensorReadings[1]);
if(data.length >2){
r = data[0];
g = data[1];
b = data[2];
ax = data[3];
ay = data[4];
sType = data[5];
//println(data[5]);
//println(int(sensorReadings[2]));
}
}
int rgbR = A0;
int rgbG = A1;
int rgbB = A2;
int axisX = A3;
int axisY = A4;
int sType = A5;
// for color
int mapR=0;
int mapG=0;
int mapB=0;
int r=0;
int g=0;
int b=0;
// for axis
int aX =0;
int aY =0;
int mapX =0;
int mapY =0;
// for shape
int shapeType =0;
int mapShape =0;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
r = analogRead(rgbR);
g = analogRead(rgbG);
b = analogRead(rgbB);
aX = analogRead(axisX);
aY = analogRead(axisY);
shapeType = analogRead(sType);
mapR = map(r,0,1023,0,255);
mapG = map(g,0,1023,0,255);
mapB = map(b,0,1023,0,255);
mapX = map(aX,0,1023,0,600);
mapY = map(aY,0,1023,0,600);
mapShape = map(shapeType,0,1023,0,3);
//Serial.print("R :");
//Serial.print(mapR);
//Serial.print(" G :");
//Serial.print(mapG);
//Serial.print(" B :");
//Serial.println(mapB);
Serial.print(mapR);
Serial.print(",");
Serial.print(mapG);
Serial.print(",");
Serial.print(mapB);
Serial.print(",");
Serial.print(mapX);
Serial.print(",");
Serial.print(mapY);
Serial.print(",");
Serial.print(mapShape);
Serial.println(",");
delay(200);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment