Nestbox bird entry/exit logger

Hey! This is my first real arduino project but not my first encounter with C/C++.

I am attempting to record entry and exit of birds in a nest box using two phototransistors (basically like a people counter for birds) and the sd shield from adafruit. The goal is to log events at the portal of the box and classify them in four different ways. An "IN" would be the bird going in and presumably triggering the outer transistor followed by the inner transistor, and "OUT" would be the reverse. A "PERCH" would be the bird perching on the front of the box looking in and triggering only the outer transistor, and a "PEEK" would be the opposite, the bird would perch on the inside looking out triggering only the inner transistor. I have the code together with lots of help from resources online (such as this forum), much of it is from ladyada and Mike Cook. I have not yet gotten my arduino and shield (hopefully tomorrow I'll see some boxes!), but I wanted to post my logging code to see if I have overlooked any logical issues or maybe if I have implemented some clunky/inefficient/or otherwise undesirable code in lieu of a better/simpler option (I am not getting any errors in the IDE, just looking for general advice). For example, in writing this post I have realized that I have kept Mike Cook's abs() function which is useful in his implementation considering he was detecting the insertion AND withdrawal of a hand in a box. I am only trying to log something moving near the phototransistors, not something moving away, the abs() I think may record a bird perching, and then also record the bird 'un-perching' due to the change in light. Additionally all delays and threshold values are total shots in the dark at this point and are only place holders until I can troubleshoot the code with the actual board.

Here is the sensor watching code specifically, I am leaving out the loop, setup, data logging, and definitions to make it clear what part is most important to me. If that information would be useful I can post it as well, I just don't know how much people want to sort through my entire sketch.

void doWatch() {
  curVal = analogRead(sensor1Pin);              // read the input pin  
  curVal2 = analogRead(sensor2Pin);              // read the other input pin  
  if (abs(curVal - lstVal) >= threshold) {
    WRITE = true;
    writevalue = "PERCH";
    now = RTC.now();
    do
    {
    curVal2 = analogRead(sensor2Pin);
    if (abs(curVal2 - lstVal2) >= threshold) {
    timeout = 1000 ;
    writevalue = "IN" ;
    }
    timeout++ ;
    delay(1) ;
    } while (timeout < 1000);
  }
  else
  {
    if (abs(curVal2 - lstVal2) >= threshold) {
    WRITE = true;
    writevalue = "PEEK";
    now = RTC.now();
    do
    {
    curVal = analogRead(sensor1Pin);
    if (abs(curVal - lstVal) >= threshold) {
    timeout = 1000 ;
    writevalue = "OUT" ;
    }
    timeout++ ;
    delay(1) ;
    } while (timeout < 1000);
  }
 }
//  Serial.println(curVal - lstVal);             // debug value
//  Serial.println(curVal2 - lstVal2);             // debug value 2
  lstVal = curVal;
  lstVal2 = curVal2;
  timeout = 0;
}

The rest of the code in the sketch essentially logs the writevalue with a timestamp to the SD in the event that WRITE is true, again I can post everything if that would be helpful in your suggestions or thought about my project
Thank you very much!

Well I received my arduino today, but not the logging shield. I did however test the sensor bit of code. I increased the delay time to 200, got rid of the abs() function and dropped the threshold down to 10 and it worked pretty well. Once I get the logger shield I'll deploy it outside on a nest box and hopefully things will go well.

If that information would be useful I can post it as well, I just don't know how much people want to sort through my entire sketch.

All coding is usefull and interesting as it may guide and inspire others :slight_smile:

The Playground...

http://arduino.cc/playground/

... is an excellent repository of "how to..." articles... and gets better and better as more and more people use it, put things there.

For some notes on "broken light beam sensors", see....

(The stuff you want is just a few notes at the bottom)

Nesting birds are....

a) Messy.
b) Sensitive.

"Things" and "Stuff" may get on the IR source and/or the IR sensor, unless they are carefully sited

If you go to the nest too often to clean your source/ sensor, you will cause the parents to abandon the nest.

For debugging a nestbox monitor, perhaps something similar monitoring a cat-flap or similar would be good? Wait... if you have nestboxes, maybe you don't have a cat? Well... monitor a mousehole instead?

Another idea for nestbox monitoring... though it doesn't record direction, as yours does...

Mount a light level sensor high in the box. Arriving/ departing/ perching birds will cause the light in the box to drop momentarily. A bit like detecting exoplanets, maybe!?! ANYWAY.... for this approach to work well, rather than looking at absolute light levels, perhaps "digitally", monitor the current analog level, and count brief variations as a "bird in hole". Otherwise, with the wrong threshold setting, it will look like a "bird in hole" from early dusk until late dawn, and a "bird in hole" at high noon may not be enough to darken interior sufficiently for the bird to be detected.

When looking at light sensors that depend upon a change in light levels to detect motion, it is important to remember when designing these sensors that light levels will vary over several orders of magnitude over relatively short periods of time, but that the light level changes you are looking for will vary over much shorter time frames. In short you need some form of high pass filter on the circuit to block the low frequency changes--or you need a digital output photogate circuit that takes such variations into account as part of its design.