controlling processing sketch with sensors

first the famous ball as always:

then the processing sketch with some tweaks:

after maping:

the code:
import processing.serial.*;

Serial myPort; // The serial port
float bgcolor; // Background color
float fgcolor; // Fill color
float xpos = 130; // X-coordeinate
float ypos = 210; // Y-coordinate
int radius = 20; // wheel Radius

float noteX;
float noteY;

float carAngle = 0;
float angleChange = 0.19;
float angleLimit = -6;
float angleBall;
void setup() {
// List all the available serial ports
println(Serial.list());

// I know that the first port in the serial list on my mac
// is always my Arduino module, so I open Serial.list()[0].
// Change the 0 to the appropriate number of the serial port
// that your microcontroller is attached to.
String portName = Serial.list()[0];
myPort = new Serial(this, portName, 9600);

// read bytes into a buffer until you get a linefeed (ASCII 10):
myPort.bufferUntil(‘\n’);
size(800, 400);
}

void draw() {
background(255);
//
// if (button2 == false) {
// // angleChange = 0;
// carAngle = 0;

// else if (keyCode == UP){
carAngle -= angleChange;

if (carAngle < angleLimit || carAngle > 0)
{
angleChange =-angleChange;
carAngle -= angleChange;
}

//wheels
//rear wheel
stroke(2);
//noStroke();
fill(#36322E); //biggest
ellipse(xpos, ypos, radius, radius);

fill(255); //white
stroke(255);
strokeWeight(1);
fill(#36322E);
ellipse(xpos, ypos, radius-7, radius-7);
ellipse(xpos, ypos, radius-11, radius-11);

noStroke();
fill(#36322E); //thiniest
ellipse(xpos, ypos, radius-18, radius-18); //inside

//FRONT wheel
noStroke();
fill(#36322E); //biggest
ellipse(xpos+330, ypos, radius, radius);

fill(255); //white
stroke(255);
strokeWeight(1);
fill(#36322E);
ellipse(xpos+330, ypos, radius-7, radius-7);
ellipse(xpos+330, ypos, radius-11, radius-11);

noStroke();
fill(#36322E);
ellipse(xpos+330, ypos, radius-18, radius-18); //inside if ever BLIN-BLIN

//body
pushMatrix();

translate(xpos, 210);

// float xpos=0;
// float y=0;
//

stroke(2);
fill(255);

rotate(radians(carAngle));

beginShape();
vertex(0, ypos-210-radius * 1.7); //1
vertex(0+radius*2.2/2, ypos-210-radius * 1.7*0.8); //2
vertex(130 + 242, ypos-210-radius * 1.7*1.1); //3
vertex(130 + 242+radius*0.6, ypos-210-radius * 1.7*0.7);//4
vertex(130 + 242, ypos-210+ radius/2);//5
vertex(130 + 242-radius*0.8, ypos-210+ radius/2);//6
vertex(130 + 242-radius*1.1, ypos-210- radius);//7
vertex(130 + 242-radius*0.8-radius*2.2, ypos-210- radius);//8
vertex(130 + 242-radius*2.2*1.7, ypos-210+ radius*0.6);//9
vertex(130+10, ypos-210+ radius*0.6);//10
vertex(0+radius*2.2/2, ypos-210- radius*0.6);//11
vertex(0-radius*2.2/2, ypos-210- radius*0.6);//12
vertex(0-radius*2.2*0.6, ypos-210+ radius*0.6);//13
vertex(0-radius*2.2*1.7, ypos-210+ radius*0.6);//14
vertex(0-radius*2.2*1.9, ypos-210);//15
vertex(0-radius*2.2*1.5, ypos-210 – radius * 1.4*0.9);//16
endShape(CLOSE);

//HOOD
beginShape();
vertex(0, ypos-210-radius * 1.7); //1
vertex(0+radius*2.2*0.9, ypos-210-radius * 1.7*1.6); //2
vertex(0+radius*6.6*0.9, ypos-210-radius * 1.7*1.9); //3
vertex(0+radius*6.6+radius*2.2*0.8, ypos-210-radius * 1.7); //4
vertex(0+radius*2.2/2, ypos-210-radius * 1.7*0.8); //2
endShape(CLOSE);

popMatrix();

// Draw the shape
//ellipse(xpos, ypos, 20, 20);
}

void serialEvent(Serial myPort) {
// read the serial buffer:
String myString = myPort.readStringUntil(‘\n’);
if (myString != null) {
// println(myString);
myString = trim(myString);

// split the string at the commas
// and convert the sections into integers:
int sensors[] = int(split(myString, ‘,’));

// xpos = map(sensors[0], 430, 580, 0, width);
// ypos = map(sensors[1], 30, 200, 0, height);

for (int sensorNum = 0; sensorNum < sensors.length; sensorNum++) {
print(“Sensor ” + sensorNum + “: ” + sensors[sensorNum] + “\t”);
}
// add a linefeed after all the sensor values are printed:
println();

// make sure there are three values before you use them:
if (sensors.length > 1) {
xpos = sensors[0];
carAngle= sensors[1];
xpos = map(sensors[0],35, 200, width +130, 0);
carAngle = map(sensors[1],44,200,0, -6);
// the switch values are 0 and 1. This makes them 0 and 255:
fgcolor = sensors[2] * 255;
}
}
}

Advertisements

Comments are closed.