Tuesday, 24 June 2014

Dog-controlled treat dispenser

I picked up one of these treat dispensers from Maplins on a whim - one of those "I'm-sure-I'll-find-a-use-for-this-someday" things. Subsequently I started wondering if I could train my dog, Jack, to use it (with some modifications, obviously)

An initial attempt involved simply wiring a switch to a footpedal for Jack to step on. That lasted all of 10 seconds as the plastic lid I'd repurposed as a pedal cracked under his weight.

One of the things that attracted me to the gadget in the first place was the capacitive button that it used. A quick test with my phone screen showed that dog's paws can operate capacitive switches.

Simply extending the leads alone wouldn't be enough. Although I want to give Jack the freedom to get his own treats, I still want to keep control over how many!

The touch sensors board has simple connections - just power, and leads out to the motor. I didn't really want to waste time modifying the board, as it's all small surface-mount components. Since I know the motor was only driven one direction by the board, I simply connected an optocoupler to the motor outputs. A couple of pull-up resistors later and the switch was done.

The finished capacitive button PCB with optocoupler


I connected a metal lid in place of the small capacitive disc to make it more dog-friendly.

Now onto the human's control system..

My original plan was to use something web connected such as an old android device, so that I could give Jack treats whilst out at work, but in practice, it was totally overkill, so I opted for a microcontroller based system instead.

The only controls I really need are to limit the number of available treats, set a time limit so he doesn't eat them all at once, and a way to see how many were available.

I found an old PCB with two buttons on it in my junk box. One button to add a treat, one to remove.
 
The salvaged button PCB (I believe it once belonged to a toaster...)


For a display I went with a 10-segment LED, controlled by a shift register. Due to the limitations of the shift register, the maximum treats available at any one time is 8 - which is more than enough anyway. The remaining 2 LEDs are left unused.
The 10 segment display (3 treats available) - the bottom 2 LEDs are not used.



The time limit I chose to fix at 1 hour in the microcontroller. As it's impossible to precisely measure the amount of treats that will be dispensed, the motor was set to a 3 second interval.

Finally I added a small LED module to the pedal, that lights when the timer ticks down and a treat is available, so that Jack could see when he can get a treat. According to the Pavlovian theory, he should soon learn that light=treat.

