Poblem with IR ball detection

Hello,

I’m currently working on a robot that detects an IR ball using some “normals” IR sensors . I made a first version of the code that allows me to detect the ball using only an analogRead , but it also detected the ambient light , so i made a second version ,which was meant to only detect the ball , that was sending a pulsed signal of 1200 Hz . The code I made is supposed to take many values in an array and scan this array to detect the consequent changes in values coming one after another . The problem is that it still detect the ambient lignt . Here is the code :

int detectionBalle(int a)
{
  int tab[500] = {0};
  int i=0;
  int somme = 0;
  int temps1=millis();
  int temps2 = temps1;
  while(temps2-temps1<50)
  {
    int b = IR(a);
    tab[i] = b;
    i++;
    temps2=millis();
    Serial.println(b);
  }
  int boucle;
  int tab2[100];
  for(boucle=1;boucle<=1;boucle++)
  {
    for(i=0;i<(100*boucle);i++)
    {
      if (tab[i]<0)
      {
      tab2[i]=0;
      }
      else
      {
      tab2[i]=tab[i];
      }
    }
    i=0;
    int compteurvar=0;
    for(i=0;i<100;i++)
    {
      if((tab2[i+1]<tab2[i]*0.9)||(tab2[i+1]>tab2[i]*1.1))
      {
      somme += tab2[i];
      compteurvar++;
      }
    }
    delay(1000);
    Serial.println(somme);
    delay(1000);
    if (compteurvar>=2)
    {
    return (int)(somme/100);
    }
  }
  return 0;
}

What is the problem here ?

Thanks!
(Sorry for the bad english , I’m a frenchspeaker)

Here is the code :

Here is (some of) the code :

At 1200 Hz, you don't need to store anything in an array, if you are controlling the IR emitter. You can compare measurement taken with it on, and with it off, and subtract in real time.

Thanks for the answer .

The purpose of the robot is to look after the ball , so we don't control the IR emitter. We can't compare the values with it on and off since we don't know where the ball is actually located .

    int b = IR(a);
    tab[i] = b;
    i++;

There is no point in using three lines to do what can be done in one.

    tab[i++] = IR(a);
  int temps1=millis();
  int temps2 = temps1;
  while(temps2-temps1<50)

Read the documentation on the millis() function. Pay particular attention to the return type. It is NOT int.

  int tab[500] = {0};

That is almost half of SRAM on a 328-based Arduino. Which Arduino are you using?

Don’t you think that it would be a good idea to check that you are not exceeding the limits of the array before writing to it?

Thanks for your answer PaulS !
We are using an Arduino Mega 2560 . We've had the memory problem in an earlier version of the code that used a 1000 values array . But as I recall , we switched to the 500 values and we were not exceeding the limits as we printed all of the values while testing it .

About the millis function , I just switched it to an unsigned long , but we've used the integer type because we weren't planning to let the code run for more than some seconds .

we weren't planning to let the code run for more than some seconds .

So, after 32 seconds, you plan to reset the Arduino. I see.

The alternative being to use the proper type and the proper code and let it run for weeks. Wouldn't want that, I guess.

Actually , we let it run for 50 seconds before reseting .
I didn't get your idea , the purpose of the robot is to look for a ball that sends an IR signal . Letting it run for some weeks sure isn't an option :smiley:

Letting it run for some weeks sure isn't an option

Deliberately coding it so it can't work for more than 32 seconds isn't, either.

Any alternative ?
We've tested the code , the array get as much as 460 values at it's maximum . I don't think the problem involve the millis function . It's after that that we have problems differentiating between the signal emitted by the ball and the ambiant light .

It's after that that we have problems differentiating between the signal emitted by the ball and the ambiant light .

So, the problem is in the code you didn't post (or involves parts that you did post that you know are wrong but have no intention of fixing).

Some Serial.print() statements (and comments) would possibly be useful.