So, I am tying to figure out how to work with an HC-SR04 ultrasonic sensor so I tryed this easy code:
const int trig = 6;
const int echo = 5;
long totaltime;
int distance;
void setup ()
{
pinMode(trig, OUTPUT);
pinMode(echo, INPUT);
Serial.begin(9600);
}
void loop()
{
digitalWrite(trig, LOW);
delayMicroseconds(2);
digitalWrite(trig, HIGH);
delayMicroseconds (20);
digitalWrite(trig, LOW);
totaltime = pulseIn(echo, HIGH);
distance= totaltime*0.034/2;
Serial.print("Distance:");
Serial.println(distance);
}
But inbetween every read it does it reads a zero, so he is constandly turning off and i can't see the mistake I'm making, so has anyone an idea how to solve this?