The LED module (it's just from one of those battery operated 'cupboard lights' you see in nearly every pound shop) - for this project I just connected a jumper over the button.


As with other projects I opted for using an Attiny (in this case the 2313) with the Attiny arduino cores, as they make it nice and easy to prototype with Arduino and switch to a regular AVR for the end product.

Stay tuned for part 2 - building an enclosure for it, some pictures of the completed project, source code, and hopefully some pictures/video of Jack using it.

UPDATE - Part 2 is now available here.

Tuesday, 10 June 2014

Android App: RSS Media Grabber

My second android app is now available on the Google Play store. It's a port of a desktop Java application I wrote a while back, which will retrieve image media from RSS feeds - useful for web comics/cartoons, pic-of-the-day sites etc.







Usage
When using the app for the first time, use the menu button and select "Add New Source".
On the next screen, enter the URL for the feed, and give a folder name (this will be where the app stores the files it downloads - eg, if you enter "MyFolder", the directory will be sdcard/RSSMediaGrabber/MyFolder/)
Select OK to return to the main screen, and select menu->Load sources. This will list all the RSS feeds you've added.
Select one of the feeds to be prompted to retrieve the feed. Select OK and it will load the feed, grabbing any image media, and saving them to the folder.

You'll then be able to view the downloaded media through your photo app of choice.


Future development plans
  • Wider media selection (MP3 for podcasts etc).
  • Integrated media viewer/player
  • Scheduling to check feeds at set intervals.

Support/Feedback
As with my other apps, it's free (ad-supported), and I welcome feedback. Unfortunately I'm not in a position to offer any kind of official support for this, so use entirely at your own risk. If you have any trouble with it, then feel free to contact me on twitter/G+/leave a comment, and I'll try to help as and when I can, but I make no guarantees!

Any feedback, particularly with regard to different phone/tablet hardware and android versions is appreciated.

Thursday, 22 May 2014

Android App: Bluetooth Macro Input

UPDATE - Please note this version of the Bluetooth Macro Input app is no longer supported. Although it's still on the Play store, it will not be updated in future. Please consider migrating to the new Bluetooth Macro and Voice Input app instead.
 
A while ago I posted this snippet of Arduino code for an upcoming project. Unfortunately with a looming redundancy and several courses on the go at the moment I've not had the free time to follow up until now.

All the code does is read a character from the serial pins of the arduino, then outputs that character as a keyboard keystroke using the keyboard emulation functionality of the Arduino Leonardo variants.

The rest of the hardware is very simple - just a bluetooth serial module, such as one of these - connected to those pins. There's just 4 pins - power, ground, TX and RX.

With that done, and the arduino sketch uploaded, the rest of the work is in the android application, which is now available for download by clicking the button below.







What it does
You can provide a library of snippets of text on your devices external storage (any ASCII filetype is fine), and then this app can access them, and send them via bluetooth to the arduino hardware, which emulates a USB keyboard and types the contents out.
This is handy for maintaining a library of code/command snippets that you might want to use across different computers or devices. (Or if, say, your day job requires you to often type the same thing on a computer that you have little control over software/config wise...)

Requirements
  • The hardware described above.. Without it, the app is kinda pointless.
  • Supports versions of android from 2.2 (Froyo) upward, though only tested on a Samsung Galaxy S3.
  • Bluetooth serial device (115200 bps)
  • Permissions
    • Requires bluetooth and external storage access.

Usage

To run the app, first turn on bluetooth.
When you first run the app, you'll need to enter the MAC address of your bluetooth device - you should be able to find this in the docs of your serial device (or printed on it). Once done it should automatically connect.
Create or move your text snippet files to the a folder on the SD card under BluetoothMacroInput directory.
Use the Menu key in the app to bring up the menu, which is basically just the folder structure of the directory - click on your chosen file and it'll load it into the text view. Click 'Type' to have the device start typing your text.
Depending on your external hardware, you might need to adjust the delay value (if the typed text is garbled, then you need to increase the number)


Future Development plans
  • One-button input for frequently used commands.
  • Mouse emulation using phone touchscreen/sensors
  • Dynamic macro recording (using USB host passthrough for a keyboard)
  • General UI improvements..

Support/Feedback
Unfortunately I'm not in a position to offer any kind of official support for this, so use entirely at your own risk. If you have any trouble with it, then feel free to contact me on twitter/G+/leave a comment, and I'll try to help as and when I can, but I make no guarantees!

Any feedback, particularly with compatibility on different hardware (both android devices and Bluetooth/keyboard hardware) will be much appreciated.

Monday, 14 April 2014

Space Invaders alarm clock mod

I'm very conscious of the fact that the last two posts have been quite text-heavy, so here's a lighter weekend project.

I was once given a Space Invaders alarm clock. It's shaped like one of the aliens from the game, the 'pixels' of the LCD display are shaped the same, and when the alarm goes off it makes the aliens sound and moves from side to side as they did in the game.



Which is all well and good unless you have a small bedside table, and the clock just rolls off the edge every morning...

The lazy answer would be to just build a barricade, or some kind of stand that would stop the wheels turning.

Opening up the clock reveals a small PCB, with wires neatly labeled, and a small motor which operates a worm gear to move the wheels.

 

My plan is to replace the motor, and use it's wires to power some LEDs which I will place in the 'eyes' of the model.

Although labelled "M+" and "M-", the motor wires polarity changes - to allow the change of direction. This is great for motors, but reversing the polarity on an LED will likely result in the magic smoke escaping.

To get around this - and also provide a neat colour change effect - each eye will have 2 LEDs in parallel, and arranged in opposite directions. The LEDs of each colour for each eye will be in series (see below). Also don't forget a 100 Ohm resistor at one of the ends (doesn't match with one)

One green and one red LED per eye. As the current changes the LEDs will alternate


This means that when the current is flowing one way, it can't get through one LED, so moves through the other. When it changes direction, it can no longer get through that one, so it diverts to the other, effectively 'blinking' each LED in turn.

Then it's just a case of drilling eye holes, poking the LEDs through, and reassembling (I also used hot glue to act as a diffuser for the eyes)

Each stage of the end result. It doesn't roll off the table anymore, but I didn't count on it looking a lot more sinister.


Thursday, 3 April 2014

Creating a VirtualBox test system for Raspberry Pi development


Firstly, I'm going to walk through the process of setting up a Ubuntu Virtual Machine (VM) on VirtualBox, which I will use as a test environment for developing for the Pi.

Download the Ubuntu ISO from here if need be.
With VirtualBox installed on your PC. Go to New, and walk through the wizard, allocating memory, and creating a virtual hard disk.

