The ArduBat Demo Sketch
An Arduino Detecting bats...
This page will take you through a walk-through of the code used in the ArduBat demo sketch. This is not meant to be a tutorial on programming, but rather an example of how the ArduBat is accessed in the coding of a simple sketch. The following are actual screenshots of the ABDemo.ino sketch as viewed in the Arduino environment. The color coding of the text is done by the Arduino editor, as it interprets the various elements of your code.
|The first lines of the sketch define
the names of the various pins of the Arduino
that are used by the sketch. This is where the mapping of
the pins used for the Bat Call
output of the detector circuit, and the pins connected to
the LEDs and push-buttons are specified.
Two variables are also defined in this section ... one to hold the value of the PulseIn function that will be called later ( a potentially large integer ) and the other to hold the results of a frequency calculation.
|The next section is the setup
section. All sketches will use this section to establish
the initial configuration of all the variables and input
/ output pins.
Here you can see that the Bats data pin , which receives the bat call pulses from the detector, is defined as an input.
The LED pins are all defined as outputs, and the push-button pins are all defined as inputs.
The three digitalWrite lines define the initial state of the three LEDs ... in this case, they are all turned off.
The last line in this section establishes the serial port for sending data back to the computer via the USB line. The port speed is set at 57600, so you need to be sure that speed is also set in the Arduino environment.
|Now we get to the start of the main
portion of the sketch. This loop runs circularly
throughout the remainder of the program.
The processing loop for the demo sketch begins by checking the two push buttons to see if they are closed. If they are, then the appropriate LED is turned on, and the other is turned off.
Note that the push-button data pin will be HIGH if not pressed, and LOW if it is pressed.
|The next section of the program loop
is where the Bats pin is tested
for an ultrasound pulse, and measurements are made to
determine the duration and frequency of the bat call.
The first line is the key to the whole process. The pulseIn command looks at the Bats pin and looks for a HIGH pulse. It will wait for one second before timing out ( giving up ). If a pulse IS detected, the pulse duration in microseconds is stored in the BatPulse variable. Then the pulse is qualified as a valid bat frequency before processing it further.
Using the Serial.print commands, the pulse duration, and later the frequency, is displayed back to the serial monitor window in the Arduino environment.
This part of the sketch also calculates the average frequency measured by the pulseIn command. Frequency is normally expressed as ( 1 / duration ( in seconds )) ... since the pulse coming to the Bats pin actually represents 16 bat call transitions, the equation should be ( 16 / duration ). Adjusting that again for a duration measument in microseconds, and a result in kHz, gives us the final equation of ( 16000 / duration ).
This ends the program loop, and the cycle begins again with the testing of the push buttons.
|So that is a quick look at
the demo sketch. You should have a better understanding
of how to check the push-buttons, and operate the LEDs.
You should also have an understanding of how you can
capture bat call segments and derive the average
frequency of those segments. And, importantly, you should
see how the program needs to be set up to know where the
LED's, push-buttons, and bat call data are all interfaced
to the Arduino.
There will be more code samples and hardware discussions on the web page that addresses the Arduino project stack that I am experimenting with. There will also be more coding examples on Frank's ArduBat web pages. You can also read more about the pulseIn sampling technique.
Tony Messina - Las Vegas, Nevada - email: T-Rex@ix.netcom.com