Arduino Cookbook on its way!

A Safari Online subscription was one of the best things I took on in 2010. As part of the service, subscribers can access early drafts of Oreilly books. Yesterday, I had a quick look at the upcoming “Arduino Cookbook” and I here is my review.

I really like the Oreilly “Cookbook” series. With most programming books, it seems that the authors assume zero knowledge from their audience and start with variables, for loops, functions and classes etc etc… With the “Cookbook” series, the editors assume a more advanced audience and give solutions to real-life problems. For me, this is great for two reasons: first of all, due to my busy schedule and short attention span, I like being able to read 5-10 pages and feeling I’ve actually learnt something. The second reason is that these books, by giving us “recipes”, i.e. solutions to real-life problems, effectively teach us how to solve problems.

Now, the questions are “how the new Arduino Cookbook compares to other arduino books” and “is it a worthy member of the Cookbook series”?


Overall, I have to say, I was very impressed. The authors opted to start with simple recipies on problems most rookies stumble upon, but at around page 100 things start getting interesting, with recipies that interface Arduino and Processing, a very good section on lcds, plus everything you may decide to connect an arduino to, from various sensors to servos, gps receivers etc.

The book is true to its Cookbook roots and gives practical advice on problems that arduino users will have at some point to deal with. I found numerous recipies on problems I had come across in the past and had spent hours on forums trying to figure out solutions.

Another good point about this book is that it’s using the new Arduino Uno.

Is this the best Arduino book to date? Yes. It’s better than “Practical Arduino” when it comes to quantity and quality of examples and more useful in the long term than Masimo Banzi’s “Getting Started with Arduino” which is a very basic intro to the subject. Should it be the first book on Arduino one should buy? No. This is a “cookbook” and expects some understanding of the subject. I still thing Tom Igoe’s book “Making Things Talk: Practical Methods for Connecting Physical Objects” is the best and most inspiring intro to the subject, but the Arduino Cookbook goes further when it comes to practical advice and knowledge that can be transfered to many different projects. It does stick to its “Cookbook” roots!

With the arrival of the iPad, I have stopped buying real books, as the ebooks are cheaper and the Safari Online subscription service is great. The Arduino Cookbook will be one of the few real books I will personally buy this year as I think it’s a book worth having on my library.

Python & Gnuplot & AHK & Processing

Gnuplot is a great command line tool that creates very quickly graphs from data files.

The official site of Gnuplot is: http://www.gnuplot.info/ . To see examples of graphs produced by Gnuplot, you should have a look here: http://gnuplot.sourceforge.net/demo_4.4/.

Using python and gnuplot, I produced approx 80 graphs similar to the one below, that fitted nicely on 5 A4 pages. gnuplot_sample

Python scans a very long file with all the historical data and passes the required data for each graph to gnuplot. The result is 80 graphs as png files in less than a minute. Then another script in processing puts everything together in a 5 page pdf file (2 cols x 8 rows = 16 per page). AutoHotKey is what “glues” everything together and makes this a “single click” solution.

DECODE – Exhibition at the V&A Museum…

Yesterday, I visited the Decode exhibition at the V&A museum http://www.vam.ac.uk/microsites/decode/

This exhibition is about creating interactive pieces using modern technology. It’s a rather small exhibition, probably due to the fact that few artists have embraced the new technologies currently being developed.

Many of the exhibits I saw, I was already familiar with:

Flight Patterns (one of my favourites) – http://www.aaronkoblin.com/work/flightpatterns/index.html

We Feel Fine – http://www.wefeelfine.org/

Weave Mirror,  similar to the wooden mirrors from the same artist Рhttp://smoothware.com/danny/woodenmirror.html

Flow 5.0 – http://www.studioroosegaarde.net/work_html.php?id=21&picture_id=1399

Many of the exhibits are using Processing, open Frameworks,  Arduinos (or other similar devices).

I loved this exhibition; my only issue was that it wasn’t big enough…

PIR sensor + Arduino + Processing + Skype…

This post is about a simple and interesting combination of software and hardware.

Last week, I picked up a couple of PIR sensors from Ebay.

pir2_w500

PIR sensors are (usually) easy to interface with an arduino board.
Red: 5V, Black: Ground, Yellow: Analog In. Could it be easier than this?

PIR_w500

After a bit of experimentation, I found that the board sends a pulse as soon as it detects some sort of movement. Also, it sends a pulse at the very beginning, when it sort of “boots”.

