Time Lapse Camera Script

# Timelapse script, because timelapse options in raspistill don't
# down the camera between captures. Script also provides a camera
# (v2 cameras don't include one) and a system halt button.
# 'gpio' command requires WiringPi: sudo apt-get install wiringpi
# Limitations: if DEST is FAT32 filesystem, max of 65535 files in
# if DEST is ext4 filesystem, may have performance issues above 10K files. # For intervals <2 sec, better just to use raspistill's timelapse feature. 
# Configurable stuff...
QUALITY=51 DEST=/boot/timelapse PREFIX=img
gpio -g mode $HALT up
gpio -g mode $LED  out
mkdir -p $DEST
# Destination directory (MUST NOT CONTAIN NUMBERS) # Image prefix (MUST NOT CONTAIN NUMBERS)
# Halt button GPIO pin (other end to GND)
# Status LED pin (v2 Pi cam lacks built-in LED) 
# Time of last capture (0 = do 1st image immediately) # Initialize GPIO states
# Create destination directory (if not present) 
# Find index of
while : do 
last image (if any) in directory, start at this + 1
$DEST -name "*.jpg" -printf %f\\n | sed 's/^[^1-9]*//g' | sort -rn | head -1 | sed 's/[^0 
# Forever 
# Time between captures, in seconds # Image width in pixels
# Image height in pixels
# JPEG image quality (0-100) 
while : # Until next image capture time do 
currenttime=$(date +%s)
if [ $(($currenttime-$prevtime)) -ge $INTERVAL ]; then 
   break # Time for next image cap
# Check for halt button -- hold >= 2 sec while [ $(gpio -g read $HALT) -eq 0 ]; do 
if [ $(($(date +%s)-currenttime)) -ge 2 ]; then gpio -g write $LED 1
shutdown -h now 
fi done 
OUTFILE=`printf "$DEST/$PREFIX%05d.jpg" $FRAME`
# echo $OUTFILE
gpio -g write $LED 1
raspistill -n -w $WIDTH -h $HEIGHT -q $QUALITY -th none -t 250 -o $OUTFILE gpio -g write $LED 0 
FRAME=$(($FRAME + 1)) # Increment image counter 
prevtime=$currenttime # Save image cap time done