#!/bin/bash

# List of the GPIOs
pins=(17 18 22 23 24)

# List of the files to play
files=(Duck.wav Owl.wav Horse.wav Rooster.wav Crickets.wav)

# Initialize the GPIOs and read the initial state
for i in ${!pins[*]}
do
  if [ ! -e /sys/class/gpio/gpio${pins[$i]} ]
  then
    echo ${pins[$i]} > /sys/class/gpio/export
  fi

  echo in > /sys/class/gpio/gpio${pins[$i]}/direction
  states[$i]=$(cat /sys/class/gpio/gpio${pins[$i]}/value)
done

# Watch the GPIOs
while true
do
  for i in ${!pins[*]}
  do
    state=$(cat /sys/class/gpio/gpio${pins[$i]}/value)

    if [ "$state" -ne "${states[$i]}" ]
    then
      states[$i]=$state

      # An active switch will pull the input down
      if [ "$state" -eq "0" ]
      then
        # Feel free to use another player or to
        # change the path to your sound files
        omxplayer /usr/share/scratch/Media/Sounds/Animal/${files[$i]}
      fi
    fi
  done

  # Allow the CPU to cool down
  sleep 0.2
done