My ultimate aim was to start Skype as soon as movement was detected.

I loaded the standard firmata sketch on my arduino, see: http://arduino.cc/en/Reference/Firmata. Basically, the arduino simply sends all the analog port readings to Processing.

The following Processing code is simply a proof of concept. The sketch starts, looks for an arduino at the second serial port of my PC, waits for 5 secs for the PIR to send its first pulse and then, if movement is detected calls a number, using Skype.

At any moment, in the middle of the screen, you see the value of the PIR sensor.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import cc.arduino.*;
import java.io.*;
import processing.serial.*;
 
int analogPin = 5;
int myV; // PIR sensor value
 
PFont font;
Arduino arduino;
 
//////////////////////////////////////////////////////////////////////////////
void setup(){
  size(400, 300);
  font = createFont("ArialUnicodeMS-12.vlw",22, false); 
 
  //my board is the 2nd serial device, hence Arduino.list()[1]
  println(arduino.list());
  arduino = new Arduino(this, Arduino.list()[1], 57600);
}
 
//////////////////////////////////////////////////////////////////////////////
void draw(){
    background(0);
    fill(255);
 
  // the PIR sensor is sending out HIGH signal in the first 5 secs
  // so we will ignore all the early signals
  if(millis() < 5000){
 
  }
 
  else{
    // myV is the scaled value of the PIR sensor
    myV = arduino.analogRead(analogPin);
 
    textFont(font, 22); 
    textAlign(CENTER,CENTER);
    text("PIR sensor value: " + myV,width/2,height/2);
    text("Active for " + round(millis()/1000) + " seconds",width/2,height*3/4);
 
    callme();
  }
}
 
//////////////////////////////////////////////////////////////////////////////
void callme(){
 
  // you should be logged in on Skype and have some credit
  // if your are dialling an external line
  String mySkype = "C:\\Program Files\\Skype\\Phone\\skype.exe /nosplash /callto:+44798000000";
 
  if(myV>100){
 
    print(" MyV: " + myV + " ");
 
    try {
      String line;
      Process p = Runtime.getRuntime().exec(mySkype);
      p.waitFor();
      System.out.println("EXIT: " + p.exitValue());
    }
 
    catch (Exception err) {
      err.printStackTrace();
    }
 
      delay(3000); 
  }
 
 
 
}
//////////////////////////////////////////////////////////////////////////////

Here is a very short video of this test in action:

PIR Sensor – Test from Arkadian.Eu on Vimeo.

Arduino Project: Intervalometer for Fuji cameras

This post is about an arduino-based intervalometer I built for my camera, a Fuji S9600 dSLR.

I decided to go for a very simple interface: a rotary switch in the middle of a plastic box which would allow me to select 12 pre-defined intervals. I went for a switch and not a potentiometer because I wanted to be certain about the option I select: the reassuring “click” of the rotary switch does exactly that!

Also, I wanted to make the device easy to use, so the labelling of the different options around the rotary switch was very important too.

The Fuji S9600 (a.k.a. Fuji S9100 in the States) has a mini-B USB connector on the side which is used, amongst other things, for a remote shutter release cable. Bob Mahar has “dissected” an original Fuji RR-80 remote release cable and has revealed that the device is actually very simple inside; Bob’s great page on the Fuji RR-80 is here: http://www.trafficshaper.com/RR-80/RR-80.html.

Based on Bob’s page, I came up with the following diagram for the RR-80:

circuit_rr80_5_w500

The circuit of the RR80 is simple to build, but I couldn’t find a suitable mini-B USB plug: remember, the camera uses the 5th pin, which is not always available on the plugs you can buy online or at Maplin’s.
http://en.wikipedia.org/wiki/Universal_Serial_Bus
http://en.wikipedia.org/wiki/File:Types-usb_new.svg

After a couple of failed attempts to source a 5-pin USB mini-B connector, I ended up buying a cheap Chinese RR80 clone:

dscf0890_w500

I was actually lucky, because this model has a 2.5″ jack on the side, which allows two cameras to be sync’d (not all the clones have this, I think the JJC clones marked “MA” have it). This meant that I could use a 2.5″ stereo connector in order to connect the arduino with the remote. All the arduino has to do in order to take a photo is to short 2 of the 3 parts of the stereo connector.

dscf0893_w500

I used an old Sparkfun Skinny arduino (it is called “Arduino Pro” now and it’s not red anymore):