Once that's done, boot the machine, and you'll get a "first-run" type prompt, asking you to select an installation source - direct it to the ISO you downloaded earlier.

Once the VM boots, select Install to hard disk, and walk through the graphical wizard for that. Once complete, reboot the virtual machine.

Once rebooted, login, and open a terminal.
Follow the same instructions to update the system and install the LAMP stack as we did in the post about setting up the Pi.

Once that's done, the next step is to install the VirtualBox 'Guest Additions'. These allow easier interaction between your host system and the VM.
Select this from Devices -> Install guest additions at the top of the VM window. After a few seconds you'll be prompted to run the software in the VM (you'll also be asked to authorise the installation)

Let that run through, and then reboot the VM. Now would be a good time to take a snapshot (Machine->Take snapshot). This effectively creates a backup of the system state at the time it was taken, so if you test something and screw it up, it's fairly trivial to get the system back to how it was.

Now, create a shared folder between the host and the VM - this'll make it easier to move files between them. Right-click on the folder icon at the bottom right of the VM window, and select shared folders. Click the 'Add' button at the right hand side of the dialog that appears - fill in the path, name, and options for the share (Uncheck read-only, check auto mount and make permenant)

You'll now find you have a folder on the virtual machine under /media/sf_FOLDERNAME, where FOLDERNAME is the name you gave your shared folder (eg, mine was named Temp, so on the VM it's /media/sf_Temp). You'll also find you can't access it.
To fix this, in the terminal enter

sudo gpasswd -a USERNAME vboxsf


replacing USERNAME with your own username. You will need to reboot for the changes to take effect.

The final step is to allow the host to access the VM's server.
Go to Machine->Settings->Network, and click Port Forwarding on the network adapter you're using (1 by default)
Click Add, then enter the name (HTTP), host port (I chose 8080), and guest port (80). Click OK.
Now when browsing to localhost:8080 on your host machine, you should see the "It works" page of the web server on the VM.

Take another snapshot here.

This was meant to be a short intro to the main development, but seeing as it's already a lengthy post, I'm going to leave it here, and pick up the rest of the development later.

Monday, 31 March 2014

Raspberry Pi LAMP Server

This is a simple guide to setting up a Raspberry Pi computer as a 'LAMP' (Linux, Apache MySQL, PHP) server.

I'll be using it at a later date to create a simple PHP-based To-Do list type system, which I'll cover in later posts.

Download and install the latest version of Raspbian from the Raspberry Pi website.
As I'm using linux, the command I needed was:

sudo dd bs=4M if=2014-01-07-wheezy-raspbian.img of=/dev/sdh

Boot up the Pi and go through initial setup:
  • Expand filesystem
  • Change user password
  • Boot to console
  • Enable SSH Server
  • Force audio through 3.5mm
  • Update tool to latest version
Finish the setup tool and from the command line run updates:

  • sudo apt-get update
  • sudo apt-get upgrade
Install the LAMP stack:

  • sudo apt-get install apache2 php5 mysql-client mysql-server
During this installation process you'll be asked to provide a root password for the MySQL server.
(Optional) Install PHPMyAdmin


  • sudo apt-get install phpmyadmin

This should boot the PHPMyAdmin installer tool:
  • In the first screen select the web server to configure (apache2).
  • Select yes to configure database for phpmyadmin with dbconfig-common.
  • Enter the root MySQL password. Create a phpmyadmin password for the sql server.
Now would be a good time to save the progress made. Shutdown the Pi with
  • sudo shutdown -h now
Once the systems shutdown, remove the memory card and make a backup of the cards data. I use the dd command again to create a gzipped image


  • dd if=/dev/sdh | gzip > /path/to/backups/backupname.img.gz

This allows backups to be restored just as easily, using the command

  • gunzip -c /path/to/backups/backupname.img.gz | dd of=/dev/sdh
If you want to run the Pi 'headless' - without screen or keyboard, now would be a good time to disconnect them.

Once the backup is done, put the SD card back in the Pi, and reboot.
Give it a little while to reboot, and then from your P, connect to it via SSH. On linux this is a command like

  • ssh HostnameOrIP -l pi
Once logged in, you can control the Pi from the command line as you were able to previously.


Monday, 24 March 2014

Motion activated Pac-Man lamp


Version 2

