Example The Arduino Dictaphone

Most people make notes on the back of train tickets and shopping lists because the effort to switch on a computer or find the phone's notepad application is too much. By combining the interface-less environment of an Arduino and the audio functionality of a monitor-less PC, you can create a very simple voice recorder.

You start with the basic switch circuit, but you then replicate it three times—once for each of the record, play, and erase buttons, as shown in Figure 2-7.

Figure 2-7. Using three switches and inputs to control a voice recorder

You can then adapt the similarly banal Arduino control program to check for three buttons instead of one, remembering to debounce each of them. There's also a slight change here; since you're using the record button to govern the length of the record, consequently it sends a start message when the button is pressed and a stop message upon release.

int pinStates[3];

int lastStates[3];

long timesLastPressed[3];

pinMode(inputSwitchPins[i], INPUT); lastState = digitalRead(inputSwitchPins[i]); timesLastPressed[i] = millis();

int pinState = digitalRead(inputSwitchPins[i]);

millis() - timesLastPressed[i] > debouncePeriod) { switch(i) {

Serial.print(pinState==0?"B":"E"); break; case 1: // play if (pinState == 0) { Serial.print("P");

break; case 2: // delete if (pinState == 0) { Serial.print("D");

break;

timesLastPressed[i] = millis(); lastStates[i] = pinState;

Notice that I have also extended the control codes. Instead of a simple 0 and 1, I now have B and E to begin and end the recording, P to play back the sounds, and D to delete them all. You can then adapt the PC-based C code as you did for the doormat to run one of three scripts you've written on the PC to control the sound card:

system("vox_record.sh start"); } else if (v == 'E') {

system("vox_record.sh stop"); } else if (v == 'P') {

system("vox_play.sh"); } else if (v == 'D') { system("vox_delete.sh"); ... as before ...

This might be to record the sound with vox_record. sh: #!/bin/bash

LOGFILE=/var/log/voxrecordpid DIR_INCOMING=/usr/local/media/voxrecord if [ "$1" == "start" ]; then

FILENAME=~mktemp -p $DIR_INCOMING\wav arecord -f cd -t wav $FILENAME >/dev/null >/dev/null 2>&1 & PID=$!

echo $PID >$LOGFILE

fi if [ "$1" == "stop" ]; then PID='cat $LOGFILE~ kill $PID rm $LOGFILE

fi or play back each sound in the directory with vox_play.sh: #!/bin/bash

DIR_INCOMING=/usr/local/media/voxrecord for F in "$DIR_INCOMING"/*.wav do play $F done or even delete them all through vox_delete.sh: #!/bin/bash

DIR_INCOMING=/usr/local/media/voxrecord rm -f $DIR_INCOMING/*

Naturally, there is a lot more scope here to support the deletion of individual recordings, and so on. But this represents the idea.

Was this article helpful?

0 0

Post a comment