I’m spending the day today at IndieWebCamp SF Decentralized Web Hackers Day hosted in Mozilla’s San Francisco office as a lead-up to this week’s Distributed Web Summit.  My personal adventure today is getting up to speed on IndieWeb basics and enabling as many of them here on orangemoose.com as time (and trial & error) allows.

Decentralization is one of the core aspects of the IndieWeb, which aims at enabling a people-focused alternative to today’s very (if not alarmingly) centralized “corporate web”, but doing so by applying familiar, existing technologies in careful ways that keep the user in control of their data including identity, posts, code, created content, location, and more.

Happily today’s Hackers Day is set up to encourage hands-on experimentation and adoption, and the room is full of wonderfully knowledgeable people eager to help.   Look for another post after the day is done, and we’ll see how well I do.

Exploring Arduino Interrupts

I’ve been wanting to add wind & rain sensors to my home-built Arduino-powered weather station and thanks to a recent sale at Sparkfun have acquired their nicely-built set.  As a result, though, I have two interesting problems to solve in getting those sensors integrated to the temperature and barometric pressure ones I have deployed today.  First, I need to work out where and how to install the wind/rain sensors as placement is critical in getting good readings  Of more immediate concern, however, is that I need to rethink my weather station application in order to properly integrate the new sensors, especially the anemometer and rain gauge.

At the moment my weather station code uses basic clock timing to read temperature and barometric pressure sensors periodically, average their values, and display the readings on a small attached OLED. Temperature and pressure sensors are continuous so their values are always available and can be read whenever desired just based on the Arduino’s internal clock. While my new wind vane sensor also provides a continuous reading, the new anemometer and rain gauge don’t work that way.  Instead they report data discontinuously whenever they have something to share.

In the case of the anemometer it closes an internal switch every time the cups rotate through one revolution.  Deriving wind speed from the anemometer means counting the number of revolutions over some specific period of time and calculating the number of revolutions per second. Multiplying revolutions-per-second by a fixed factor based on the size and geometry of the anemometer lets you determine average wind speed over that period of time.  You can’t simply poll the anemometer and ask it to tell you what it is observing as you can with temperature, pressure, or wind direction sensors.

Similarly the rain gauge can’t provide a continuously-available reading, but instead closes a switch every time its internal self-emptying bucket fills and dumps.  The volume of the tiny bucket is known, so keeping track of how many times it has emptied over a specific period of time lets you figure out how much rain has fallen.

Therefore a different approach is needed to read sporadic sensors like the anemometer and rain gauge and keep track of every time their internal switches close, whenever that turns out to be and no matter how often.  The asynchronous, switch-closing nature of those sensors is a perfect case for interrupt-driven data gathering.

A good overview on interrupts and how they work on Arduino is available over at EngBlaze. In my case I need to modify my weather station code to add logic to handle the calculations to be done whenever either the anemometer or rain gauge closes their internal switch. Just as with reading my other sensors, I need to devote an Arduino pin to each of the anemometer and rain gauge so I can detect their respective switch closures.  This turns out to be pretty easy as Arduino has already set aside pins to handle two external interrupts: interrupt number 0 (or INT0) on digital pin 2 and interrupt number 1 (or INT1) on digital pin 3.  All you need to do is connect the interrupt-generating device to one of those two pins, write a simple handler function you want to be called every time an interrupt occurs, and use the special built-in attachInterrupt function to associate your handler with the chosen pin.

A simple example is provided in the Arduino reference for attachInterrupt(). Here’s another one aligned with my intent to use interrupts to read my anemometer and calculate wind speed:

/*
 * Example program for Arduino external interrupts, in this case to read
 * an anemometer connected to digital pin 2 (which is INT0).
 *
 * Author: David Bryant (david@orangemoose.com)
 */

volatile int interruptCnt = 0;  // Counts anemometer interrupts

unsigned long sampleDelayMs = 30*1000; // Sample interval, milliseconds
unsigned long prevSampleMs = 0;        // Timestamp for previous sample

void setup()
{
  Serial.begin(9600);
  Serial.println("Gathering data...");

  // Attach our interrupt service routine to INT0
  attachInterrupt(0,anemom_cnt,RISING);
}

void loop()
{
  unsigned long currentMillis = millis();    // Get current timer value
  /* See if it is time to calculate wind speed */
  if( (currentMillis - prevSampleMs) >= sampleDelayMs) {
    Serial.print("Anemometer count: ");
    Serial.println(interruptCnt);

    prevSampleMs = currentMillis;   // Remember clock time
    interruptCnt = 0;               // Reset counter
  }
}

/*
 * Interrupt service routine called when the anemometer switch closes.
 * All it needs to do is increment the interrupt counter.
 */
void anemom_cnt()
{
  interruptCnt++;
}

