Code update for Motion Sensor Face Warp Project
After a few test i have seen that the Serial Monitor of the Arduino was sometime wrong and just had wrong data.
To kill that for a fluetly installation, i updated the code and inserted a filter. Here is the updated code for the Arduino with comments:
#define trigPin 12
#define echoPin 13
float looper,zwischenspeicher; //global cache & Looper (for init the programm with zero at startup)
void setup() {
Serial.begin (115200 );
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
looper=0; //Init with zero
zwischenspeicher=0; //Init cache with zero
}
void loop() {
float duration, distance;
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
if (looper == 0){
zwischenspeicher = (duration / 2) * 0.0344;
}// save first messure into the cahe
else {
zwischenspeicher = (zwischenspeicher +( (duration / 2) * 0.0344))/2;
} // addition of the actuall messuringpoint to the chache and divide by two
looper++; // Looper is going up 1 step
if (looper >= 100){ //if 100 times messured
distance = zwischenspeicher; //cache is distance
looper=0; // reset looper
zwischenspeicher=0; // reset cache
Serial.println(distance); // output
}
delay(5); // delay shorter for for output
}
Now it looks more like this: