Distance sensor without delay

Hi!!
I´ve got an Arduino Uno R3, and a HC-sr04 ultrasonic distance sensor. It works perfectly using delays. Gives me the exact distance. But when I tried to do it without delay, the first read on the serial monitor is ok, but the next ones are all 0. If I shut the serial monitor and reopen it again, it just give the distance but only once, and then all ZEROS.
.
Has anyone know what is the problem??
Thank you!!

#define ECHO 11   // Pin received echo pulse
#define TRIGGER 12  // Pin sends shot pulse
 
unsigned long time; // the time ECHO is HIGH
int distance = 0; // the distance in cms
unsigned long previousMicros = 0; // will sotre last time TRIGGER was updated
long OnTime = 10; //microseconds of on-time
long OffTime = 2; //microseconds of off-time
unsigned long viewTime = 200; 
unsigned long previousMillis = 0; //will sotre last time viewTime was updated
int echoState = LOW; // echoState used to set it up

void setup() 
{
  Serial.begin(9600); 
  pinMode(ECHO, INPUT);
  pinMode(TRIGGER, OUTPUT);
}

void view()
{ //  pc  data views
  Serial.print(distance);
  Serial.println("  cm");
  // chech to see if it´ time to change the state 
  unsigned long currentMillis = millis();
  if (currentMillis - previousMillis >= viewTime)
  {
   previousMillis = currentMillis; // remember the time
  }
}

void loop()
{
  // check to see if it's time to change the state of the TRIGGER
  unsigned long currentMicros = micros();
  if((echoState == LOW) && (currentMicros - previousMicros >= OffTime))
   {
     echoState = HIGH; // turn it on
     previousMicros = currentMicros; // remember the time
     digitalWrite(ECHO, echoState); // update the actual echo
   }
   else if((echoState == HIGH) && (currentMicros - previousMicros >= OnTime))
   {
     echoState = LOW; // turn it on
     previousMicros = currentMicros; // remember the time
     digitalWrite(ECHO, echoState); // update the actual echo
   }
   time = pulseIn(ECHO, HIGH);
   distance = time/58;
   view();
}

let's see the code that works with delay.

Here is the code with delay

#define ECHO 11        
#define TRIGGER 12     
 
unsigned int time;
int distance = 0;


 
void setup() {
  Serial.begin(9600); 
  pinMode(ECHO, INPUT);
  pinMode(TRIGGER, OUTPUT);
  
}
void view(){ 
  Serial.print(distance);
  Serial.println(" cm");
  delay(200);
} 
void loop(){
  digitalWrite(TRIGGER, LOW);
  delayMicroseconds(2);
  digitalWrite(TRIGGER, HIGH);
  delayMicroseconds(10);
  digitalWrite(TRIGGER, LOW);
  
  time = pulseIn(ECHO, HIGH);
  
  distance = time/58;
  
  view();
}
Here is the code with delay

How about some code that actually compiles?

i

WTF?

Matching curly braces are not only neat, they are the law.

In your code without delay, you are not toggling the trigger pin high and low.

the "i" was a mistake, I have to copy and replace somethings, and the } at the end was missed, but that code works fine, the problem is the first one, "without delay"

Thank you cattledog
it works now, what a mistake!! 8) 8)
it takes to long (2 seconds) between one number and the other

but now I have these readings, I keep checking. Than you again

6 cm
0 cm
12 cm
0 cm
12 cm
0 cm
12 cm
0 cm
85 cm
0 cm
0 cm
0 cm
216 cm
0 cm
11 cm
0 cm
11 cm
0 cm
11 cm
0 cm
11 cm
0 cm
11 cm
0 cm
11 cm
0 cm

You might want to look at the NewPing library - interrupt driven, so no calls to (blocking functions like) pulseIn().

Thank you Pauls

I have added timeout (optional): the number of microseconds to wait for the pulse to start; default is one second (unsigned long)
Syntax

pulseIn(pin, value)
pulseIn(pin, value, timeout)

It now shows the values very fast, but still have the ZERO reading problem.

Please post your current code.

There is more to this project than just replacing delay() with millis-oldtime>=interval timers. Your will need to give some thought to whether or not the trigger sequence is completed or if the echo has been returned before another trigger pulse is issued.

The delay structure to the program makes sure that a pulse is triggered and then looks for the echo.

If you don't have the correct states for the trigger and the echo you may be looking for an echo before the pulse is actually triggered, and since pulsein is blocking you will get no return.

Is this a school assignment? If so, keep the structure your teacher wants, but be careful about the states.

If you are doing an independent project, then As Paul says, take a look at New Ping library, or try and write the pin change interrupt code yourself. The pulseIn function maybe a bigger problem for blocking in this program than the 12 microseconds of trigger delay.

It is an independent project, I am making a self balanced robot, objects avoiding, so I need no interruptions in the code.
This is the first time I work without delays, and it isn't easy.
I will try to have a read on Timers. But you are right,

Here is the code now

#define ECHO 11        // Pin received echo pulse
#define TRIGGER 12     // Pin sends shot pulse
 
unsigned long time; // the time ECHO is HIGH
int distance; // the distance in cms
unsigned long previousMicros = 0; // will sotre last time TRIGGER was updated
long OnTime = 10; //microseconds of on-time
long OffTime = 2; //microseconds of off-time
unsigned long viewTime = 200; 
unsigned long previousMillis = 0; //will store last time viewTime was updated
int triggerState = LOW; // triggerState used to set it up

void setup() 
{
  Serial.begin(9600); 
  pinMode(ECHO, INPUT);
  pinMode(TRIGGER, OUTPUT);
}

void view()
{ //  pc  data views
  Serial.print(distance);
  Serial.println("  cm");
  // chech to see if it´ time to change the state 
  unsigned long currentMillis = millis();
  if (currentMillis - previousMillis >= viewTime)
  {
   previousMillis = currentMillis; // remember the time
  }
}

void loop()
{
  // check to see if it's time to change the state of the TRIGGER
  unsigned long currentMicros = micros();
  if((triggerState == LOW) && (currentMicros - previousMicros >= OffTime))
   {
     triggerState = HIGH; // turn it on
     previousMicros = currentMicros; // remember the time
     digitalWrite(TRIGGER, triggerState); // update the actual trigger
   }
   else if((triggerState == HIGH) && (currentMicros - previousMicros >= OnTime))
   {
     triggerState = LOW; // turn it off
     previousMicros = currentMicros; // remember the time
     digitalWrite(TRIGGER, triggerState); // update the actual trigger
   }
   time = pulseIn(ECHO, HIGH , 1000);
   distance = time/58;
   view();
}

I appreciate your help!!