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 Beschleunigungssensor

22. November 2011

Aufgaben

3.Visualisiert die Werte des Sensors in Processing – zunächst nur als Liniengrafik Material: MMA7455 Breakout 5.Erstellt eine Funktion, die ermittelt, welche Seite des Sensors zur Zeit Oben liegt. Material: MMA7455 Breakout Eine Karton-Box wurde mit einem Bewegungssensor und einem Vibrationselement ausgerüstet. Damit wird in jeder Lage ein unterschiedliches Vibrationsfeedback ausgegeben. Arduino
#include  //Include the Wire library
#include  //Include the MMA_7455 library
#define BUFFER_SIZE 20 // actual size of the buffer for integer values: (numberOfValsToRead*6)+(numberOfValsToRead-1)

const int MOTOR = 11;

MMA_7455 mySensor = MMA_7455(); //Make an instance of MMA_7455

char xVal, yVal, zVal; //Variables for the values from the sensor

char incommingBuffer[BUFFER_SIZE]; // buffer to store incomming values
char incomming; // primary buffer to store single incommning bytes
int incommingCounter = 0; // counter for counting the positions inside the buffer

int firstValue, secondValue, thirdValue; // fourthValue, fifthValue, ... // add more if needed

void setup()
{
  Serial.begin(9600);
  // Set the sensitivity you want to use
  // 2 = 2g, 4 = 4g, 8 = 8g
  mySensor.initSensitivity(2);
  // Calibrate the Offset, that values corespond in
  // flat position to: xVal = -30, yVal = -20, zVal = +20
  // !!!Activate this after having the first values read out!!!
  mySensor.calibrateOffset(14, 25, 0);

  pinMode(MOTOR, OUTPUT);
}

void readSerial()
{
  while(Serial.available())
  {
    incomming = Serial.read(); // read single incommning bytes

    if(incomming != '\r') //if no carriage return is received proceed in reading the serial port
    {
      incommingBuffer[incommingCounter++] = incomming; // go on the next position in the buffer
    }
    else //read until a carriage ('\r') is received
    {
      incommingBuffer[incommingCounter] = '\0'; // set the last byte to NULL to sign it for the string operators

      char *a = strtok(incommingBuffer, ","); // split the string after delimiters into tokens
      //char *b = strtok(NULL, ","); // ...
      //char *c = strtok(NULL, ","); // ...
      //char *d = strtok(NULL, ",.;"); // add another line if needed

      firstValue = atoi(a); // convert the strings into integers
      //secondValue = atoi(b); // ...
      //thirdValue = atoi(c); // ...
      //fourthValue = atoi(d); // add another line if needed

      incommingCounter = 0; // reset the counter
      memset(incommingBuffer, 0, BUFFER_SIZE); //overwrite the incommingBuffer
    }
  }
}

void loop()
{
  readSerial();
  xVal = mySensor.readAxis('x'); //Read out the 'x' Axis
  yVal = mySensor.readAxis('y'); //Read out the 'y' Axis
  zVal = mySensor.readAxis('z'); //Read out the 'z' Axis

  Serial.print(xVal, DEC);
  Serial.print("\t");
  Serial.print(yVal, DEC);
  Serial.print("\t");
  Serial.println(zVal, DEC);

  analogWrite(MOTOR, firstValue);

}
Processing
import processing.serial.*;
Serial myPort;

int xVal, yVal, zVal;

void setup() {
  background(0);
  size(500, 500, P3D);
  println(Serial.list());
  myPort = new Serial(this, Serial.list()[0], 9600);
}

void draw() {
  background(0);
  translate(width/2, height/2);
  rotateX(radians(map(xVal, -64, 64, 0, 180)));
  rotateY(radians(map(yVal, -64, 64, 0, 180)));
  fill(255);
  box(90);

  float triggerPositive = 0.8;
  float triggerNegative = 0.2;

  float xValCorrected = float(xVal)/64;
  float yValCorrected = float(yVal)/64;
  float zValCorrected = float(zVal)/64;

  println(xValCorrected +"," +yValCorrected +"," +zValCorrected);

  if (xValCorrectedtriggerPositive) {
    myPort.write("0\r");
    println("SEITE 1");
  }
  else if (xValCorrectedtriggerPositive && zValCorrected    myPort.write("200\r");
    println("SEITE 2");
  }
  else if (xValCorrected    myPort.write("150\r");
    println("SEITE 3");
  }
  else if (xValCorrectedtriggerPositive && yValCorrected    myPort.write("255\r");
    println("SEITE 5");
  }
  else if (xValCorrected0) {
    String completeString = myPort.readStringUntil(10);
    if (completeString !=null) {
      trim(completeString);
      String seperateValues[] = split(completeString, "\t");
      xVal = int(seperateValues[0]);
      yVal = int(seperateValues[1]);
      zVal = int(seperateValues[2]);
    }
  }
}