Willkommen auf unserem Seminar-Blog

Immer auf dem aktuellen Stand bleiben

Dieser Seminar-Blog befindet sich noch im Aufbau und wird in den kommenden Tagen entsprechend verfeinert.

Member Login

Lost your password?

Registration is closed

Sorry, you are not allowed to register by yourself on this site!

You must either be invited by one of our team member or request an invitation by email at viad.info {at} zhdk {dot} ch.

11 Accelerometer

24. November 2011

In dieser Übung lernten wir den Umgang mit dem Beschleunigungssensor. Hierfür haben wir in Processing ein kleines Programm geschrieben, welches die verschiedenen Werte sichtbar machte. Arduino Code

import processing.serial.*;  // Import the Processing Serial Library for communicating with arduino
Serial myPort;               // The used Serial Port

int xnewVal, ynewVal, znewVal; // fourthValue, fifthValue, ... // add more if needed
int xPosition;

void setup()
{
size(500, 500);
background(0);
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()
{
if(xPosition >= width)
{
xPosition = 0;
background(0);
}
else
{
stroke(255,0,0);
strokeWeight(3);
point(xPosition, xnewVal+(height/2));
stroke(0,255,0);
strokeWeight(3);
point(xPosition, ynewVal+(height/2));
stroke(0,0,255);
strokeWeight(3);
point(xPosition, znewVal+(height/2));
xPosition ++;
}
println(xnewVal+"\t"+ynewVal+"\t"+znewVal);
}

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
xnewVal = int(seperateValues[0]);
ynewVal = int(seperateValues[1]);
znewVal = int(seperateValues[2]);
}
}
}

  Processing Code

import processing.serial.*;  // Import the Processing Serial Library for communicating with arduino
Serial myPort;               // The used Serial Port

int xnewVal, ynewVal, znewVal; // fourthValue, fifthValue, ... // add more if needed
int xPosition;

void setup()
{
size(500, 500);
background(0);
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()
{
if(xPosition >= width)
{
xPosition = 0;
background(0);
}
else
{
stroke(255,0,0);
strokeWeight(3);
point(xPosition, xnewVal+(height/2));
stroke(0,255,0);
strokeWeight(3);
point(xPosition, ynewVal+(height/2));
stroke(0,0,255);
strokeWeight(3);
point(xPosition, znewVal+(height/2));
xPosition ++;
}
println(xnewVal+"\t"+ynewVal+"\t"+znewVal);
}

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
xnewVal = int(seperateValues[0]);
ynewVal = int(seperateValues[1]);
znewVal = int(seperateValues[2]);
}
}
}