Arduino Experiment 1 : Volume Slider for OSX using Eventmachine

At the moment I am experimenting a lot with Arduino. Arduino is an open-source electronics prototyping platform that helps you to build hardware solutions in a very short time.

Arduino can be used as a stand alone solution and can run software on a little chip that is onboard of the circuit. You can write a little weather station with it or create a little device that twitters messages if a certain input is triggered. There are tons of examples for this online. It’s also possible to connect it to your pc and use it as an input device for your program. One solution to react on input from the Arduino board is Eventmachine, an event driven I/O framework for ruby.

This is a little program that reads data from the serial port and sets the volume of my mac:

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
require 'serialport'
require 'rubygems'
require 'eventmachine'

if ARGV.size < 1
  puts 'ruby example.rb /dev/tty.your-usbdevice'
  exit 1
end

sp = SerialPort.new(ARGV.shift, 9600, 8, 1, 0)

EventMachine::run do

  EventMachine::defer do
    loop do
      line = sp.gets
      if line
        puts "New volume : #{line}"
        `osascript -e "set volume #{line}"`
      end
    end
  end

end

sp.close

Before you run it, you need to install the needed gems with gem install eventmachine serialport. After that you can start the script with ruby example.rb /dev/tty.your-usbdevice.

But what will it read? At the moment it doesn’t read anything because the Arduino isn’t transmitting data to it. We need a little program that runs on the Arduino and computes the input and sends a new volume to the serial port. That code is a little bit longer because it has to reduce the jitter from the data. You don’t want the volume to constantly switch between 50% and 52%, am I right :) ?

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
// analog pin used to connect the potentiometer
#define POT_PIN        2  

// READ_AVG is how many readings to average
// set it to one less than the actual #
// e.g.: 10 readings = set to 9
//
// the more you average, the more accurate your reading is likely to
// be -- too many though, and you'll start missing changes if the motor
// is moving quickly
#define READ_AVG       9

// variable to read the value from the analog pin
int val;    

// our current and previous potentiometer readings
int cur_reading = 0;
int pre_reading = 0;
int steps   = 0;

// our current readings array, and our previous average readings array
int vals[READ_AVG + 1];
int prev_posts[2]  = { 0, 0 };

void setup() {
  Serial.begin(9600);           // set up Serial library at 9600 bps
}

int read_pot() {
  //read the voltage on the potentiometer:
  cur_reading = analogRead(POT_PIN);
  int diff = 0;
  int post = prev_posts[1];

  // we're going to average the last READ_AVG reads
  // put in a value for our current step
  vals[steps] = cur_reading;

  // if we've saved enough values to go ahead and perform an average...
  if( steps >= READ_AVG ) {
     // reset our read counter
     steps = 0;

     // determine the average value read
     // -- this is mostly to deal with big jitter
     int tot = 0;
     int avg = 0;

     // sum up totals
     for (int i = 0; i <= READ_AVG; i++) {
       tot += vals[i];
     }

     avg = tot / READ_AVG + 1;

     // ignore current reading if it was either of our last two readings
     // avoid bouncing back and forth between two readings (slight voltage
     // variation in the same range)

     if( avg == prev_posts[0] || avg == prev_posts[1] ) {
       return(post);
     }

     // determine the absolute difference between the current average
     // and the previous average
     diff = avg > prev_posts[1] ? avg - prev_posts[1] : prev_posts[1] - avg;

     // if there's a difference between the averages
     if( diff > 0 ) {
       // move our last reading back, and put our current reading in
       // our array to track the last two positions
       prev_posts[0] = prev_posts[1];
       prev_posts[1] = avg;
       post = avg;
     }
  } else {
    // increment our saved value # for the next loop
    steps++;
  }
 
  return(post);
}

void loop() {
  // reads the value of the potentiometer and scale it to use it for volumes (between 0 and 7)
  int newVal = map(read_pot(), 0, 1023, 7, 0);    

  if (val != newVal) {
    val = newVal;
    Serial.println(val);
  }

}

Now you have to compile and upload the code ( For details on how to do this, there is a good documentation about it ).

Connect a potentiometer to +5, GND and Pin 2 and after that it should look something like this:

More links around Arduino can be found in my delicious account.

Posted in Development | Leave a comment

The Passionate Programmer

The last book for this week is The Passionate Programmer by Chad Fowler.

I really want to travel back in time and give this book to my younger self. The one that was bored in his job and yelled at by a few stupid managers. To quit that job was one of the best things that happend to me. But I was forced to do it and should have done it earlier.

After reading this book you will reevaluate your live as a developer. Your chosen path. You will recheck everything. In short: this book tries to get you passionate again for your job. Don’t just do it, love it.

You will see yourself with different eyes after reading this book. At least I did. Now I have to act on it.

Posted in Books, Development | Leave a comment

Next Ruby and Rails Usergroup Meetup

