Kidpi…!

My seven year old son, Camden, who is a hardened Windows user has made life very difficult for me to introduce him to the Pi. He was really interested in making an LED flick on and off for the best part of 5 minutes and then found out that he could play Minecraft (which is a great distraction, but teaches him nothing about programming).

My Veroboard layout of my kidPi set-up.

My Veroboard layout of my kidPi set-up.

So, I decided to write a Bash script that made it easier for him to control various aspects of the Pi on his own. My aim was for him to type in easy commands that the script would then interpret and provide the relevant output. I have also included coloured text to make it more understandable when running.

So far the script does the following:

  • Turns LEDs on and off
  • Flashes LEDs
  • Recognises a button press
  • Takes a photo on the webcam that prints his name on the photo
  • Tells the temperature as per my Temperature tutorial
  • Plays music that is included in the “Music” folder
kidPi Veroboard Setup

kidPi Veroboard Setup

I have not included any error checking yet (i.e. don’t allow photo to be taken if no camera is attached), but I hope to do this soon. So the script and information comes as-is for the time being. This script also assumes that you have the following packages installed:

  • fswebcam (to take a photo)
  • wiringPi (for the button press and LED flashing)
  • mpg321 (for playing music)

Anyway, lets have a look at the script (I’ll explain it below the code):
Firstly you need to create a folder called “kidpi”:

mkdir /home/pi/kidpi

Then we need to navigate to the folder:

cd /home/pi/kidpi

Now create a file called kidpi.sh:

nano kidpi.sh

You can now copy the following into the file then press CTRL+X (to save), press Y (to confirm) and then ENTER. Or you can download the file here.

#!/bin/bash
#
# Text color variables
txtund=$(tput sgr 0 1) # Underline
txtbld=$(tput bold) # Bold
bldred=${txtbld}$(tput setaf 1) # red
bldblu=${txtbld}$(tput setaf 4) # blue
bldwht=${txtbld}$(tput setaf 7) # white
txtrst=$(tput sgr0) # Reset
info=${bldwht}*${txtrst} # Feedback
pass=${bldblu}*${txtrst}
warn=${bldred}*${txtrst}
ques=${bldblu}?${txtrst}

# Clear the terminal
clear

# Set up the gpio (WiringPi) pins for the Green & Red LEDs and the button
button=16
green=0
red=1

# Set up a function to read a button press
waitButton ()
{
echo "Press the button to begin the sequence ... "
while [ `gpio read $button` = 1 ]; do
  sleep 0.5
done
}

# echo a welcome message
echo -e "\n\n $txtbld $txtund Hello. Welcome to the kid safe script... please enter a command... $txtrst"

