Arduino Uno Project - Counter does not begin from 0

Dear Support people,

I have the following problems regarding my arduino uno project:
See the source code, attached in this post.

a ) I have created a simple people counter with the support of infrared leds.
I have 2 doors , and for each door i am using 2 receivers.
The main problem is that the counter is not beggining from 0 , but it begins from the integer value 1.
Is there any correction that i can make in the source code, to solve this sily problem ?

b ) In addition, how i can store the total value from Door A and Door B, to the internal memory of arduino, so that in case i disable and ReEnable the arduino, the previous total variable is Redisplayed in the Arduino's Display screen.

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

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);

int a1,a2,a3,a4;

int b1,b2,b3,b4;

int lb1,lb2,lb3,lb4;

int c1,c2,c3,c4;

int CounterA, CounterB;

int mem_a, mem_b;

void setup()
{
  // set up the LCD's number of columns and rows: 
  lcd.begin(16, 2);
  // Print a message to the LCD.
  lcd.print("Entry Counter ");
  lcd.setCursor(0,1);
  lcd.print("   Panos Iliadis");
  delay(3000);
  lcd.clear();
  a1 = 1000;
  a2 = 1000;
  a3 = 1000;
  a4 = 1000;
  b1 = b2 = b3 = b4 = 0;
  lb1 = lb2 = lb3 = lb4 = 0;
  c1 = c2 = c3 = c4 = 0;

  lcd.setCursor(0,0);
  lcd.print("A:");
  lcd.print( CounterA,10 );

  lcd.setCursor(0,1);
  lcd.print("B:");
  lcd.print( CounterB,10 );

  lcd.setCursor(10,0);
  lcd.print("Total ");

  lcd.setCursor(10,1);
  lcd.print( (CounterA+CounterB),10 );
}

void loop() {

  // set the cursor to column 0, line 1
  // (note: line 1 is the second row, since counting begins with 0):
  //lcd.setCursor(0, 0);
  // print the number of seconds since reset:
  //lcd.print(millis()/1000);

  //  lcd.setCursor(6,0);
  //lcd.print( ":" );
  a1 = analogRead( 4 );
  delay(10);

  a2 = analogRead( 3 );
  delay(10);

  a3 = analogRead( 2 );
  delay(10);

  a4 = analogRead( 1 );
  delay(10);

  if (a1>500) b1 = 1;
  else        b1 = 0;

  if (a2>500) b2 = 1;
  else        b2 = 0;

  if (a3>500) b3 = 1;
  else        b3 = 0;

  if (a4>500) b4 = 1;
  else        b4 = 0;


  //lcd.setCursor(14,0);
  //lcd.print( b1,10 );
  //lcd.print( b2,10 );
  //lcd.setCursor(14,1);
  //lcd.print( b3,10 );
  //lcd.print( b4,10 );

  //Main Algorithym
  if (!b1 && !lb1) c1 = 0;
  if (!b2 && !lb2) c2 = 0;
  if (!b3 && !lb3) c3 = 0;
  if (!b4 && !lb4) c4 = 0;

  if (b1 && lb1) c1++;
  if (b2 && lb2) c2++;
  if (b3 && lb3) c3++;
  if (b4 && lb4) c4++;

  //lcd.setCursor(0,0);
  //lcd.print( c1,10 );
  //lcd.setCursor(3,0);
  //lcd.print( c2,10 );


  //lcd.setCursor(0,1);
  //lcd.print( c3,10 );
  //lcd.setCursor(3,1);
  //lcd.print( c4,10 );


  if (b1 && lb1 && b2 && lb2 && (c1>2) & (c2>2)) {
    if (mem_a==0) {
      mem_a = 1;
      CounterA++;
      lcd.setCursor(2,0);
      lcd.print( CounterA,10 );

      lcd.setCursor(10,1);
      lcd.print( (CounterA+CounterB),10 );
    }
  }

  if (b3 && lb3 && b4 && lb4 && (c3>2) & (c4>2)) {
    if (mem_b==0) {
      mem_b = 1;
      CounterB++;
      lcd.setCursor(2,1);
      lcd.print( CounterB,10 );

      lcd.setCursor(10,1);
      lcd.print( (CounterA+CounterB),10 );
    }
  }


  if (!b1 & !lb1) mem_a = 0;

  if (!b3 & !lb3) mem_b = 0;

  /*
  lcd.setCursor(6,0);
   lcd.print( a1,10 );
   //lcd.print( "," );
   
   lcd.setCursor(12,0);
   lcd.print( a2,10 );
   //lcd.print( "," );
   
   lcd.setCursor(6,1);
   lcd.print( a3,10 );
   
   lcd.setCursor(12,1);
   //lcd.print( "," );
   lcd.print( a4,10 );
   */

  lb1 = b1;
  lb2 = b2;
  lb3 = b3;
  lb4 = b4;
  delay( 50 );
  //a1++;
  /*
  if (a1==10) {
   a1 = 0;
   a2++;
   if (a2==10) {
   a2=0;
   a3++;
   if (a3==10) {
   a3=0;
   a4++;
   }
   }
   }
   */
}

