SparkFun Forums 

Where electronics enthusiasts find answers.

For the discussion of Arduino related topics.
By Ardalan
#198132
Hi there,,

I have made a person counter,

Here is the code:

___________________________________________________________________________
___________________________________________________________________________

#define trigPin1 6
#define echoPin1 7

#define trigPin2 8
#define echoPin2 12

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


// include the library code:
#include <LiquidCrystal.h>

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(11,10,9,2,3,4,5);

void setup() {
Serial.begin(9600);
// set up the LCD's number of columns and rows:
lcd.begin(16, 2);
//Print a message to the LCD.
lcd.print("Persons:");

//sensors
pinMode(trigPin1, OUTPUT);
pinMode(echoPin1, INPUT);
pinMode(trigPin2, OUTPUT);
pinMode(echoPin2, INPUT);

}

void loop() {
// set the cursor to column 0, line 1
lcd.setCursor(0, 1);


//check sensor1
int duration, distance;
digitalWrite(trigPin1, LOW);
delayMicroseconds (2);
digitalWrite (trigPin1, HIGH);
delayMicroseconds (5);
digitalWrite (trigPin1, LOW);
duration = pulseIn (echoPin1, HIGH);
distance = (duration/2) / 29.1;

if(distance < 50){
Serial.println("Entering...");
hit1 = 1;
}

//check sensor2
int duration2, distance2;
digitalWrite(trigPin2, LOW);
delayMicroseconds (2);
digitalWrite (trigPin2, HIGH);
delayMicroseconds (5);
digitalWrite (trigPin2, LOW);

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

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

//entry was triggered, scan exit until exit is hit
while(hit1 == 1 && hit2 == 0){
//check sensor2
int dur, dist;
digitalWrite(trigPin2, LOW);
delayMicroseconds (2);
digitalWrite (trigPin2, HIGH);
delayMicroseconds (5);
digitalWrite (trigPin2, LOW);

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

if(dist < 50){
//reset the two values
hit1 = 0;
hit2 = 0;
numEntries++;
Serial.println("\tENTERED");
break;
}
}


//exit was triggered, scan entry until entry is hit
while(hit1 == 0 && hit2 == 1){
//check sensor2
int dur, dist;
digitalWrite(trigPin1, LOW);
delayMicroseconds (2);
digitalWrite (trigPin1, HIGH);
delayMicroseconds (5);
digitalWrite (trigPin1, LOW);

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

if(dist < 50){
//reset the two values
hit1 = 0;
hit2 = 0;
numExits++;
Serial.println("\tEXITED");
delay(100);
break;
}
}

/* DEBUGGING
Serial.print(distance);
Serial.print("-----");
Serial.print(distance2);
Serial.println();
*/


// print the number of seconds since reset:
lcd.print(numEntries - numExits);

delay(100);
}
___________________________________________________________________________
___________________________________________________________________________

I have one pin left on my Arduino Uno - Pin 13. I want to edit that code to make Pin 13 Output LOW when value of the LCD count "persons" is 0 and Pin 13 Output be HIGH when the result is greater than 0.

Can anyone help me?

Thanks,

Ardalan G.
User avatar
By DanV
#198139
Please use Code Tags (from the Full Editor) around your code so it will read properly.

You are using two blocking methods in you code that will cause problems.
- while() will remain in that while loop until conditions are satisfied - no other code will run until it exits the loop.
- pulsein() will wait (forever, I think) until the input is received. pulsein(pin, value, timeout) will expire upon timeout but still blocks all other code.

But, back to your question, setup the pin_13 in setup(), resolve your numEntries and numExits to a variable (maybe numPersons) or just do the math at the if() test and then something along these lines (put this inside your loop:
Code: Select all
if (numPersons >0)
 	{
	digitalWrite(pin13, HIGH)
	}
else
 	{
	digitalWrite(pin13, LOW)
	}
you should look into non-blocking code.
search for BlinkWithoutDelay and that'll get you started.