Infrared counter plus and minus

Hi , Im new to programming. I have a sketch for infrared counter but only for single sensor. I would like to have two so one will add up and another for deduction. Means i could measure how many ppl in a room at the time of being and how many has went out. Ive tried a few times but fails . Thank you in advance.

counter.ino (1.06 KB)

if (sensB = HIGH)

Whoops

opps sorry thats the wrong sketch..i might have been upload the wrong file...this one ive played around with and it fails ...Ill get the correct one

// IR Obstacle Collision Detection Module

int LED = 13; // Use the onboard Uno LED
int isObstaclePin = 7; // This is our input pin
int isObstacle = HIGH; // HIGH MEANS NO OBSTACLE

void setup() {
pinMode(LED, OUTPUT);
pinMode(isObstaclePin, INPUT);
Serial.begin(9600);
}

void loop() {
isObstacle = digitalRead(isObstaclePin);
if (isObstacle == LOW) {
Serial.println("270 London");
digitalWrite(LED, HIGH);
} else {
Serial.println("No Data");
digitalWrite(LED, LOW);
}
delay(200);
}

Wrong program again

Finally…apologize for my silly mistake

conterex.ino (868 Bytes)

Now no program
I wonder where this is all leading.

as attached (conterex.ino). anyway here it is

// IR Increament and Decreament Counter
int ledPin = 13; // Use the onboard Uno LED
int switchPin = 7; // This is our input pin
int val = 0; // variable for reading the pin status
int counter = 0;
int currentState = 0;
int previousState = 0;

void setup() {
pinMode(ledPin, OUTPUT); // declare LED as output
pinMode(switchPin, INPUT); // declare pushbutton as input
Serial.begin(9600);
}

void loop(){
val = digitalRead(switchPin); // read input value
if (val == HIGH) { // check if the input is HIGH (button released)
digitalWrite(ledPin, HIGH); // turn LED on
currentState = 1;
}
else {
digitalWrite(ledPin, LOW); // turn LED off
currentState = 0;
}
if(currentState != previousState){
if(currentState == 1){
counter = counter + 1;
Serial.println("Onboard");
Serial.println (counter);
}
}
previousState = currentState;
delay(250);
}

The latest code that you posted seems only to deal with one input and from the names of the variables seems to be related to reading the input from a switch or pushbutton

...and it lacks code tags.

yes. from there i have try to adjust to have two inputs and plus when onboard and minus when offload .. but it fails .
This is what i think

int switchPin2 = 8; //add another input on port 8

but then i was lost ..dont know how to mix them together

as attached (conterex.ino). anyway here it is

Clearly, the code was small enough to post inline, so quit wasting your time attaching code that can be posted inline.

And start using code tags. If you can't be bothered to read the stickies to learn how to post properly, why should any of us be bothered trying to help you?

Before getting into the programming let's deal with an obvious question. Do people enter past one sensor and exit past another ?

but then i was lost ..dont know how to mix them together

You've got two hands. Put them on the table. I've got a baseball bat. Can you tell when I bash your left hand? Can you tell when I bash your right hand?

I'll bet that you can.

Can you scream in agony when I bash your left hand? Can you hop up and down every time I bash your right hand?

I'll bet that you can.

Doing one thing when one input BECOMES HIGH (or LOW, as appropriate), and doing something else when another thing BECOMES HIGH (or LOW, as appropriate), is pretty simple.

Of course, you'll have problems with people leaving by the in door, and entering by the out door, unless you have appropriate measures in place to prevent that.

You'll have problems with people poking there heads in the room, being counted as entering, and then backing out, without being counted as leaving.

Five minutes with Mr. Google would have told you all about the pitfalls of your design, which many people have tried - none as successful as they would have liked.

yes one will be the entrance door and another on the exit door ..it should minus the number collected by the first sensor

I suggest that the first thing you do us to rename the variables in the single sensor sketch to make them relevant to using an IR sensor and when you do give each of them a suffix of zero

