running SR04 on an UNO. After 40-45mins, gets stuck...

First off, I am a complete N00B to Arduino. I am making a little sensor that will just detect when something is in front of it. I have it briefly flashing to set the first limit and the second limit. The uno runs PERFECTLY and does exactly as it should, but after about half an our, it’ll just get stuck in some kinda weird mood where it won’t react any more and my LED pin is flashing. I’ve tried different SR04’s different UNO boards. new soldering job, everything. I’ve constructed 5-6 of these now and THEY ALL will crash/freeze/stuck at the same time(about).

Really would appreciate some help on this. PLEASE understand I am a n00b, and my coding is probably as dumb as it can get.

Thank you everybody! This forum has been so full of information! LOVE IT!

UNO-NewPingFinder.ino (3.36 KB)

You are calling loop() inside itself. Although this is not problematic by itself, you have to take care that this recursion ends. In your code there are circumstances where this doesn't happen. If you reach such a status, the Arduino recurses into the loop() tree until the stack overflows. Then it resets in the best case or freezes in the worst case.

As far as I can see there is absolutely no need to call loop() recursively, so just remove the loop() calls inside the loop() routine.

Let me try that!

Also, the way I'm setting my two "defined" walls/limits by using the "while" statement. Is that ok?

And is there a way to clear the cache or what not. I can have it clear the stack or cache or whatever it needs after every loop if necessary.

Here's my latest update:

removed the LOOP()'s inside the loop.

Setup 3 indentical boards running the same sketch.

Figured out 1 thing. If the sensor isn't triggered, it will go into that "stuck mode". i unplug and replug the SR04 just to see if its stuck or not, but the arduino is still stuck.

So my conclusion is, if the arduino doesn't pause for a moment(like how it does with the LED's on), it will eventually get stuck into some frozen blinky mode and no thing happens, even if i powercycle the sensor.

is there a way to fix that? or is this a common problem? I guess I don't know what to search/google for to find myself the answer. Thanks everybody for any insight.

Please post your current code, all if it, inside code tags. Then we can have a look and see what is going on there.

All fixed! Thankd

Please edit your message and add code tags around it (the [/] button in the preview). Not only makes it your code more readable, it also prevents smilies from appearing in it.

One issue I noticed is that you have two variables, wallcount and wallcount2 that get increased, but never decreased or reset. So over time they continue to increase until they overflow and become huge negative numbers. This may be your blocking problem.

Furthermore you make lots of use of delay() calls, which are blocking. Not good coding practice.

Do add comments to your code, especially if you hope to be able to understand your own code a year later.

What exactly do you mean by delay calls that are blocking? I changed the code to be inside the brackets. Sorry

When you call delay() the Arduino is waiting for the delay() to finish without being able to do anything else. It’s blocking.

   if (wall = distance && wall < walllimit) {

   if (wall2 = distance && wall2 < wall) {

I think you want to use == instead of = here.

Then I noticed you measure the distance twice - once using the sonar() function, once using the pulseIn() function after sending a trigger signal. Why the two different ways?

Looking at the code I don’t see other errors, there is a while loop in your code which is a candidate for getting “stuck” simply for being a while loop.

Again without any comments or other explanation I don’t really understand what you’re trying to do with wall, wallcount, wall2, wallcount2 and walllimit. What are they, what is it supposed to do exactly, why is the logic written as it is, etc.

It seems you’re trying to see whether a distance is exactly equal to a certain number. That’s a bad idea. As you use int types there is a fair chance for this to actually be true at times, there is also a great chance you never get that exact reading (due to object moving too fast or the error of the sensor itself). So test for distance being within a range, or less than or greater than a certain number.

You have been a HUGE help!

A couple of things:

I was using the regular non NEWPING method at first, then I decided to start using it and put it inside half the code. Also, the way the WHILE parts are, its so that you can set the two unique limits that will flash different lights. As in limit1(wall1) and limit2(wall2).

I need to initialize that based on when it's booted up and where the limit is, it would change. I am looking for "closer or that limit" to flash zone 1 or zone 2.

I will rework it and clean up some of this stuff.

The main reason why I am not using NEWPING for the beginning part is that it will not spit any values back. I kind of want it to ONLY take the value if it stays consistent for 8 counts. Like if I turn on the unit and i'm moving it around, I don't want it to register random values it will see briefly. So the WHILE parts are really more of a..
Look for a value that is within the limit and if the value is good, count to 8, if anytime in those 8 counts, it changes, restart the count over again.. It's like my own INITIALIZATION kind of.

Does that make sense? I wil post up better code and better comments.

Thanks so much for the help, removing some of those wallcount + 1 for no reason without a reset of the value is the reason why it was crashing.

I REMOVED that, and now it looks VERY promising, I will do more testing in differently scenarios to make sure.

KUDOS!

Good luck with the project, glad to have been able to help.