176 lines
4.3 KiB
Plaintext
176 lines
4.3 KiB
Plaintext
/**
|
|
* Simple Read
|
|
*
|
|
* Read data from the serial port and change the color of a rectangle
|
|
* when a switch connected to a Wiring or Arduino board is pressed and released.
|
|
* This example works with the Wiring / Arduino program that follows below.
|
|
*/
|
|
|
|
|
|
import processing.serial.*;
|
|
|
|
Serial myPort; // Create object from Serial class
|
|
int analoglow; // Data received from the serial port
|
|
int analoghigh; // Data received from the serial port
|
|
int newdata=0;
|
|
|
|
int screenwidth=1024;
|
|
int screenheight=768;
|
|
|
|
int xstart;
|
|
int ystart;
|
|
int xend;
|
|
int yend;
|
|
|
|
int testpart=1; //0=finished, 1=left, 2=right, 3=up, 4=down
|
|
|
|
int readingA=0;
|
|
int readingB=0;
|
|
|
|
int stopsize=30; //minimum rect height and width
|
|
int minvaluedistance=50; //how far analog values need to be apart
|
|
|
|
void setup()
|
|
{
|
|
size(1024, 768);
|
|
|
|
xstart=0;
|
|
ystart=0;
|
|
xend=width;
|
|
yend=height;
|
|
|
|
printArray(Serial.list());
|
|
//String portName = Serial.list()[35];
|
|
String portName="/dev/ttyUSB0";
|
|
myPort = new Serial(this, portName, 115200);
|
|
}
|
|
|
|
void draw()
|
|
{
|
|
if ( myPort.available() > 0) { // If data is available,
|
|
String inBuffer = trim(myPort.readString());
|
|
if (inBuffer != null) {
|
|
//println(inBuffer);
|
|
String[] list = split(inBuffer, ';');
|
|
//print("list=");
|
|
//printArray(list);
|
|
if (list.length>=2){
|
|
analoglow=parseInt(list[0]);
|
|
analoghigh=parseInt(list[1]);
|
|
//print("->");
|
|
//print(analoglow);
|
|
//print(",");
|
|
//println(analoghigh);
|
|
newdata+=1;
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (newdata>=3){ //wait some reading
|
|
newdata=0; //reset counter
|
|
|
|
int reading=analoglow; //use only one analog value
|
|
|
|
switch(testpart){
|
|
case 1: //left
|
|
readingA=reading;
|
|
print("A="); println(readingA);
|
|
testpart++; //next part
|
|
break;
|
|
case 2: //right
|
|
readingB=reading;
|
|
print("B="); println(readingB);
|
|
//apply to positions
|
|
if (readingA>readingB+minvaluedistance){ //left part was brighter
|
|
xend=xstart+(xend-xstart)/2;
|
|
println("Left");
|
|
testpart++; //next part
|
|
}else if(readingB>readingA+minvaluedistance){
|
|
xstart=xstart+(xend-xstart)/2;
|
|
println("Right");
|
|
testpart++; //next part
|
|
}else{
|
|
print("No definitely better part found: ");
|
|
print(readingA); print(" vs. "); println(readingB);
|
|
float randommove=random(-(xstart-xend)/20,(xstart-xend)/20);
|
|
while (abs(randommove)<1){
|
|
randommove*=2;
|
|
}
|
|
xstart+=randommove; //move rect a bit
|
|
xend+=randommove;
|
|
testpart--;
|
|
}
|
|
break;
|
|
case 3: //up
|
|
readingA=reading;
|
|
print("A="); println(readingA);
|
|
testpart++; //next part
|
|
break;
|
|
case 4: //down
|
|
readingB=reading;
|
|
print("B="); println(readingB);
|
|
if (readingA>readingB+minvaluedistance){ //upper part was brighter
|
|
yend=ystart+(yend-ystart)/2;
|
|
println("Up");
|
|
testpart++; //next part
|
|
}else if(readingB>readingA+minvaluedistance){
|
|
ystart=ystart+(yend-ystart)/2;
|
|
println("Down");
|
|
testpart++; //next part
|
|
}else{
|
|
print("No definitely better part found: ");
|
|
print(readingA); print(" vs. "); println(readingB);
|
|
float randommove=random(-(ystart-yend)/20,(ystart-yend)/20);
|
|
while (abs(randommove)<1){
|
|
randommove*=2;
|
|
}
|
|
ystart+=randommove; //move rect a bit
|
|
yend+=randommove; //move rect a bit
|
|
testpart--;
|
|
}
|
|
break;
|
|
}
|
|
|
|
|
|
if (testpart>4){
|
|
testpart=1;
|
|
}
|
|
|
|
print("Testing Part:");
|
|
println(testpart);
|
|
}
|
|
|
|
if (yend-ystart<=stopsize){
|
|
testpart=0; //stop
|
|
}
|
|
|
|
|
|
|
|
background(0); // Set background to white
|
|
clear();
|
|
fill(255);
|
|
rectMode(CORNERS);
|
|
switch(testpart){ //draw current segment
|
|
case 1: //left
|
|
rect(xstart, ystart, xstart+(xend-xstart)/2, yend);
|
|
break;
|
|
case 2: //right
|
|
rect(xstart+(xend-xstart)/2, ystart, xend, yend);
|
|
break;
|
|
case 3: //up
|
|
rect(xstart, ystart, xend, ystart+(yend-ystart)/2);
|
|
break;
|
|
case 4: //down
|
|
rect(xstart, ystart+(yend-ystart)/2, xend, yend);
|
|
break;
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
} |