dscf1065_w500

This is a 12-pole rotary switch from Maplin and 12 resistors:

dscf1050_w500

I found a simple plastic box at Muji:

Muji Box

First things first: I converted the rotary switch into a “potentiometer”:

dscf1097_w500

In case it’s not clear from the image above, I connected all the pins in the periphery with resistors. The yellow cable coming out from the middle pin of the switch goes to the analog In pin (pin 5 here). The black cable coming out from the 12th pin of the switch goes to the ground and the red cable coming out from the 1st pin of the switch goes to the 3.3V arduino output.

At the opposite side of the arduino board, I used a blue LED on pin 13 (and ground) and on pin 12 I connected a transistor, 2N222A: http://en.wikipedia.org/wiki/File:2n2222A_and_schema.jpg) and the cable (twisted red-black) from the 2.5″ connector.

dscf1078_w500

Here is a better diagram of the wiring of the transistor (to keep things simple, only the transistor is shown here):
transistor1

For more information on the use of transistors as switches: http://en.wikipedia.org/wiki/Bipolar_junction_transistor

After a few minor tweaks, the prototype was up and running:
dscf1077_w500

The code loaded on the arduino is pretty simple really:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
///////////////////////////////////////////////////
int swPin  =  5;  // 12-step switch
int ledPin = 13;  // Blue & pcb leds
int camPin = 12;  // Camera pin
 
int sw    = 0;  // 12-step resistance value
int stage = 1;  // 12-step stage (i.e. 1 - 12)
 
// this delay is specified by the rotary switch
int  delaySec;   
 
// this is the current second count
long currentSec = 0;   
 
// this is how long the shutter switch will be on
long shutterSec  = 100; 
 
///////////////////////////////////////////////////
void setup() {
 
  // declare the swPin & ledPin as an OUTPUTs
  pinMode(ledPin, OUTPUT);  
  pinMode(swPin,  OUTPUT);
 
// the serial is only used for debugging and for
// the calibration of the rotary switch  
//  Serial.begin(9600); 
 
}
 
///////////////////////////////////////////////////
// I 
void loop() {
  readSwitch();
  timeCheck();
 
// printLine is only used for debugging and for
// the calibration of the rotary switch
//  printLine(); 
}
 
///////////////////////////////////////////////////
///////////////////////////////////////////////////
void timeCheck(){
  if(delaySec<=currentSec){
 
    digitalWrite(ledPin, HIGH);  // turn the ledPin on
    digitalWrite(camPin, HIGH);  // turn the camPin on
 
    delay(shutterSec); // keep the led and the switch on
 
    digitalWrite(ledPin, LOW);   // turn the ledPin off
    digitalWrite(camPin, LOW);   // turn the camPin off
 
    delay(1000-shutterSec);
    currentSec = 0;
  }
 
  else{
    delay(1000);
    currentSec = currentSec + 1;
  }
}
///////////////////////////////////////////////////
void readSwitch(){
 
  // read the value from the sensor
  sw = analogRead(swPin);  
 
// the serial is only used for debugging and for
// the calibration of the rotary switch 
//  Serial.print(sw);
 
  if(sw>=0  && sw< 20) {
    stage =  1; 
    delaySec =   10;
  } // 10 secs
  if(sw>=20  && sw< 110) {
    stage =  2; 
    delaySec =   20;
  } // 20 secs
  if(sw>=110  && sw< 200) {
    stage =  3; 
    delaySec =   30;
  } // 30 secs
  if(sw>=200  && sw< 290) {
    stage =  4; 
    delaySec =   60;
  } // 1 min
  if(sw>=290  && sw< 380) {
    stage =  5; 
    delaySec =   90;
  } // 1.5 mins
  if(sw>=380  && sw< 480) {
    stage =  6; 
    delaySec =  120;
  } // 2 mins
  if(sw>=480  && sw< 570) {
    stage =  7; 
    delaySec =  180;
  } // 3 mins
  if(sw>=570  && sw< 660) {
    stage =  8; 
    delaySec =  300;
  } // 5 mins
  if(sw>=660  && sw<750) {
    stage =  9; 
    delaySec =  600;
  } // 10 mins
  if(sw>=750 && sw<850) {
    stage = 10; 
    delaySec =  900;
  } // 15 mins
  if(sw>=850 && sw<950) {
    stage = 11; 
    delaySec = 1200;
  } // 20 mins 
  if(sw>=950 && sw<1024) {
    stage = 12; 
    delaySec = 1800;
  } // 30 mins
}
///////////////////////////////////////////////////
void printLine(){
  Serial.print(" - Stage: ");
  Serial.print(stage);  
  Serial.print(" (");
  Serial.print(delaySec);    
  Serial.print(") - Next shot in: ");
  Serial.print(delaySec - currentSec);    
  Serial.println();
}
///////////////////////////////////////////////////

