Sound the Octo-alert!

Standard

Both our children are currently big Octonauts fans – if you haven’t come across the Octonauts, they are a team of underwater explorers who featured in a series of books by a Canadian duo jointly known as Meomi. The books have been made into an incredibly popular TV cartoon shown in the UK on the CBeebies channel (the BBC channel aimed at younger children).

 Octoalert illustration

Whenever there is an emergency, Captain Barnacle will order “Sound the Octo-alert!” – the big button with the distinctive Octopus logo will be pressed, and alarms sounds, and the Octonauts report for duty. As I said, both our children are big fans, and Freya does a mean Octo-alert imitation, so when I was looking for a new Arduino project to do with Bryn, I thought building a working Octo-alert would be great fun.

While you can get an extension (called a shield) for an Arduino to record and play sound (e.g. http://www.ladyada.net/make/waveshield/) I hadn’t got one of these, and I thought a simpler (and in some ways more satisfying) approach would be to have the Arduino joined up to the computer and have a button on the Arduino trigger a sound/video file on the computer. A quick search found the Octoalert website – with a clickable Octo-alert image which then plays an mp3 of the Octo-alert sound. Given this was already there and worked, my idea morphed into triggering the alert from this website by simulating a ‘click’ on the image.

However, before we got to this point we needed to build an appropriate Arduino circuit and craft a button and console. The Arduino circuit is a very simple one – just a single button wired up to an LED and of course a pin on the Arduino. Once I’d put the circuit diagram on, Bryn was able to get going on the wiring. Some of the components are still a bit fiddly for Bryn to put into the breadboard by himself, but for the wires he can follow the diagram and put the wires in himself. I helped with the bits he couldn’t manage, and generally supervised him – but he could do a lot of this himself.

To create the button we cut in half a small plastic ball, Damyanti drew the logo, and Bryn set to work glueing on orange tissue paper

octoalert1

 

A simple ‘button press’ program for the Arduino is available from http://www.oomlout.com/oom.php/products/ardx/circ-07 and I adapted this slightly so that the LED flashed several times on a single button press, and so the button also controlled the output to the serial port – which allows the Arduino to send data back to the computer it is joined to. The Arduino code looked like this:

// set pin numbers:
const int buttonPin = 3;     // the number of the pushbutton pin
const int ledPin =  9;      // the number of the LED pin

// variables will change:
int buttonState = 0;         // variable for reading the pushbutton status

void setup() {
  // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);      
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT);
  // initialize serial port
  Serial.begin(9600);  
}

int interval_time = 500;

void loop(){
  // read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin);

  // check if the pushbutton is pressed.
  // if it is, the buttonState is HIGH:
  if (buttonState == HIGH) {     
    // LED off:    
    digitalWrite(ledPin, LOW);
    Serial.println("0");
    delay(interval_time);
  } else {
    // turn LED on:
    Serial.println("1");
    for (int i=0;i < 10;i++) { 
      digitalWrite(ledPin, HIGH); 
      delay(500);
      digitalWrite(ledPin, LOW);
      delay(500);
    }
    delay(interval_time);
  }
}

On the computer side I needed a mechanism to read the output on the serial port – I found a hint on how to do this here http://playground.arduino.cc/Interfacing/Ruby. I also needed a way of accessing the Octoalert website – I knew there was a Ruby Gem to control a browser called Watir, so I got this installed, and after a few false starts (the task was complicated by the fact that clicking the button on the website actually runs some javascript) I got some code working that could read the input from the Arduino and trigger the necessary javascript on the website:

#!/usr/bin/ruby
require 'rubygems'	
require 'watir-webdriver'
require 'serialport'

#params for serial port  
 port_str = "/dev/tty.usbmodem1411"
 baud_rate = 9600  
 data_bits = 8  
 stop_bits = 1  
 parity = SerialPort::NONE  

 sp = SerialPort.new(port_str, baud_rate, data_bits, stop_bits, parity) 
 browser = Watir::Browser.new

 #just read forever  
 while true do
   while (i = sp.gets) do
   	puts i
    if i.to_i > 0
    	browser.goto "http://octoalert.com"
		browser.link(:id,"playLink").fire_event "onclick"
		sleep(7)
		browser.goto "http://brynpatelstephens.com"
   	else
		puts "No Octoalert"
	end
   end 
 end

With this code running on the computer, and the Arduino program loaded to the board we had a working button – we now needed to put the whole thing together.

A bit of improvisation with some cardboard and we mounted the Octo-alert button in such a way it could sit over the Arduino and pushing down on it pushed the actual button on the Arduino. I cut a hole in a cardboard box for the button to be mounted in, and Bryn decorated it in the style of the Octonauts instrument panel.

octoalert2

This was a nice project, in that it mixed some nice crafting (glueing the tissue paper, making the control panel) with a very simple circuit – all of which Bryn could either do by himself, or with a small amount of help. The programming was slightly more tricky, and I just got on this with while Bryn did other stuff – but I explained a bit about how the Arduino was able to ‘talk’ to the computer which I think he got. He also was quite concerned that by using the sound/graphic from the website that we were ‘stealing’ another’s work – which meant an opportunity to talk about the web and how it worked – although I’m not entirely sure he understood my explanation of using the website versus copying the files – and to be honest I’m not sure I quite understand it either – after all in both cases you end up with a copy of the file on your computer!

And it wasn’t just fun for me and Bryn – Freya got to join in too as you can see in this video – yes, it’s time to sound the Octo-alert 🙂