11 Gestensteuerung
Gestensteuerung
Aufgaben
1.Fügt dem Arduino Sketch in welchem ihr den MMA7455 auslest eine Funktion hinzu, welche die Werte glättet.
Material: MMA7455 Breakout
2.Übergebt die Werte des Sensors an Processing
Material: MMA7455 Breakout
3.Visualisiert die Werte des Sensors in Processing – zunächst nur als Liniengrafik
Material: MMA7455 Breakout
4.Erstellt eine Figur in Processing, welche auf eure Gesten reagiert
Material: MMA7455 Breakout
5.Erstellt eine Funktion, die ermittelt, welche Seite des Sensors zur Zeit Oben liegt.
Material: MMA7455 Breakout
import processing.serial.*; // Import the Processing Serial Library for communicating with arduino
Serial myPort; // The used Serial Port
int xVal, yVal, zVal; // fourthValue, fifthValue, ... // add more if needed
int xPosition = 0;
float xRotation, yRotation;
void setup()
{
background(0);
size(500, 500, P3D);
println(Serial.list()); // Prints the list of serial available devices (Arduino should be on top of the list)
myPort = new Serial(this, Serial.list()[0], 9600); // Open a new port and connect with Arduino at 9600 baud
}
void draw()
{
background(0);
translate(width/2, height/2);
float xValCorrected = float(xVal)/64;
float yValCorrected = float(yVal)/64;
float zValCorrected = float(zVal)/64;
//float xRotation = atan((xValCorrected)/(sqrt(sq(yValCorrected)+sq(zValCorrected))));
//float yRotation = atan((yValCorrected)/(sqrt(sq(xValCorrected)+sq(zValCorrected))));
if(zValCorrected>0)
{
xRotation = radians(map(xVal, -64, 64, 0, 180));
yRotation = radians(map(yVal, -64, 64, 180, 0));
}
else
{
xRotation = radians(map(xVal, -64, 64, 180, 0));
yRotation = radians(map(yVal, -64, 64, 0, 180));
}
rotateX(xRotation);
rotateY(yRotation);
fill(255);
box(80);
//println(xVal+"\t"+yVal+"\t"+zVal+"\t");
//println(xValCorrected+"\t"+yValCorrected+"\t"+zValCorrected+"\t");
//println(xRotation+"\t"+yRotation);
}
void serialEvent(Serial myPort) // Is called everytime there is new data to read
{
if (myPort.available() > 0)
{
String completeString = myPort.readStringUntil(10); // Read the Serial port until there is a linefeed/carriage return
if (completeString != null) // If there is valid data insode the String
{
trim(completeString); // Remove whitespace characters at the beginning and end of the string
String seperateValues[] = split(completeString, "\t"); // Split the string everytime a delimiter is received
xVal = int(seperateValues[0]);
yVal = int(seperateValues[1]);
zVal = int(seperateValues[2]);
}
}
}
Links
Beispiel auf bildr.com
Beispiel auf der ITP Website
App Note Freescale