I am using this HC-SR04-DistanceSensor. I have wired a 10nF-capacitor between Vcc and GND for decoupling.
seems to work somehow on the first sight, but it outputs me some strange chars in the SerialMonitor.
12
15
20
23
28
30
38
&33
!35
#40
(43
+48
051
353
556
858
:61
=58
:53
551
343
+40
(38
&43
+28
23
20
17
20
17
20
25
30
33
!43
+40
(46
.51
356
also the reading of the distance is not as linear as I try to move my hand over the sensor.
How can I fix that? what causes those chars in the serial-output? is it simply a shitty sensor?
const int trigPin = 9;
const int echoPin = 10;
long duration;
int distance;
int distanceOld;
void readSensor() {
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = duration*0.034/2;
if(distance <= 25 && distanceOld != distance) {
Serial.println(map(distance, 0, 25, 0, 64));
Serial.write(map(distance, 0, 25, 0, 64));
distanceOld = distance;
}
}
void setup() {
// SERIAL
Serial.begin(9600);
while ( !Serial && (millis()<4000) ) {
// ..
}
Serial.println("SERIAL LOADED..");
// SENSOR_PINS
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input
}
void loop() {
readSensor();
delay(30);
}