Controlling birdsong playback by IR detectors


The goal of this project was to control birdsong playback by detecting when a bird was on a given perch. We decided to do this by sending an IR beam along the length of the perch (about 35 cm), about 2 cm above the perch. The sensor interface as very simple but depends on having an analog interface to matlab to do the analysis. The IR sensor and IR emitter were positioned directly next to each other one one side of the cage. On the other side of the cage we placed a few cm square piece of reflective tape, like the kind used on bicycle helmets. Using the retroreflective tape made allignment of the sensors trivial because light was always returned in the direction of the emitter. The retroreflective tape is on the side of the cage nearer the camera in the image below.

One sensor is shown below. A total of two sensors were used, although many more could be operated from one National Instruments USB-6008 interface. The asterisk next to the two opto devices indicate the positive leads. The positive lead is longer. The base of the round opto packages are flattened slightly on the side nearest the negative lead.

The USB-6008 connections are shown below. For simplicity, channels 0 and 1 (connections 2 and 5) were used in the matlab code. Ground was attached through connection 4.

The code is below. We start by defining several opjects: a figure, a user interface control, two audioplayer objects, and the analog interface. We then loop a few times to get the average IR sensor output with no bird in the cage. Calibration should be performed with the room lighting the same as that used during the experiment. The program then drops into a loop to read analog values and play songs.

%record two IR sensors and play songs
clear all

set(gcf,'position', [200 200 200 100])

%make a control to stop the loop
   'string','Quit', ...
   'position',[0 0 50 20], ...

%buid two songs
load handel
s1 = y(1:16000);
s2 = y(60000:73000);
Fs = 8000; 
%define the audio players
song1Player = audioplayer(s1, Fs) ;
song2Player = audioplayer(s2, Fs) ;

%define the analog input
adaptor = 'nidaq';
adaptorData = daqhwinfo(adaptor);
id = adaptorData.InstalledBoardIds{1} ;
% Analog input object Configuration.
% Create an analog input object with two channels.
ai = analoginput(adaptor, id);
ch = addchannel(ai, [0 1]);

%text for sensor level
texthandle = text(.1, .5, ' ');

%calibrate sensor
for i=1:10
   calSample(:,i)  = getsample(ai) ;
   disp( calSample(:,i));
threshold1 = mean(calSample(1,:)) +  0.1 
threshold2 = mean(calSample(2,:)) +  0.1 

%start looping and waiting for a mouse click
while (stopit==0)

    sample = getsample(ai) ;
    %show it for debugging
    set(texthandle,'string', num2str(sample));
    %play song one 
    if sample(1)>threshold1 & ~isplaying(song1Player)  & ~isplaying(song2Player);
        disp(['playing song 1:',num2str(sample)]);
    %play song 2
     if sample(2)>threshold2 & ~isplaying(song2Player)  & ~isplaying(song1Player);
        disp(['playing song 2:',num2str(sample)]);

close all