if statement with array value

Hi guys,

My name is Tom and im new to the arduino stuff. I really like it and i'm using to build a counter for school.
I'm building this with two ir distance sensors. I can run the code and open up the terminal window and seeing 0 shooting past.
When i move my hand in front of the sensor it will be all 1's, so far so good. Now i want to combine this setup with 2 led's.
1 green led and 1 red one. If the counter is below four the green light will stay on, if it's 4 the red light will come up. The second sensor will count down. So it is a 2-way counter for people. If someone goes in a room it will be + and if he leaves it will be -.

void setup() 
{
  Serial.begin(9600);
  pinMode(13, OUTPUT);     
  pinMode(10, OUTPUT);

}

    void loop() 

   {
 
  int sensorValue = analogRead(A0);
  int sensorValue2 = analogRead(A2);
  int totaal[0];
    
      if ( sensorValue < 100 )

      { 
  
       totaal[0] ++;
       delay(2000);
   
     }

        else 

       {
   
 
       }
   
            if ( totaal[0] = 4 )

          {
  
           digitalWrite(13, HIGH);
           Serial.println("test")

          }


  Serial.println(totaal[0],DEC);
  
}

Now i only put the red light in but the problem i have is that it just starts red immediately after starting up the arduino. Maybe the if statement counts the lines in terminal, i dont know excactly. Do you guys have some advice for coding this if statement with the value of the count = max 4
I did put text in the if and it just started to put in the text from the start in, am i missing something ?
Thanks in advance

Tom

remove a bad post.

I was thinking that totaal[0] ++; was a bad way of writing it because of space before ++

But in fact it works...

this is not good because, it does nothing, and doen't store it back again

I think you're going to have to explain that sentence.
Are you referring to the post-increment operator?

Of course, an array with zero elements isn't much use to anyone.

Even if it did have elements, it would be better to make it static or global.

this is not good because, it does nothing, and doen't store it back again

Wrong. The totaal[0]++; statement is perfectly valid. It increments the value in the 0th position of the array by 1.

This, on the other hand, is wrong:

            if ( totaal[0] = 4 )

= is an assignment operator. == is the equality operator. They are not interchangeable.

This is useless:

        else 

       {
   
 
       }

If there is nothing to do if the if test fails, leave the else clause out.

            if ( totaal[0] = 4 )

          {
  
           digitalWrite(13, HIGH);
           Serial.println("test")

          }

Even if the if test were right, the statement to reset totaal[0] back to 0 is missing, isn't it?

It actually works if i use my old code, it stores the number in the array. If it passes it again it will increase with 1 ( ++ )
Ah ok i changed it to ==, removed the else ( it does nothing indeed )
But totaal[0] does not have to be reset to 0, it just has to stay at 4 until someone leaves the room. Than it will go 3 2 1.
It's like traffic light for a room.
If i run it now i'll get -15872 in my terminal, it will count up but i need it to start at zero.

Tom

Now it's time to go read reply #2.

if ( totaal[0] == 4 )

{
  
  Serial.println("lol");
  
}

if i put that code in it , the value will start at 15872, i changed the

 if ( totaal[0] == 4 )

to

 if ( totaal[0] == -15870 )

that worked, it started spit out test when it counted to -15870.
Now i only need it to start at 0.
Ok i read comment #2, ye i understand it says 0 elements but the terminal window will count 1,2,3,4,5 etc, so thats strange

Your variable is declared inside loop, so it goes out of scope at the end of loop. It is also never initialized. Global variables and static variables ARE initialized, by the compiler, to 0. Heap variables, declared in a function, are not.

Ok, ye that makes sense. Im also a bit new to c so i appreciate the help. I now declared totaal[1] out of the loop ( i put it just above void loop(). If i run the program it will count up really fast without moving my hand in front of the sensor.

You've made a number of code changes that we can't see. Perhaps it's time to post your code again.

jup here it is

void setup() {
  Serial.begin(9600);
  pinMode(13, OUTPUT);     
  pinMode(10, OUTPUT);
 
}
int totaal[1];


void loop() {
 
int sensorValue = analogRead(A0);
int sensorValue2 = analogRead(A2);
   
if ( sensorValue < 100 )

{ 
  
   totaal[1]+=1;
   delay(2000);
   
}

if ( totaal[1] == 4 )

  {
     
    digitalWrite(13, HIGH);
    Serial.println("test");
  }
          
  Serial.print("counter =");
  Serial.print(totaal[1],DEC);
  Serial.print("\t sensorValue = ");
  Serial.println(sensorValue,DEC);
    

}

with this définition :

int totaal[1];

only totaal[0] can be assigned without buffer overflow.

But writing : int totaal[1] is equivalent of writing : int totaal; because totaal[1] is an array of ONE element.

so replace totaal[1]+=1; by as you want :

totaal[0]++; or totaal[0] +=1; up to you

but the most simple is to declare

int totaal;

and use

totaal++;

if ( totaal == 4 )

Serial.print(totaal,DEC);

you don't need array of int for the moment.

ok, i replaced it by an int, it now works !
I was using the array because that was working, i tried the int but i didn't placed it outside the loop i guess. Thanks for the useful info about arrays :wink:
I will work on the 2nd sensor and see how that goes. Again thanks for the fast responses.

Tom