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();
}
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"
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();
}