Arduino Completely Freezes After a Few Minutes

Hi there,

I’m running an Arduino Uno R3 with this code:



#define trigPin1 6
#define echoPin1 7

#define trigPin2 8
#define echoPin2 12

int hit1 = 0, hit2 = 0;
int numEntries = 0, numExits = 0;

#include <LiquidCrystal.h>

LiquidCrystal lcd(11,10,9,2,3,4,5);

void setup() {
Serial.begin(9600);
lcd.begin(16, 2);
lcd.print(“Persons:”);

pinMode(trigPin1, OUTPUT);
pinMode(echoPin1, INPUT);
pinMode(trigPin2, OUTPUT);
pinMode(echoPin2, INPUT);
pinMode(13,OUTPUT);
}

void loop() {
lcd.setCursor(0, 1);
int duration, distance;
digitalWrite(trigPin1, LOW);
delayMicroseconds (60);
digitalWrite (trigPin1, HIGH);
delayMicroseconds (60);
digitalWrite (trigPin1, LOW);
duration = pulseIn (echoPin1, HIGH);
distance = (duration/2) / 29.1;

if(distance < 50){
Serial.println(“Entering…”);
hit1 = 1;
}
int duration2, distance2;
digitalWrite(trigPin2, LOW);
delayMicroseconds (60);
digitalWrite (trigPin2, HIGH);
delayMicroseconds (60);
digitalWrite (trigPin2, LOW);

duration2 = pulseIn (echoPin2, HIGH);
distance2 = (duration2 / 2) / 29.1;

if(distance2 < 50){
Serial.println(“Exitting…”);
hit2 = 1;
}

while(hit1 == 1 && hit2 == 0){
int dur, dist;
digitalWrite(trigPin2, LOW);
delayMicroseconds (60);
digitalWrite (trigPin2, HIGH);
delayMicroseconds (60);
digitalWrite (trigPin2, LOW);

dur = pulseIn (echoPin2, HIGH);
dist = (dur / 2) / 29.1;

if(dist < 50){
hit1 = 0;
hit2 = 0;
numEntries++;
Serial.println("\tENTERED");
delay(250);
break;
}
}

while(hit1 == 0 && hit2 == 1){
int dur, dist;
digitalWrite(trigPin1, LOW);
delayMicroseconds (60);
digitalWrite (trigPin1, HIGH);
delayMicroseconds (60);
digitalWrite (trigPin1, LOW);

dur = pulseIn (echoPin1, HIGH);
dist = (dur / 2) / 29.1;

if(dist < 50){
hit1 = 0;
hit2 = 0;
numExits++;
Serial.println("\tEXITED");
delay(250);
break;
}
}
lcd.print(numEntries - numExits);

if(numEntries - numExits > 0) digitalWrite(13,HIGH); else digitalWrite(13,LOW);

/*lcd.begin(16,2);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(“Persons:”);
lcd.print(numEntries - numExits);
*/

delay(50);
}



It counts the number of people in the room and turns on a light accordingly.

It works great except it just freezes after I pass through the sensors a few times. Sometimes only 3 times before it freezes, sometimes 20.

What do I need to do to fix this problem?

Thanks in advance.

What do I need to do to fix this problem?

A better power supply possibly.
Better code factoring maybe.
Some debug prints wouldn't hurt.

CtrlAltElite:
A better power supply possibly.
Better code factoring maybe.
Some debug prints wouldn't hurt.

Code tags.

So. The culprit was found. If I trigger both the ultrasound sensors at the same time.

Then it will not work because they are both waiting for the other one to become triggered.

How do I fix this?

How do I incorporate a line of code that will reset the code if the second sensor is not triggered within 2 seconds of the first.

Ardalan:
How do I incorporate a line of code that will reset the code if the second sensor is not triggered within 2 seconds of the first.

You're thinking like code that tells a story. Think like code that defines a checklist.

Story code goes, if this happens then go over here and wait for this to happen and then go over here and do this and wait for this other thing, then reset this so we go back to this other part of the story.

Checklist code works more like a check list that at any given time can tell you what the next step is and take it. Let the loop keep turning over and over.

You haven't made it very clear to me yet what this thing is supposed to do in the end. If you'd fix that I might could help you see how to lay it out better.