Migrated the control over to an ATtiny25, using the ATtiny cores for Arduino.
I found a ported version of the IRRemote library on Github. It looks as though this port was for a specific purpose, and lacked the IR codes I needed, however it was easy enough to port the necessary parts from the orignal IRRemote library.

This is the revised code:

#include "IRremoteTiny.h"

IRsend irsend;

#define PIR PB2 // pin 7 on ATtiny

void setup(void) 
{
  DDRB &= ~(_BV(PIR)); // Set as input
  DDRB |= _BV(PB3);

}

void loop(void) {
  
  if (digitalRead(PIR) == HIGH) {
    digitalWrite(PB3, HIGH);
    for (int i = 0; i < 3; i++) {
      irsend.sendNEC(0xF7C03F, 32);
      delay(40);
    }
    delay(10000);
    for (int i = 0; i < 3; i++) {
      irsend.sendNEC(0xF740BF, 32);
      delay(40);
    }
  } else {
    digitalWrite(PB3, LOW);
  }
}


Version 1
I was given a Pac-Man lamp for Christmas.
It's a simple RGB colour changing lamp that is remote controlled.


In my flat the hallway can be quite dark, and when leaving, there can be a few seconds between turning the flat's hall light off, and the external hall light being triggered.

I was considering getting a simple plug-in 'night light' type thing, but I didn't want something that stayed on all the time. And besides, I wanted to put Pac-Man to good use.

As it was a Christmas gift, it didn't seem right pulling it apart straight away, so the plan is to create a motion-triggered device that mimics the remote.

The first thing is to find the remote protocol being used.
To do this use a IR receiver module (I scavenged one from a VCR that was on the junk pile), and hook it up to an Arduino, using Ken Shirriff's arduino IR library. Using the 'IRrecvDump' sample sketch you can find the details of the protocol in the serial monitor - it'll look something like

F7C03F
Decoded NEC: F7C03F (32 bits) Raw (68): 15538 8950 -4200 700 -400 700 -400 650 -450 700 -400 700 -400 650 -450 650 -450 650 -450 650 -1500 700 -1500 700 -1500 650 -1550 650 -450 650 -1500 700 -1500 700 -1500 700 -1500 700 -1500 650 -450 650 -450 650 -400 700 -400 700 -400 700 -400 700 -400 700 -400 650 -1550 650 -1500 700 -1500 700 -1500 700 -1500 700 -1500 650 F740BF

Decoded NEC: F740BF (32 bits) Raw (68): 26860 8950 -4200 700 -400 700 -400 650 -400 700 -400 700 -400 700 -400 700 -400 650 -450 650 -1550 650 -1500 700 -1500 650 -1550 650 -450 650 -1500 650 -1550 700 -1500 650 -400 700 -1500 700 -400 700 -400 650 -450 650 -400 700 -400 700 -400 700 -1500 650 -450 650 -1550 650 -1500 700 -1500 650 -1550 650 -1500 700 -1500 650

My first thought that the pacman remote looks very similar to the remotes that tend to get bundled with rolls of RGB leds, and I had a couple of those going spare, so perhaps one of them could've been sacrificed, however performing the same test with those showed that they use a different protocol, and so wouldn't work.

The second thought was to make use of an old Sky TV remote - they have a feature that allows the protocol to be programmed to control several makes of TV, but after a while trying various codes, it appeared that the one I was after wasn't one of them.

So the final option was to use the arduino itself for sending the signal. It seemed a bit overkill, but I can always migrate it to a smaller microcontroller when I have one spare. The IR led was taken from the old Sky remote.

The next stage is to wire in the PIR sensor (motion detector)

I used one of these:


They're a simple 3 pin setup - Vcc, Ground, and Data. Data simply goes high for a few seconds when motion is detected.
It can be easily wired in, just effectively like a simple button.


Here's the code I used - when motion is detected, turn the lamp on for 10 seconds, then off again.

#include <IRremote.h>

IRsend irsend;

void setup()
{
  pinMode(4,INPUT);
  pinMode(13, OUTPUT);
}

void loop() {
  if (digitalRead(4) == HIGH) {
    digitalWrite(13, HIGH);
    for (int i = 0; i < 3; i++) {
      irsend.sendNEC(0xF7C03F, 32);
      delay(40);
    }
    delay(10000);
    for (int i = 0; i < 3; i++) {
      irsend.sendNEC(0xF740BF, 32);
      delay(40);
    }
  } else {
    digitalWrite(13, LOW);
  }
}