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.

Drehspiel

24. November 2011

Dreht man die Schachtel in der richtigen Reihenfolge, fängt sie an zu vibrieren. Zudem zeigen LED an ob man eine richtige Seite erwischt hat. Falls man sie falsch herumdreht, erlischen die LED und man muss neu Anfangen. Als Übung wurde von Arduino Daten (Accelerometer) via Processing geschickt und dann wieder zurück (LED) geschickt. Processing Code:
import processing.serial.*;
Serial myPort;

boolean led1, led2, led3;

int xVal, yVal, zVal;
int xPos = 0;
float zRot, yRot;
boolean q1, q2, q3, q4;

void setup() {
  background(0);
  size(500, 500, P3D);
  println(Serial.list());
  myPort = new Serial(this, Serial.list()[1], 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(80, 120, 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);

  //seitenabfrage via accelerometer
  if (xValCorrected<triggerNegative && yValCorrected<triggerNegative && zValCorrected>triggerPositive) {
    println("seite 1 ist oben");
  }
  if (xValCorrected<triggerNegative && yValCorrected>triggerPositive && zValCorrected<triggerNegative) {
    println("seite 2 ist oben");
    led2=true;

    if (!led1) {
      led1 = false;
      led2 = false;
      led3 = false;
    }
  }

  if (xValCorrected<triggerNegative && yValCorrected<(-triggerPositive) && zValCorrected<triggerNegative) {
    println("seite 3 ist oben");
    led1 = false;
    led2 = false;
    led3 = false;
  }
  if (xValCorrected<(-triggerPositive) && yValCorrected<triggerNegative && zValCorrected<triggerNegative) {
    println("seite 4 ist oben");

    if (!led1 || !led2) {
      led1 = false;
      led2 = false;
      led3 = false;
    }
    else
      led3=true;
  }
  if (xValCorrected>triggerPositive && yValCorrected<triggerNegative && zValCorrected<triggerNegative) {
    println("seite 5 ist oben");
    led1 = true;
  }
  if (xValCorrected<triggerNegative && yValCorrected<triggerNegative && zValCorrected<(-triggerPositive)) {
    println("seite 6 ist oben");
    led1 = false;
    led2 = false;
    led3 = false;
  }

  //abfragen, ob die Reihenfolge stimmt
  if (led1) {
    myPort.write(str(255) +"," +str(0) +"," +str(0) +"," +str(0) +"\r");  //send data to arduino
    //println("led 1 Blink!");
    if (led2) {
      myPort.write(str(255) +"," +str(255) +"," +str(0) +"," +str(0) +"\r");  //send data to arduino
      //println("led 2 Blink!");
      if (led3) {
        //println("led 3 Blink!");

        //einfach etwas als Finales machen
        myPort.write(str(255) +"," +str(255) +"," +str(255) +"," +str(255) +"\r");  //send data to arduino
        delay(500);
        myPort.write(str(255) +"," +str(0) +"," +str(0) +"," +str(255) +"\r");  //send data to arduino
        delay(500);
        myPort.write(str(255) +"," +str(255) +"," +str(0) +"," +str(255) +"\r");  //send data to arduino
        delay(500);
        myPort.write(str(255) +"," +str(255) +"," +str(255) +"," +str(255) +"\r");  //send data to arduino
        delay(500);
        myPort.write(str(0) +"," +str(0) +"," +str(0) +"," +str(255) +"\r");  //send data to arduino
        delay(500);
        myPort.write(str(255) +"," +str(255) +"," +str(255) +"," +str(0) +"\r");  //send data to arduino
        delay(100);
        myPort.write(str(255) +"," +str(255) +"," +str(255) +"," +str(255) +"\r");  //send data to arduino
        delay(100);
        myPort.write(str(0) +"," +str(0) +"," +str(0) +"," +str(0) +"\r");  //send data to arduino
        delay(100);
        myPort.write(str(255) +"," +str(255) +"," +str(255) +"," +str(255) +"\r");  //send data to arduino
        delay(100);
        myPort.write(str(0) +"," +str(0) +"," +str(0) +"," +str(0) +"\r");  //send data to arduino
        delay(100);
        myPort.write(str(255) +"," +str(255) +"," +str(255) +"," +str(255) +"\r");  //send data to arduino
        delay(100);
        myPort.write(str(0) +"," +str(0) +"," +str(0) +"," +str(0) +"\r");  //send data to arduino
        delay(100);
        myPort.write(str(255) +"," +str(255) +"," +str(255) +"," +str(255) +"\r");  //send data to arduino
        delay(100);
        myPort.write(str(0) +"," +str(0) +"," +str(0) +"," +str(0) +"\r");  //send data to arduino

        led1 = false;
        led2 = false;
        led3 = false;

        println("win!");
      }
    }
  }
  else {
    myPort.write(str(0) +"," +str(0) +"," +str(0)+"\r");  //send data to arduino
  }
}

void serialEvent(Serial myPort) {
  if (myPort.available()>0) {
    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]);
    }
  }
}
ARDUINO CODE:
#include <Wire.h> //Include the Wire library
#include <MMA_7455.h> //Include the MMA_7455 library
#define BUFFER_SIZE 20 // actual size of the buffer for integer values: (numberOfValsToRead*6)+(numberOfValsToRead-1)

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; // fourthValue, fifthValue, ... // add more if needed

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

char xVal, yVal, zVal; //Variables for the values from the sensor
char prevxVal=0, prevyVal=0, prevzVal=0;
float smoothPrev = 0.8;
float smoothNew = 0.2;

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(20, 35, 9);

  pinMode(9, OUTPUT);  //led
  pinMode(10, OUTPUT);  //led
  pinMode(11, OUTPUT);  //led
  pinMode(3, OUTPUT);  //vibrator
}

void loop()
{
  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

  xVal = (smoothPrev * prevxVal) + (smoothNew * xVal);
  yVal = (smoothPrev * prevyVal) + (smoothNew * yVal);
  zVal = (smoothPrev * prevzVal) + (smoothNew * zVal);

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

  prevxVal = xVal;
  prevyVal = yVal;
  prevzVal = zVal;

  readSerial(); // read the values available at the serial port
  analogWrite(9,firstValue);
  analogWrite(10,secondValue);
  analogWrite(11,thirdValue);
  analogWrite(3,fourthValue);
}

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 (untill 'println')
    {
      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
    }
  }
}