For instance, rename switchPin to IRPin0 and so on. Do that then post your code here using code tags when you do. Read read this before posting a programming question if you do not know how to use code tags

Im not sure how to do it in the loop, the calculation and display .

// IR Increament and Decreament Counter
int ledPin = 13; // Use the onboard Uno LED
int IRpin0 = 7; // This is our input pin
int IRpin1 = 8; // This is our input pin
int val = 0; // variable for reading the pin status
int counter = 0;
int currentState = 0;
int previousState = 0;

void setup() {
pinMode(ledPin, OUTPUT); // declare LED as output
pinMode(IRpin0, INPUT); // declare pushbutton as input
pinMode(IRpin1, INPUT);
Serial.begin(9600);
}

void loop(){
val = digitalRead(IRpin0); // read input value
if (val == HIGH) { // check if the input is HIGH (button released)
digitalWrite(ledPin, HIGH); // turn LED on
currentState = 1;
}
else {
digitalWrite(ledPin, LOW); // turn LED off
currentState = 0;
}
if(currentState != previousState){
if(currentState == 1){
counter = counter + 1;
Serial.println("Onboard");
Serial.println (counter);
}
}
previousState = currentState;
delay(250);
}
int IRpin0 = 7; // This is our input pin
int IRpin1 = 8; // This is our input pin

The comments are useless, and distracting. Using In and Out as the suffixes makes more sense than 0 and 1.

When you read that that In pin has changed state, to whatever "someone is going by" means, then you increment the number of people in the room. When you read that the Out pin has changed state, to whatever "someone is going by" means, then you decrement the number of people in the room.

You can NOT have one current state and one previous state for two pins, and have any kind of hope of knowing when one of the pins changes state.

So, more copy and pasting, and renaming, but with In and Out suffixes, not 0 and 1.

// IR Increament and Decreament Counter
int ledPin = 13; // Use the onboard Uno LED
int IRpinIN = 7; // This is our input pin IN
int IRpinOUT = 8; // This is our input pin OUT
int val = 0; // variable for reading the pin status
int counter = 0;
int currentState = 0;
int previousState = 0;

void setup() {
pinMode(ledPin, OUTPUT); // declare LED as output
pinMode(IRpinIN, INPUT); // declare pushbutton as input
pinMode(IRpinOUT, INPUT);
Serial.begin(9600);
}

void loop(){
val = digitalRead(IRpinIN); // read input value
if (val == HIGH) { // check if the input is HIGH (button released)
digitalWrite(ledPin, HIGH); // turn LED on
currentState = 1;
}

else {
digitalWrite(ledPin, LOW); // turn LED off
currentState = 0;
}
if(currentState != previousState){
if(currentState == 1){
counter = counter + 1;
Serial.println("Onboard");
Serial.println (counter);
}
}
previousState = currentState;
delay(250);
}

//This is how I see it
val = digitalRead(IRpinOUT);
if (val == HIGH) {
digitalWrite(ledPin, HIGH); 
currentState = -1;
}

else {
digitalWrite(ledPin, LOW);
currentState = 0;
}
if(currentState != previousState){
if(currentState == 1){
counter = counter - 1;
Serial.println("OffLoad");
Serial.println (counter);
}
}
previousState = currentState;
delay(250);
//how do i get these into the previous code.

Posting code without explaining why you posted it wastes your time AND out time.

ive written comments in the code … .i duplicate the lloop but not sure how to insert into the code

//This is how I see it
val = digitalRead(IRpinOUT);
if (val == HIGH) {
digitalWrite(ledPin, HIGH); 
currentState = -1;
}

else {
digitalWrite(ledPin, LOW);
currentState = 0;
}
if(currentState != previousState){
if(currentState == 1){
counter = counter - 1;
Serial.println("OffLoad");
Serial.println (counter);
}
}
previousState = currentState;
delay(250);
//how do i get these into the previous code.