5. Dezember 2011
Diese Übung befasste sich mit dem Lagesensor, Vibrationsmotor und selbst gewählten Komponenten. Mit diesen Bauteilen galt es eine Box zu erstellen, welche auf Benutzerinteraktionen reagiert. Ich habe eine art von Blindenwürfel gemacht.#include <Wire.h> #include <MMA_7455.h> #define BUFFER_SIZE 20 #define LED_PIN 11 MMA_7455 mySensor = MMA_7455(); char xVal, yVal, zVal; char oldxVal, oldyVal, oldzVal; 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); mySensor.initSensitivity(2); mySensor.calibrateOffset(16,30,9); pinMode(LED_PIN, OUTPUT); } void loop() { xVal = mySensor.readAxis('x'); xVal = (0.8*oldxVal)+(0.2*xVal); yVal = mySensor.readAxis('y'); yVal = (0.8*oldyVal)+(0.2*yVal); zVal = mySensor.readAxis('z'); zVal = (0.8*oldzVal)+(0.2*zVal); Serial.print(xVal, DEC); oldxVal = xVal; Serial.print('\t'); Serial.print(yVal, DEC); oldyVal = yVal; Serial.print('\t'); Serial.print(zVal, DEC); oldzVal = zVal; Serial.print('\t'); Serial.println(); readSerial(); // read the values available at the serial port analogWrite(LED_PIN, firstValue); } 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 } } }