Arduino Software Basics

The Arduino uses the C programming language. There are many good books, including the classic “K&R” reference (The C Programming Language by Brian Kernighan and Dennis Ritchie, Prentice-Hall, 1978) and many newer books and websites focused on C for Arduino. If you are familiar with other programming languages, you should be able to comprehend most of the code. Here are some pointers for a few of the less intuitive parts:

The ++ and - - operators increment or decrement the adjacent variable Logical operators are OR (||), AND (&&) and NOT (!)

Each Arduino program (“sketch”) consists of two main functions. The function setup() is performed once at power up. The function loop() is repeated thereafter.
I’ve used the setup() function to program how I use the input and output pins and to initialize variables. In the loop() function, I first deal with the audio signal:

• Read the audio signal and implement the first comparator and OR function
• Pass through a digital single pole filter
• Then implement the second comparator to get the AudioPresent signal

You can see how a couple of lines of code replace a lot of analog circuitry.
Then, I implement the state machine. The state machine transitions between four different states (OFF, P0, P1, and P2) based on the condition of the inputs from the override switch (ManualOn, ManualOff) and the output from the audio detector (AudioPresent). Counters are used to implement the time delays, advancing once for each time through the loop() function.
Figure 1 shows the state machine with the lines illustrating the condition of the inputs and timers that cause it to transition from one state to another. You can learn more about state machines at http://en.wikipedia.org/wiki/Finite-state_machine.
At the end of the code, there is an extra delay to set the sample rate to about 2 kHz (500 usec each time through the
loop() function).
 
Figure 1: The state machine logic shows the condition of the inputs and timers that cause it to transition from one state to another.
2/2
related items