Thanks for offering to help Delta_G.

I need this written in code:


If "distance" is less than 50 but "distance2" does not become less than 50 within 1.5 seconds. Then stop and restart from the beginning of the loop.


and the other way around too:

If "distance2" is less than 50 but "distance" does not become less than 50 within 1.5 seconds. Then stop and restart from the beginning of the loop.


I hope that can make sense.

This is a good candidate for a state machine. Minor nightmare with "if" and "but" statements.

You're still describing code. Start by describing what it should do. For example, this code should count people walking into the room. It should count up one if this sensor is triggered and then that one within 1.5 seconds. It should count down if the second sensor is triggered first and then the first in 1.5 seconds. Is that about right?

There will be no "restarting" of the loop when we're done. When you get this right the loop function will be going over and over thousands of times a second. You'll never stop or start it anywhere.

I see a state machine with three states like:

IDLE - nothing happening read both sensors. If first sensor is tripped go to GOING_IN state. If second sensor is tripped go to GOING_OUT state.
GOING_IN - First sensor triggered, get the time. On each pass of loop compare with the current time to see if it has timed out. Check for signal on second sensor. If you get the second sensor then go back to IDLE.
GOING_OUT - Just like going in but we get here if the second sensor is triggered while in IDLE mode.

Now this can get you started:

// let's not confuse things with enum just yet.  That's next
#define IDLE 0
#define GOING_IN 1
#define GOING_OUT 2

int state = 0;
unsigned long tripTime = 0;

void loop(){

   if(state == IDLE){
        // read both sensors and see if we need to go to GOING_IN or GOING_OUT
        if(sensor 1 is tripped){
             trippedTime = millis();
             state = GOING_IN;
        }
        if(sensor 2 is tripped){
            trippedTime = millis();
             state = GOING_OUT;
    }
    else if (state == GOING_IN){
            if(millis() - tripTime >= timeOutInterval){
                 // Timed out, go back to IDLE
                 state = IDLE;
            }
             if (second sensor is triggered) {
                    // add one to the people count and go back to IDLE
                    state = IDLE;
            }
    }
     // You can write the GOING_OUT state.  It looks a lot like GOING_IN

.?.? I thought a picture would help.

I got my Arduino about 4 days ago so I am really confused right now.

Picture Attached.

If the right sensor is triggered (distance<50) first and then the left one, then someone has entered the room.
If the left sensor is triggered (distance2<50 first and then the right one, then someone has left the room.

Circuit Diagram Attached.

The problem in the code is that if both sensors are triggered at the same time, the whole device stops working because there is no time limit between the two triggers.

I need to add a line that will tell the arduino to “forget” for lack of a better word about the last trigger if the second trigger doesn’t happen within 1.5 seconds.

Or any other method to fix the freezing issue.

I know I’m basically asking someone to do it for me but it’s not because I am lazy. I literally don’t understand it. After seeing how to do this. I will definitely understand it better.

Thanks all.

Ardalan

You have tackled a problem that is very difficult for a beginner, so expect to be frustrated.

We recommend that beginners start with simpler projects, so they can learn the language, the special features of the Arduino and real time programming. Learn to blink an LED without using delay(), for example. Using delay(), by the way, is part of your problem.

Unless this is a school assignment due shortly (in which case you won't find forum members very willing to help), put it off until you have gained some experience. Learn to crawl before trying to run.

Is it safe for duration be int? Return is in microseconds from pulsein().

Ardalan:
it's not because I am lazy. I literally don't understand it.

I got my Arduino about 4 days ago

Maybe you've jumped in a little too deep. Perhaps you would do well to do a little learning on some simpler projects before you jump into something like this. I've shown you the logic, but if you don't understand anything about the coding language you're just going to languish in frustration.

Hi,
OPs images,
0ab394b066ab90cafebaebbdd5648179f1727b43.jpg
87996ec44ad10609b42acbf64d4ffcc719c8874d.jpg
Tom… :slight_smile:

Delta_G:
You're thinking like code that tells a story. Think like code that defines a checklist.

I like that analogy, I have not seen it before.

Story telling is usually looking backwards to describe something that has happened (or is imagined to have happened).

A checklist is normally a forward-looking process - where are we now and what do we need to do next.

...R