Monday, April 15, 2013

Arduino-Powered Maneko Neki

Yet another very practical project that will enrich your life. Extremely hard to make, believe me (just look at the complicated circuit, and you can guess how complicated the code is).



Check out http://arduino.cc and get started yourself.

Sunday, March 3, 2013

Arduino Project - Screen's Brightness Determines Sound (or: JavaScript powered Light Theremin)

I just finished my first Arduino project. Its individual components are very basic, but I love the combination of them.

Check this out:

The Arduino code is fairly simple, it takes the light sensor's value (a value between 0 and 1024 (well, in fact it's a smaller range, because the screen's black is somewhere around 900, and white is about 200)) and maps it to various tones (again a value between 0 and 1024) that I send to the Piezo speaker.

What you can see on the screen is a simple HTML page, whose background is changed for each tone. Here's a sketch of how it works:


var c4 = 0, cs4 = 1, d4 = 2, ds4 = 3, e4 = 4, f4 = 5 ... as5 = 22, b5 = 23;

var melody = [  
  e5, ds5, e5, ds5, e5, b4, d5, c5, a4,a4,a4,
  c4, e4, a4, b4,b4,b4, e4, a4, b4, c5,c5,c5, ...];

function playSong() {
  var intervals = 24; // supports 2 octaves
  for(var i = 0; i < melody.length; i++) {
    var note = melody[i];
    var lumin = 100 - 100/intervals*note;

    // http://stackoverflow.com/a/6425113
    setTimeout( (function(lumin) { return function() { changeBackground(lumin); } })(lumin), i*250);
  }
}


function changeBackground(lum) {
  document.body.style.backgroundColor="hsl(0,0%,"+lum+"%)";
}


Edit 2013-03-09:
I put the source code on GitHub - https://github.com/bodnerdan/js-arduino-theremin  

Looking forward to get some feedback, or even pull requests.
As you can guess, it didn't take me very long to implement this code, but I'd rather see it as a seed for inspiration for better, bigger projects. Let me know if you have used my code as a basis to create something more sophisticated.

Thanks for reading!

Monday, July 2, 2012

Miniature Photo Album with ImageMagick (montage, convert) and Some Shell Scripting

How to develop photos smaller than 13x9, 15x11...

The fork is just there to indicate the size of the photos
When ordering photos online, you usually have the option to choose between formats like 13x9cm, 15x11cm, etc. However, none of these digital photo online services (that I know) offers smaller sizes. This post describes how to easily "merge" 4 JPG files into a big one (containing these 4 files in a 2x2-grid), which you can then order online and cut with scissors (or your other favorite cutting device) in order to create an album with photos of the size 7.5x5.5cm (or smaller, by adjusting some parameters)

Important: The shell script shown below does not touch the original files (it copies and rotates them). However, I don't give any warranty that you don't lose or ruin your original files, so make sure to have a backup!

Prerequisites

  • A set of JPEG images (optimally the same aspect ratio in order to get a seamless montage)
  • An installation of ImageMagick (the executables have to be in the $PATH)
  • A bash-terminal

How to do it

  • Move all the JPG files in a directory
  • cd into this directory and create an executable script with the content from below
  • Execute the script and wait
  • After that, the subdirectory generated contains the files you might want to upload to your online photo service  


#!/bin/bash

g=generated
t=$g/tmp

mkdir -p $t

### STEP 1 - cloning/rotating the file to 'portrait' 
### into the temporary subdirectory(0.jpg, 1.jpg, etc.)
i=0
IFS=$'\n'
for file in `find . -type f -iname "*.jp*g"`
do
 echo "rotating file $file into $t/$i.jpg"
 convert "$file" -rotate '90>' $t/$i.jpg

 let i=i+1
done


### STEP 2 - take 4 JPG files at a time 
### and compile them into new 2x2-images 
i=0
nroffiles=`ls $t/*.jpg | wc -w`
let nrofiterations=nroffiles/4
while [ $i -lt $nrofiterations ] 
do
 ## helper variables to access file names
 let f=i*4
 let a=f+0
 let b=f+1
 let c=f+2
 let d=f+3
 echo "creating a$i.jpg ($a.jpg $b.jpg $c.jpg $d.jpg)"

 ## compile the 4 separate images into a 2x2-grid. 
 ## please change the parameter 768x1024 if you 
 ## don't have a 3:4 aspect ratio
 montage $t/$a.jpg $t/$b.jpg $t/$c.jpg $t/$d.jpg -tile 2x2 -geometry 768x1024 $g/a$i.jpg
 let i=i+1
done

### remove the tmp directory
rm -rf $t



