Hello all,
im wrestling with a question which for me is higher mathematics. The problem:
Ive got a square. On each corner, I place a Piezo element. These corners are named as followed:
Left upper side corner : A
Left down side corner: B
Right down side corner: C
Right upper side corner: D
Case: Im trowing a ball on a random place on this square and i want processing to do the calculations for me, where the X and Y-axes are.
In Arduino I got the following code:
int firstSensor = 0; // first analog sensor
int secondSensor = 0; // second analog sensor
int thirdSensor = 0; // third analog sensor
int fourthSensor = 0; // fourth analog sensor
int inByte = 0; // incoming serial byte
void setup()
{
// start serial port at 9600 bps:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}
establishContact(); // send a byte to establish contact until receiver responds
}
void loop()
{
// if we get a valid byte, read analog ins:
if (Serial.available() > 0) {
// get incoming byte:
inByte = Serial.read();
firstSensor = analogRead(A1);
secondSensor = analogRead(A2);
thirdSensor = analogRead(A3);
fourthSensor = analogRead(A4);
// send sensor values:
Serial.write(firstSensor);
Serial.write(secondSensor);
Serial.write(thirdSensor);
Serial.write(fourthSensor);
}
}
void establishContact() {
while (Serial.available() <= 0) {
Serial.print('A'); // send a capital A
delay(300);
}
}
in Processing ive got the following code:
import processing.serial.*;
int bgcolor; // Background color
int fgcolor; // Fill color
Serial myPort; // The serial port
int[] serialInArray = new int[4]; // Where we'll put what we receive
int serialCount = 0; // A count of how many bytes we receive
float A,B,C,D;
float xPos = 0;
float yPos = 0;
float totaal = 0;
float totaalABC = 0;
float totaalBCD = 0;
float lengte = 30.7;
float breedte = 29.7;
float A1 = 0;
float B1 = 0;
float C1 = 0;
float D1 = 0;
float AB = 0;
float BC = 0;
float CD = 0;
float BD = 0;
boolean firstContact = false; // Whether we've heard from the microcontroller
void setup() {
size(256, 256); // Stage size
noStroke(); // No border on the next thing drawn
// Print a list of the serial ports, for debugging purposes:
println(Serial.list());
// I know that the first port in the serial list on my mac
// is always my FTDI adaptor, so I open Serial.list()[0].
// On Windows machines, this generally opens COM1.
// Open whatever port is the one you're using.
String portName = Serial.list()[7];
myPort = new Serial(this, portName, 9600);
}
void draw() {
totaal = A+B+C+D; // Maximum output
totaalABC = AB+BC;
totaalBCD = CD+BD;
// Alles in verhouding tot elkaar zetten
A1 = A/totaal;
B1 = B/totaal;
C1 = C/totaal;
D1 = D/totaal;
// Voorbereiding voor de stelling van Pythagoras
AB = A+B;
BC = B+C;
CD = C+D;
BD = B+C;
xPos = lengte*totaalABC/totaalABC;
yPos = breedte*totaalBCD/totaalABC;
}
void serialEvent(Serial myPort) {
// read a byte from the serial port:
int inByte = myPort.read();
// if this is the first byte received, and it's an A,
// clear the serial buffer and note that you've
// had first contact from the microcontroller.
// Otherwise, add the incoming byte to the array:
if (firstContact == false) {
if (inByte == 'A') {
myPort.clear(); // clear the serial port buffer
firstContact = true; // you've had first contact from the microcontroller
myPort.write('A'); // ask for more
}
}
else {
// Add the latest byte from the serial port to array:
serialInArray[serialCount] = inByte;
serialCount++;
// If we have 3 bytes:
if (serialCount > 3 ) {
A = serialInArray[0];
B = serialInArray[1];
C = serialInArray[2];
D = serialInArray[3];
// print the values (for debugging purposes only):
println(xPos,yPos);
// println(A + "\t" + B + "\t" + C + "\t" + D);
// Send a capital A to request new sensor readings:
myPort.write('A');
// Reset serialCount:
serialCount = 0;
}
}
}
On top of the Processing code i defined the width and height of the table so I can use a scale of the total combined outcome of the Piezo elements. I did this so I can calculate it back to the measurements of the table we gonna use in the end.
I know I declared a lot of floats (variables) which i don't use, don't worry about them, I can always remove them.
Any help is appreciated !
Cheers Tommie