The Cologne Ruby and Rails usergroup will meet on Thursday, 24 March at 20:00 at the Coworking Cologne.

We will have a talk by Jan Lühr about JRuby and an open mike session afterwards. If you want to talk to ruby developers, show your newest work or just have a beer with some great people, you have to be there :) .

Posted in Development | Leave a comment

Eloquent Ruby

At the moment I am sick at home and use the time to finally read some books that where on my to-do list. The first was “Eloquent Ruby” by Russ Olsen.

It got very positive reviews around the net, including Peter Cooper, who said:

Eloquent Ruby [...] is the first Ruby book I’ve read in its entirety within 24 hours; it’s that good.

There is also a positive review by Antonio Cangiano.

What’s it all about? Basically Eloquent Ruby doesn’t try to teach you to write Ruby code, it tries to teach you how to write good Ruby code. Not only by telling you how to write it, but also by explaining why the Ruby world choosed that certain way to do it. I have read it in one day and didn’t learn much while doing so. But it was a good reminder on how and why we Ruby Developers do the stuff we do. And it was a nice read nevertheless. Russ is a talented and very funny author. I would definitively read a book written by him even if it’s about fly-fishing :) . Even better: he reacts on tweets about his book.

Strong buying advice for everybody who just started to learn ruby or wants to feel more safe while coding in it.

Posted in Books, Development | Leave a comment

Yeah. The new projector.

Thanks to everyone who donated money for the video projector.

Here it is, in it’s full glory :) :

CoWoCo Cologne Projector
CoWoCo Cologne Projector

And now let’s start using the new baby. The first group that will be able to use it is the cologne.js on Tuesday.

Have fun with it!

Posted in Development | Leave a comment

A video projector for CoWoCo

If you look at hacking-cologne you will see that a lot of user group meetups are in a location called CoWoCo. It’s a really nice place with a lot of friendly people and enough Club Mate and Afri Cola for everyone. But one important thing is missing. A video projector.

This has to be fixed! If you want to help us, please consider donating here:

Click here to lend your support to: Beamer fürs CoWoCo and make a donation at www.pledgie.com !

Thanks!

Update: Thanks to everyone who helped getting the money for the video projector!

Posted in Events | 1 Comment

I *hate* the amazon start page

Sorry, but I really do. Every time I am searching for stuff on amazon it will have an effect on the start page. Articles that are similar to the search will show up. Regardless on how embarrassing they might be. I regularly search for gifts and I really don’t want those guys on my landing page.

Thinking of buying a present for your girlfriend? Never open amazon when she is around you. Even if you bought the present a few days ago. Amazon will ruin the surprise and show related products on your start page. Even worse: if a friend sends a link to an amazon product as a joke, it will show up there, too.

I won’t open amazon when other people look over my shoulder. Never. Because of that.

Posted in Websites | Tagged | Leave a comment

Promote JS

JavaScript JS Documentation: JS Function arguments, JavaScript Function arguments, JS Function .arguments, JavaScript Function .arguments The search for javascript on google leads to very bad sites, “promote js” tries to change this by promoting people to link to the mozilla documentation.

You should add this, too!

Posted in Development | Tagged | Leave a comment

Check MySQL Master-Slave replication

A lot of guides explain that you can use MySQL master-slave replication to scale your database performance. But nobody explains how to keep the database instances in synch. MySQL replications are very shaky and tend to break and mess up stuff very easily. From my standpoint the whole replication scheme looks like a smart hack into the MySQL system.

Till today I used some self written scripts to check our systems. Nobody told me that there was a perfect toolchain for this problem already existing: Maatkit. Maatkit offers a few commandline tools that help you to keep the slaves in synch with your master.

Install it using brew:

1
brew install maatkit

Check tables for differences :

1
mk-table-checksum h=HOSTMASTER,u=USERMASTER,p=PASSWORDMASTER h=HOSTSLAVE,u=USERSLAVE,p=PASSWORDSLAVE -d DATABASE | mk-checksum-filter

And after you found differences you can even use Maatkit to repair those tables:

1
mk-table-sync --execute --sync-to-master h=SLAVEIP,u=USER,p=PASSWORD,D=DATABASE,t=TABLE

Besides repairing damaged tables Maatkit helps you to check if the slave replication is still working. Mk-heartbeat updates a heartbeat table in the master database and checks if the slave database has been updated. I have written a small script that wraps the heartbeat check and sends a mail if the slave replication has stopped.

More documentation can be found here and a few examples in this presentation.

And yes, using couchdb would make this toolchain obsolete :) .

Posted in Development | Tagged , | Leave a comment

Events for developers around Cologne

I was tired to search for events in Cologne that could interest me. Others might have the same problem, so I created a shared calendar:

If you know events in the Cologne area (including Düsseldorf and Bonn), feel free to leave me a Note.

Update: You can now find the calendar on this site.

Posted in Events | Tagged , | 3 Comments