Computer-controlled random interval timer utilizing shell scripts
I've just completed constructing a fully programmable timer system to manage a Hitachi magic wand (or any other mains-powered items, such as lamps and water…
I’ve just completed constructing a fully programmable timer system to manage a Hitachi magic wand (or any other mains-powered items, such as lamps and water pumps). I used only household supplies for the build and wanted to share this with anyone interested in implementing a similar setup. The software side might be limiting if you’re not on Mac or *nix platforms. My solution is tailored specifically for those environments. Essentially, I wrote a ‘shell script’, which is a small program stored as a text file that gets interpreted by the shell—the command line interface supporting the operating system. The output from the computer is entirely mechanical: the program simply opens and closes CD/DVD drives. I had an old broken drive lying around; even though it can’t read CDs anymore, it works perfectly for this purpose. If possible, use a discarded or broken drive (perhaps found at an electronics recycler) because repeated opening and closing might not be kind to the actuation motor. This mechanical output also means the system could serve as a release method, such as pushing a key off of its shelf. If you want to control a DC circuit powered by batteries, it should be fairly straightforward, though I haven’t tried it myself yet. You could use a pushbutton that gets depressed when the tray is open, or attach tape on wire to both the drive tray and a fixed object so they connect when the tray opens. My setup involves a C-shaped section of clothes-hanger wire duct-taped to the leading edge of the drive tray, creating an L-shaped protrusion with some vertical leeway. The drive sits next to a wireless light switch like this one … 033&sr=8-8 so that the protrusion pushes the switch on and pulls it off as the tray opens and closes. The receiver then toggles the outlet, turning your vibrator/lamp/whatever on or off. Now that we have the computer controlling the device, let’s discuss how to control the drive from the computer. I’m using a Mac, so I’ll focus on that here; if you have a *nix machine, figuring out any differences should be easy. First, open Terminal in your Mac—a program used to interact with the shell. You can find it under Applications/Utilities or just search for it. When it opens, look for a prompt like this one waiting for your command: Code: Select all
Computername:~ username$
If you have any external drives plugged in already, type the following to check if they are recognized. Code: Select all
drutil list
This will display a list similar to this: Code: Select all
Vendor Product Rev Bus SupportLevel
1 HL-DT-ST DVDRW GWA4080MA AE39 ATAPI Apple Shipping
2 LACIE CDBP-241040A 6.34 USB Apple Supported
The number on the left is how we will reference each drive in our commands. The commands to open and close a specific drive are: Code: Select all
Open
drutil -drive x tray eject
Close
drutil -drive x tray close
Where ‘x’ is replaced by the drive number listed above, e.g., “drutil -drive 2 tray eject”. Another basic command is sleep, which simply waits for a specified number of seconds before executing the next instruction. For instance, if you want your computer to wait two minutes and then turn on your vibrator, type the following and hit enter: Code: Select all
sleep 120; drutil -drive 2 tray eject
To introduce random timing, we need our first script. I expect you’ll want to use mine as a starting point. Open TextEdit or another text editor and ensure it is set to plain text format (in TextEdit: Format > Make Plain Text). Then copy and paste the following into your document: Code: Select all
#!/bin/sh
#defaults
defaultOnMax=60
defaultOnMin=30
defaultOffMax=30
defaultOffMin=10
RANDOM=$5 #Seed Value for $RANDOM
#If arguments provided, set them, else use default values
echo number of arguments $#
if [ $# -lt 4 ]
then
maxOn=$defaultOnMax
minOn=$defaultOnMin
maxOff=$defaultOffMax
minOff=$defaultOffMin
else
minOn=$1
maxOn=$2
minOff=$3
maxOff=$4
fi
echo minOn $minOn
while [ 1 ]; do
drutil -drive 2 tray eject #turn on circuit
##### onTime #####
echo turning on for $minOn to $maxOn seconds
echo random number generated $rand
rand=$RANDOM #get a random number
range=`expr $maxOn - $minOn` #determing the range between min and max
ratio=`expr 32767 / $range` #conversion factor to seconds
diff=`expr $rand / $ratio` #find difference btwn min and onTime
onTime=`expr $diff + $minOn`
echo on for $onTime seconds
sleep $onTime #wait for onTime seconds
drutil -drive 2 tray close #turn off circuit
##### offTime #####
echo random number generated $rand
rand=$RANDOM #get a random number
range=`expr $maxOff - $minOff` #determing the range between min and max
ratio=`expr 32767 / $range` #conversion factor to seconds
diff=`expr $rand / $ratio` #find difference btwn min and onTime
offTime=`expr $diff + $minOff`
echo off for $offTime seconds
sleep $offTime #wait for offTime seconds
done
Save this file to your desktop. You can name it whatever you like, but I will use ‘random.txt’ in the instructions below. Back in Terminal, we need to change the file permissions so it is “executable”. Type or paste the following (substituting your filename if different): Code: Select all
chmod +x ~/Desktop/random.txt
Then hit enter. This program uses drive number 2. If you are using a different one, change two lines in the text editor that start with ‘drutil’ to match your correct drive number. To run it from Terminal, type: Code: Select all
~/Desktop/random.txt
The program’s behavior is controlled by four parameters I call minOn, maxOn, minOff, and maxOff. By default, these values are 30, 60, 10, and 30 seconds respectively. This means the drive stays open for a random interval between 30 and 60 seconds, then closes for another random interval of 10 to 30 seconds before repeating. To change these values when running the program, type the numbers after your command in this format: Code: Select all
~/Desktop/random.txt minOn maxOn minOff maxOff
for example:
~Desktop/random.txt 60 120 180 240
This example would turn it on for 1-2 minutes and off for 3-4 minutes. To change the default values permanently, just edit the numbers at the top of the program in your text editor. I hope this helps someone out there. Let me know if you have any questions, corrections, or suggestions.