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!