# Start the script
while true
do
  # Provide options for each process
  echo -e "\n\n Options... "
  echo -e " > Turn on/off the green led = $bldblu green on/green off $txtrst \n > Turn on/off the red led = $bldblu red on/red off $txtrst \n > What is the temperature = $bldblu how hot is it $txtrst \n > Make the LEDs flash (x) amount of times = $bldblu flash $txtrst \n > Make the button write to the screen = $bldblu button $txtrst \n > Take a photo with the webcam = $bldblu take a photo $txtrst \n > Play a song = $bldblu music $txtrst \n > Exit this script = $bldblu exit $txtrst \n"
  read -p "$bldblu What would you like to do : $txtrst " input
  # Ask for the users input using the provided options
  clear
  if [ "$input" == "green on" ]; then
    # set the green LED pin to output
    gpio mode $green out
    # Turn the green LED on
    gpio write $green 1
    echo "$bldred >>>>> The green LED has been turned on. $txtrst"
  elif [ "$input" == "green off" ]; then
    # Turn the green LED off
    gpio write $green 0
    echo "$bldred >>>>> The green LED has been turned off. $txtrst"
  elif [ "$input" == "red on" ]; then
    # set the red LED pin to output
    gpio mode $red out
    # Turn the red LED on
    gpio write $red 1
    echo "$bldred >>>>> The red LED has been turned on. $txtrst"
  elif [ "$input" == "red off" ]; then
    # Turn the red LED off
    gpio write $red 0
    echo "$bldred >>>>> The red LED has been turned off. $txtrst"
  elif [ "$input" == "flash" ]; then
    # ask the user how many times to flash the LEDs
    echo -e "\n How many times would you like the LED to flash? \n"
    read flash
    # Set the green & red LED pins to outputs
    gpio mode $green out
    gpio mode $red out
    i=1
    flash=$(($flash+1))
    while [ $i -lt $flash ]
    do
        echo -n -e "$bldred >>>>> The LEDs have flashed $i times $txtrst \r"
        # Turn the green & red LEDs on and off quickly (every millisecond) and repeat 10 times
        gpio write $green 1
        gpio write $red 1
        sleep 0.1
        gpio write $green 0
        gpio write $red 0
        sleep 0.1
        i=$(($i+1))
    done
  elif [ "$input" == "how hot is it" ]; then
    # Open the thermometer CAT file and read the temperature
    tempread=`cat /sys/bus/w1/devices/28-00000495b443/w1_slave`
    tem=`echo $tempread | cut -d"=" -f3`
    temp=$(echo "scale=2; $tem /1000" | bc)
    # Flash the LEDsto make it look pretty
    gpio write 0 1
    gpio write 1 1
    sleep 2
    gpio write 0 0
    gpio write 1 0
    echo "$bldred >>>>> The temperature outside is: $bldblu $temp degrees $txtrst"
  elif [ "$input" == "button" ]; then
    # Run the button script
    waitButton
    echo -e "$bldred >>>>> You have pushed the button $txtrst"
  elif [ "$input" == "take a photo" ]; then
    # Ask the user for their name. This will be put on the final image
    read -p "$bldred Please enter your FULL name... >>>>> $txtrst" name
    # Get the date to asist in naming the photo
    today=$(date +"%d-%m-%Y-%H-%M")
    # Instigate fswebcam to take the photo and save it
    fswebcam -r 1280x720 -S 15 --jpeg 95 --shadow --title "PiCam" --subtitle "" --info "Photographer: $name" --save kidpi_"$name"\_$today.jpg -q
    echo "$bldred Your photo has been saved to the computer$bldblu $name. $bldred It is in the same location as this script $txtrst"
  elif [ "$input" == "music" ]; then
    # Change directory and display any mp3 files.
    cd music
    echo "$bldblu These are the songs you can play: $txtrst"
    ls -1 --file-type *.mp3
    # Ask the user to type the filename of the song to play
    read -p "$bldblu Which song would you like to play : $txtrst" song
    echo "$bldred >>>>> Now playing $song $txtrst"
    # play the song
    mpg321 $song -q
    # Return to the kidpi directory
    cd ..
  elif [ "$input" == "exit" ]; then
    # Turn off the LEDs (if they are still on)
    gpio write $green 0
    gpio write $red 0
    sleep 0.5
    # echo a thank you message
    echo -e "\n $bldwht Thank you for using this script. I hope you have enjoyed it...? 🙂 $txtrst \n"
    # Exit the script safely
    exit
  else
    echo "I'm sorry. I don't recognise your input. Please try again"
    sleep 5
  fi
done

Quite long isn’t it! If you download the file using the above link it looks slightly clearer. Basically we are starting out by setting variables to change the font colour. We then use them throughout the script.
We then ask the user for their input using the menu of choices. This is done using the “read” command.
After that it is basically a file full of if/else statements based on the user input.

It is easily possible to just remove any “elif” statements that are not relevant to your setup. E.G. you may not want the LEDs to flash so you can remove the “elif” statements 1 through 5.

If you need any clarification on any of this script please leave a comment below.

Anyway, Camden loves this little script and couldn’t wait to flash the LEDs on and off 400 times… 😀

This entry was posted in bash, Child Friendly, GPIO, kidPi, raspberry pi, Scripting, Tutorial and tagged , , , , . Bookmark the permalink.

4 Responses to Kidpi…!

  1. This is a great web page, will you be interested in doing an interview about how you designed it? If so e-mail me!

  2. Excellent task writing Kidpi&. I’d like to learn more on this matter.

Leave a Reply

Your email address will not be published. Required fields are marked *