Hallo HotSystems,
erstmal vielen Dank für deine Antwort.
Laut Datenblatt benötigt der Sensor einen Delay von 50us zwischen den Starts der Messung. Daher hatte ich ein delay zum Ende der Schleife eingefügt. Ich hab die Pause nun erhöht auf 0,5 Sekunden. Es funktioniert nun ist aber nun sehr langsam. Ich verstehe aber nicht wieso die Schleife am Anfang zu schnell läuft und danach wieder normal ? Das Datenblatt ist hier verlinkt:
www.mpja.com
Da steht halt in der Grafik 50us was ich mit 50µs gedeutet habe.
Hier ist nochmal der gesamte Code mit TFT Display:
#include <dht.h>
#include <TFT.h> // Arduino LCD library
#include <SPI.h>
// pin definition for the Uno
#define cs 10
#define dc 9
#define rst 8
#define DHT11_PIN 7
#define trigPin 3
#define echoPin 5
// create an instance of the library
TFT TFTscreen = TFT(cs, dc, rst);
dht DHT;
// char array to print to the screen
char sensorPrintout[6];
float tempalt, humialt, rangealt;
unsigned long time;
void setup() {
Serial.begin(9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
// Put this line at the beginning of every sketch that uses the GLCD:
TFTscreen.begin();
// clear the screen with a black background
TFTscreen.background(0, 0, 0);
// write the static text to the screen
// set the font color to white
TFTscreen.stroke(255, 255, 255);
TFTscreen.line(0, 9, 160, 9);
TFTscreen.line(0, 34, 160, 34);
TFTscreen.line(0, 46, 160, 46);
TFTscreen.line(0, 71, 160, 71);
TFTscreen.line(0, 82, 160, 82);
// set the font size
TFTscreen.setTextSize(1);
// write the text to the top left corner of the screen
TFTscreen.text("Temperatur in Celsius", 0, 0);
TFTscreen.text("Luftfeuchtigkeit in %", 0, 37);
TFTscreen.text("Entfernung in cm", 0, 73);
// ste the font size very large for the loop
TFTscreen.setTextSize(3);
}
void loop() {
String sensorVal;
float duration, distance;
float speed;
float summe=0;
int i=0, h=0, tempera=0;
tempera=DHT.temperature;
while(i<=99){
digitalWrite(trigPin, LOW);
delayMicroseconds(5);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
//speed = 331.4 + (0.606 * tempera);
speed = 331.5 + (0.6 * 20);
distance = (duration / 2) * (speed / 10000);
summe=summe+distance;
h=distance*100;
Serial.println(h);
delay(500);
i++;
}
distance = summe/100;
delay(250);
int chk = DHT.read11(DHT11_PIN);
if(tempalt != DHT.temperature)
{
tempalt=DHT.temperature;
// Read the value of the sensor on A0
sensorVal = String(DHT.temperature);
// convert the reading to a char array
sensorVal.toCharArray(sensorPrintout, 3);
// set the font color
TFTscreen.stroke(0,0,0);
TFTscreen.fill(0,0,0);
TFTscreen.rect(0,11,160,21);
TFTscreen.stroke(255, 255, 255);
// print the sensor value
TFTscreen.text(sensorPrintout, 0, 11);
}
if(humialt != DHT.humidity)
{
humialt=DHT.humidity;
// Read the value of the sensor on A0
sensorVal = String(DHT.humidity);
// convert the reading to a char array
sensorVal.toCharArray(sensorPrintout, 3);
// set the font color
TFTscreen.stroke(0,0,0);
TFTscreen.fill(0,0,0);
TFTscreen.rect(0,48,160,21);
TFTscreen.stroke(255, 255, 255);
// print the sensor value
TFTscreen.text(sensorPrintout, 0, 48);
}
//Serial.println(rangealt-distance);
//Serial.println(distance);
// Read the value of the sensor on A0
sensorVal = String(distance);
// convert the reading to a char array
sensorVal.toCharArray(sensorPrintout, 6);
// set the font color
TFTscreen.stroke(0,0,0);
TFTscreen.fill(0,0,0);
TFTscreen.rect(0,84,160,21);
TFTscreen.stroke(255, 255, 255);
// print the sensor value
TFTscreen.text(sensorPrintout, 0, 84);
}