In order to calibrate the rotary switch, I had enabled the printLine() function (which is commented out in the loop() above and I got the following results on the terminal window (the first number on each line is the pin5 analog in reading):

serial_settings

Now it’s time to put everything in the box. I used my soldering iron to make the necessary holes on the box (I have a tip which I only use to make holes on plastic). Here is everything placed in the box for the first time (to make sure that everything fits the way it should).

dscf1099_w500

dscf1090_w500

The black surfaces you see on bottom of the box are foam boards; you can find them at art supplies shops. The foam boards are easy to cut and shape and, in this case, prevent the battery and the remote control moving around the box.

Bare box #1

My next task was to come up with a way of indicating the time interval of each one of the 12 stages of the rotary switch. I didn’t think twice – this was a nice and easy job for processing. I created this image, which I got printed as a 5″ x 7″ in matt paper at photobox.com (10 s = 10 seconds, 1 m = 1 minute):

frame_w500

The Processing code below created the above image:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
PFont myFont;
PFont myFont2;
 
float thisStage;
float nextStage;
 
float tp24 = TWO_PI/24;
 
void setup() {
  size(750, 1070);
  background(255);
 
  smooth();
  noStroke();
 
  float adj = -17 * tp24; 
  float space = TWO_PI/120;
  int diameter = 400;
 
  String[] stage = {    
    "10 s", "20 s", "30 s" , "1 m", 
    "1.5 m", "2 m", "3 m", "5 m", 
    "10 m", "15 m", "20 m", "30 m"  };
 
 
//remember to create the Arial-BoldMT-48 font
//otherwise this is not going to work...
  myFont = createFont("Arial-BoldMT-48", 18,true);
  myFont2 = createFont("Arial-BoldMT-48", 38,true);
 
  for (int i = 0; i < stage.length; i++){
 
    thisStage = (TWO_PI * i /12  - adj);
    nextStage = (TWO_PI *(i+1)/12 - adj);
 
    fill(0,50,100);
    noStroke();
    arc(width/2, 225, diameter, diameter, thisStage, nextStage);
    fill(255);
    arc(width/2, 225, diameter, diameter, nextStage  - space, nextStage);
    arc(width/2, 225, diameter, diameter, thisStage, thisStage + space);
 
    fill(150);
    stroke(150);
    textFont(myFont); 
 
    textAlign(CENTER,CENTER); 
    text(stage[i],(width/2) + diameter * 0.43 * cos( thisStage + tp24 ), 
225 + diameter * 0.43 *sin(thisStage + tp24 ));   
 
  }
 
  //this is the middle white circle
  fill(255);
  ellipse(width/2, 225, diameter * 0.6, diameter * 0.6);
 
  fill(0,50,100);
  noStroke();
  rect(0,0,150,150);
  rect(width-150,0,150,150);
  rect(0,450,width,150);
  rect(0,height-150,width,150);
 
  fill(0);
  stroke(0);
 
  textFont(myFont2); 
  textAlign(CENTER,CENTER); 
  text("Arduino-based Intervalometer 
\n for Fuji Digital SLRs", width/2,height -150 - (450/2) );   
 
  textFont(myFont); 
  textAlign(CENTER,CENTER); 
  text("www.arkadian.eu", width/2,height -200);   
 
  fill(0,50,100,100);
  noStroke();
  for(int x = 1; x < 30; x++){
    rect(int(random(0,width)),int(random(450,height-150)),20,20);
  }
  saveFrame("frame.png"); 
}

The 7″ x 5″ prints in matt paper (the glossy photo paper would get fingerprints):

dscf1121_w500

I had to make two more holes at the top part of the box to secure the cover.

Here is my intervalometer in its final form.

dscf1136_w500

dscf1125_w500

As you can see from the photo below, the intervals are clearly visible:

dscf1131_w500

That’s it, the end… In the next few weeks I will post a couple of videos put together using frames taken with this brand new intervalometer.

This must be my longest post ever…