Which counter are you referring to CounerA or B? And if you are using counters, why are you not declairing them equal to 0?

.
.
int CounterA = 0, CounterB = 0;
.
.

Sorry, but unfortunately the counting begins with 1 (again).

The result (in my display) is :
A=1
B=1
Total = 2

orry, but unfortunately the counting begins with 1 (again).

Since you don't seem to be displaying the value until after you increment it, what do you expect?

After incrementing the CounterA , i display the value to the Screen. What do you mean ?

if (mem_a==0) { // CounterA = 0
mem_a = 1;
CounterA++; // CounterA = 1
lcd.setCursor(2,0);
lcd.print( CounterA,10 ); // Show CounterA -> 1

lcd.setCursor(10,1);
lcd.print( (CounterA+CounterB),10 );
}

You did the exact same thing for CounterB.
However when you power up the arduino or upload the code, do you at anytime see 00 on your lcd?

Actually, the only thing i want, is that the code should count the people. Only that.
If i disable the CounterA++ or CounterB++ , the display begins with 0, BUT !! i can not count the people !
That's my problem !

If you start at zero, and one person enters, how many people are in the room?

Why are you expecting something other than 1?

If you mean to display the number in the room at all times, you need to move that code outside of the "increment when someone arrives" and "decrement when someone leaves" blocks.

It would also benefit you to learn about arrays and for loops - you could eliminate a lot of duplicate code in your sketch.

When you see the number 1 on the LCD is it coming from the lcd.print() in setup() or the lcd.print() in loop() ?
Temporarily put a delay(2000); at the end of setup(). What do you see during that delay ?

I solved the problem by using the variable R and R2 in my code, and now it starts counting from 0. Thank you all for your comments. I'll put this counter on a door and count only those who enter. So I want to start from 0 and increase...

Any ideas on how to save the "total" variable in the memory of arduino? So as when restarting, the last total number shows up in the screen of Arduino...

if (b1 && lb1 && b2 && lb2 && (c1>2) & (c2>2)) {
     if (mem_a==0)
     {
       R=1;
       CounterA++;
       mem_a = 1;
       lcd.setCursor(2,0);
       lcd.print( CounterA-R,10 );

       lcd.setCursor(10,1);
       lcd.print( (CounterA+CounterB)-(R+R2),10 );
     }
   
  }
  
  if (b3 && lb3 && b4 && lb4 && (c3>2) & (c4>2)) {
     if (mem_b==0) {
       R2=1;
      CounterB++;
       mem_b = 1;
       lcd.setCursor(2,1);
       lcd.print( CounterB-R2,10 );

       lcd.setCursor(10,1);
       lcd.print( (CounterA+CounterB)-(R+R2),10 );

Any ideas on how to save the "total" variable in the memory of arduino?

You are already doing that. Perhaps you mean to store the value in EEPROM?

Exactly! And when I reboot, i want to show the total number on my display. Then with reset button i will noolify it whenever I want

Panos_iliadis:
Exactly! And when I reboot, i want to show the total number on my display. Then with reset button i will noolify it whenever I want

int CounterA, CounterB;

How many people do you expect to be in the room? -12,456? 32,512?

It seems like a more reasonable type would be byte, with a range of 0 to 255.

EEPROM.write() when the value changes, and EEPROM.read() in setup.().

If you really expect a negative number of people in the room, or more than 32,767, you might need highByte() and lowByte().

EEPROM.write() when the value changes, and EEPROM.read()

I expect only positive numbers. I think the number 32,767 is quite good. If I need a larger number i will look it up later. But where in the code should I put these commands? Sorry if the question is stupid but I'm new in programming :slight_smile: ...

I solved the problem by using the variable R and R2 in my code, and now it starts counting from 0

You didn't solve it, you worked around it by adding a fudge factor.

You didn't solve it, you worked around it by adding a fudge factor.

Actually yes. At least I get the result that I want. If you suggest to me a more optimal solution, I would appreciate that!!

If you cannot track down the problem and understand it then it may come back to bite you at some time. Did you try what I suggested in reply #9 ?

I think the number 32,767 is quite good

You plan to have 32000+ people in a room at one time?

But where in the code should I put these commands? Sorry if the question is stupid but I'm new in programming

I don't think that the problem is that you are new to programming. You appear to be new to thinking.

There is some place in the code where you need to know the number of people that were last known to be in the room. so that you can set the initial value. It doesn't really seem that difficult to figure out where that is.

There is some place in the code where the number of people in the room changes, and you would want to persist that infomation. Again, it doesn't seem like it should be all that difficult to find where that is.