The images created by this script look somewhat like this one (individual pictures taken from http://www.freeimages.co.uk/)


Not very advanced stuff, I know, but I think it's a nice combination of some handy tools to make a nice present.

P.S. 1: I know that there are more elegant ways to write the shell-script, but for my purposes and skills this was the quickest way to go.

P.S. 2:  I think the 7.5x5.5cm format is optimal for a flip book. Wouldn't it be easy and fun to create your own flip book story with the continuous shooting mode of your digital camera?

Friday, April 6, 2012

ImageUploader as Java Web Start

In older blog posts I reported about my ImageResizer/Uploader appplication.
A while ago I turned it into a Java Swing application, startable via Java Web Start.

Short description
It's a tiny but very useful software to resize and upload a bunch of JPG-files to a Google Picasa Web Album. It´s perfectly suitable for travellers in countries with slow Internet connections. The idea behind this software is the following: A full-sized JPG files on a digital camera with (let´s say) 3 MB has only 300 KB if it is resized to 70% of its original size, but has (nearly) the same quality. So Internet-backups can be made 10-times as fast.

You can find it here:
https://sites.google.com/site/dbimageuploader/home

Please don't forget to read the "Background" section:
https://sites.google.com/site/dbimageuploader/background

Tuesday, March 13, 2012

Google Trends for Android Version Names

This Google Trends query shows the trends for the last 5 Android version names (ice cream sandwich, honeycomb, gingerbread, froyo, eclair).
I had to smile when I realized why there's a heart beat pattern for "gingerbread".

 

Tuesday, January 25, 2011

Pragmatic Diary with LaTeX

A while ago, I decided to write something like a personal diary, and wanted to do it with LaTeX. I wanted to have a pragmatic environment, which is established when typing a simple command.

Before I describe the implementation, I would like to describe this environment:
  • When I run the command diary, an instance of kate opens a file named with the current date (e.g. 2010-12-09.tex, located in a directory, let's say $DIARY)
    • If this file exists, then a warning appears, and it is opened
    • Otherwise, this file is created
  • This file contains a skeleton for a LaTeX document, where I immediately can start writing down my thoughts
  • In the terminal-area of kate, I simply have to type pdflatex 2010-12-09.tex to compile the tex-file into a PDF.

And here is how I implemented it.
The template for the LaTeX-skeleton looks as follows (file named $DIARY/template.tex):

\input{./macros.tex}
\begin{document}
\title{}
\maketitle

Here goes the text
\end{document}

The file $DIARY/macros.tex contains document and package definitions, that define the common properties for all tex-files, and hence a unique look for all resulting pdf-files.
Example:

\documentclass[paper=a5,pagesize=auto]{scrartcl}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[ngerman]{babel}
\usepackage{amsmath}
\usepackage{listings}

The script, that launches when typing diary is the following ($DIARY/diary.sh):

#!/bin/bash
TODAY=$(date +"%Y-%m-%d")
BASE=~/Dokumente/texs
TEXFILE=$BASE/$TODAY.tex
PDFFILE=$BASE/$TODAY.pdf
if [ -f $TEXFILE ] 
then
  zenity --error --text "Tex-File $TEXFILE already exists!!!"
else
  cp $BASE/template.tex $TEXFILE
fi

kate $TEXFILE &
cd $BASE
pdflatex $TEXFILE
okular $PDFFILE &

Some notes:
  • zenity simply pops up a dialog box with the given warning
  • okular is my favorite PDF viewer - in fact it could be any other viewer
Finally, I made a softlink to $DIARY/diary.sh in /usr/bin.

By the end of each year I might create a document named diary-201x.pdf, which includes all entries and is a nice compilation of my diary entries. For instance, this could be done with the pdftk. (pdftk *.pdf cat output diary-201x.pdf)

P.S.: You may wonder, why I'm mixing Gnome and KDE software. I'm working with Ubuntu and have some KDE-packages installed. Well, that's my environment, and I'm very fine with that. If you are Ubuntu-User, too, you will find all of the necessary software in the Ubuntu repositories (sudo apt-get install XXX, whereas XXX stands for kate, okular, zenity, pdftkm, etc.)

Monday, January 24, 2011

CropPDF integrated in Nautilus

During my master thesis I had to perform the following task several times: crop a PDF, i.e. remove the outer white borders of the PDF.

A software called pdfcrop (I think it is in the Ubuntu repositories) can do this. From a console the syntax is the following:

pdfcrop input.pdf output.pdf

I wanted to use this feature from my file browser (Nautilus), by right-clicking the file-to-be-cropped and choosing "Scripts -> Crop PDF". Here is how to do this:
  • Create a file called CropPDF located at ~/.gnome2/nautilus-scripts with the following content
  • #!/bin/bash
    pdfcrop $NAUTILUS_SCRIPT_SELECTED_FILE_PATHS \
            $NAUTILUS_SCRIPT_SELECTED_FILE_PATHS
  • Make it executable (chmod +x CropPDF)
  •  
That's it.
Caution: this modifies the file, on which you execute the operation!