This works well enough and is quite straightforward, but I’m not completely happy with it.  To add my anemometer and rain gauge I need two interrupts, but I’d like to make broader use of interrupts in my weather station code. For example, I’m currently using a push button to step through a series of screens on an attached OLED so a user can browse a variety of information from the sensor including maximum and minimum readings, system statistics, etc.  At the moment that push button is read periodically as part of the sensor timing loop but it’d make more sense to have it generate an interrupt to trigger stepping to the next screen.  It may also make sense to use interrupts to handle other sensors I could add, or support other system functions. Using all the available external interrupts just for the anemometer and rain gauge seems short sighted.

Happily the Arduino supports handling interrupts on every pin, both analog and digital, through a different mechanism than INT0/INT1 and their attachInterrupt function.  That means there’s lots of room for expansion, but it requires a bit more low-level knowledge and coding.  That’s a good subject for another blog post…

Thanks to special help from a good friend my son and I had a chance to attend this year’s PAX Prime conference.   In 2004 Jerry Holkins and Mike Krahulik, creators of the “Penny Arcade” web comic, decided the world needed a conference solely dedicated to celebrating gamer culture and the Penny Arcade eXpo (PAX) was born.  Originally hosted just in Seattle, PAX is now a global phenomenon with events in Boston (PAX East), Melbourne Australia (PAX Australia) and still in Seattle (PAX Prime).  As long-time game players my son and I had talked about attending PAX but had never managed it, so were absolutely delighted when the opportunity arose this year.

Naturally the first year at a conference is a major learning experience, especially a large and diverse one like PAX Prime.  We were only attending for just over one day out of the four days overall so had to make some compromises in what we attempted to do.  Here are some highlights of our rookie experience, which will definitely help us plan for return visits (if we’re lucky enough to get tickets) and perhaps inform others as well.

See new games – Game publishers like to use PAX to showcase games that are close to being launched. This means attendees have lots of opportunities to take a look at pre-released game content. This year we saw/played “Borderlands 2: TK Baha’s Bloody Harvest“, “XCOM: Enemy Within“, “Wolfenstein: The New Order“, and some great indie games such as “Escape Goat 2” and “Audiosurf 2“.

Try existing games – In addition to unreleased games PAX offers ample opportunity to play current games that you just haven’t had the time (or money) to try.  We checked out “Dragon’s Prophet“, “Assassin’s Creed IV“, “The Bureau: XCOM Declassified“, “Rogue Legacy“, “Portal 2“, “Left 4 Dead 2“, and “Transistor“. Often these games are used to help demo gaming gear in vendor booths.  The NVIDIA booth was a great place to play games while at the same time see new graphics cards and handhelds.

Visit the Free Play areas – When you need a break from the expo and panels go find one of the free play areas and plop down in front of a PC or console to unwind with some games.  The PC area has hundreds of high-end machines each with dozens of pre-loaded games and while we didn’t visit the console area I presume it’s equally large.   Free play is limited to 45 minutes at a time so while there may a line of folks waiting for a chance to play the line moves pretty steadily.  The area is managed by a timekeeper that can allocate groups of people to adjacent available computers, enabling co-op group free play if you like.  (We had all kinds of trouble with the voice chat set-up so being next to each other is the only way you can game together.)  Free play areas are open late into the evening so you can get in some game play after the expo closes and panels end, though lines are longer then as a result.

Check out new gamer gear – Vendors like Corsair, NVIDIA, Skullcandy, Kingston, and Logitech, are on hand to show off current and new gaming gear including graphics cards, keyboards, mice, headsets, and accessories (like the Stinky Footboard and Cooler Master Skorpion). It’s great to be able to ask in-depth questions, try everything out, and compare performance in real game situations. You may also be able to take advantage of special show prices or packages.

Go Indie – The “Indie Megabooth” is a special area of the expo set aside just for indie games, and you should definitely spend time there to talk to the developers and share in their creative energy.  Equally important is the “PAX 10“, a collection of ten indie games selected by a panel of experts for exemplary game play and fun.  The PAX 10 was not part of the Indie Megabooth so we made sure to reserve time to see both areas.

Decide how much you want to stand in line – Mobs of people will stand for hours in long lines just for a glimpse at a new game or a chance at some special give-aways.   Ask what a line is for and decide how long you’re willing to wait.

Collect & Scavenge – Acquiring and trading collectable pins and buttons has long been an aspect of PAX Prime and it has become a formal part of the conference through “Pinny Arcade”. You’ll see lots of folks wearing lots of pins and buttons, and they’re generally looking to trade to grow their collection. Also, several vendors offer scavenger hunt promotions in which you need to visit different booths, collect stamps or information (or both), then redeem the full set for some prize.  PAX Prime does this too by hiding special QR codes throughout the venue and encouraging you to collect them all and earn a special “PAX XP” prize.

Overall PAX Prime was an extraordinary experience and is well worth attending for any gamer even if you can only get there for a day.

I’ll have more to add in a separate post on my impressions of particular games and gear.