Category Archives: Programming

Arduino Hot Wheels Drag Strip Race Track

For my son’s second birthday I decided to introduce him to die-cast cars and what better way than building a drag strip race track with an electronic start gate, timing and race results?

While Hot Wheels does offer a 6-Lane Raceway and a number of other drag strip style tracks, they didn’t have the timing and electronic start gate that I knew an Arduino and some other bits could provide, so I set to work. The basic idea is was have a servo motor open the start gate by pulling down a hinged plate with dowel stoppers to release the cars and a photocell (photoresistor/LDR) pointed at an infrared LED on each track to detect each car crossing the finish line.

Some quick searching revealed a number of projects from which I could draw insight and inspiration, most useful were apachexmd’s Hot Wheels Track Timer and Robby C’s Ultimate Guide to Building a Hot Wheels Race Track. This demystified a number of points, I hadn’t really had any fun with die-cast cars since I was kid myself, turns out there are some oddities.

Tracks

One of the oddities is that Hot Wheels doesn’t offer any long track for sale, all of their tracks are sold in segments a foot or two long that must be connected — the only exception being a vintage 50-foot Track Pak Raceway sold at some point in the 70′s or 80′s which can be found for upwards of $100 currently. Another track option is BluTrack which is sold in many lengths but only in a two-lane configuration for some reason. I decided to buy the readily available Hot Wheels segments in the form of 4 Hot Wheels Track Builder Straight Track kits.

I had some 1/2″ x 8″ pine left over from framing a door I thought I would use to mount the track on. There are a number of methods folks have employed to mount the Hot Wheels track on wood, I decided to use a method mentioned on Robby C’s page which involves using screws to secure a stack of two different sized washers to the wood at intervals.

A small washer below a larger one provides the elevation that the track needs to slide onto the larger washer. The exact washers and screws I used can be seen in the gallery below, be sure to drill pilot holes for the screws and try to keep them straight, the straighter the screws are, the better they will fit in the washer and, in turn, the smaller the bump in the track will be. This method (at least with the sizes I used) does leave ever so slight bumps in the track once it’s fitted, but I felt it was slight enough that it wouldn’t adversely affect the races. I used one of these washer guides for each section of track positioned to sit in the center of the track, except for the finish line track where I placed one at either end.

The track has 4 main pieces (though one I cut into two to make it more modular). These four pieces are a small section of track for the finish line which has the results display attached, the main straightaway, the start incline and a support for the start incline. Following some of the ideas on Robby C’s page again I decided to use hinges to connect the start incline track to its support piece as well as to the straightaway allowing for an adjustable incline. If you use hinges with removable pins it allows for easier disassembly.

I ended up cutting the straightaway into two sections, and I’ll probably do the same with the incline so that I can make the entire track shorter and thus more palatable in the living room.

For the joint between the incline and the straightaway I made sure that no track joints would run across the curve allowing for the longest pieces of track at this joint and thus a smooth transition for cars from the incline to the straightaway.

The flat joints I cut where the tracks connect, this makes disassembly a bit easier, but I’m not sure it was the right choice as some of the wood warped and having a track joint and a wood joint at the same spot may not result in the smoothest run. Experiment with the hinges and see which orientation works best for your setup.

Start Gate

The start gate on this track uses a high torque servo to actuate a hinged plate that has 4 wood dowels inserted into it. The dowels feed up through routed slots in the wood and tracks to hold the cars at the start gate.

The Hot Wheels tracks can be cut easily with a sharp exacto, though it’s difficult to get the corners of cuts perfectly smooth. The track can also be drilled, one method is to drill an appropriate diameter hole at either end of the route you wish to cut and then connect the two holes with a single exacto cut on either edge.

To start the race the servo pulls a stiff wire which is connected to the hinge plate with a small L-bracket, this pulls the dowels back down through the routed slots and releases the cars. The slots I routed in the wood were 1/2″, the track slots were a little smaller than that, somewhere between 1/4″ and 1/2″ as the dowels themselves were 1/4″ diameter and needed some clearance to move smoothly. The dowels are simply friction fit into drilled holes.

It may not be obvious from the video and photos, but I drilled a small hole in the L bracket which fit the wire much better than the large screw holes. This prevented any extra travel of the wire at the hinge plate connection when the servo actuates.

The wire I used to connect the servo to the hinge plate is a malleable steel of some sort, when my son decides to push or pull on the start gate hinge or dowels it will bend this wire rather than stress the servo, after which I inform him that is not how it’s suppose to function and bend the wire straight again. For my setup a run-of-the-mill servo wasn’t strong enough to push and pull the 1/4″ wooden hinged plate so I picked up a high torque servo. If you made a lighter, thinner hinge plate you may be alright with a weaker servo.

Robby C mentions something about this type of drop-out start gate being not as fair as a gate that lifts rather than drops, but I’m not sure why that would be — in any case, my goal was not to create a track fit for the world cup of die-cast racing. A lift gate would have required more fabrication, so I opted for the simpler drop-out.

Finish Line

The finish line employs 4 photocells, also know as photoresistors or LDR’s (Light Dependent Resistors) which sit in an enclosure above the track in holes drilled into a piece 1/2″ piece of wood. These photocells point down through the drilled holes, through larger drilled holes in the enclosure at 4 infrared LED’s embedded in the wood under the track and aligned with holes in the track above. Placing the photocells in holes in the 1/2″ piece of wood keeps them focused on their respective infrared LED underneath without picking up a lot of ambient light.

While the infrared LED’s don’t produce any light visible to the naked eye, they are detected by the photocells and when a car passes over the infrared LED, the reading on the respective photocell drops dramatically and thus the Arudino brain can determine when each car passes the finish line by waiting for the photocell reading to drop.

Embedding the photocells into a piece of wood also allowed me to align that wood with holes I drilled in the track wood for the infrared LED’s before placing the wood, along with the photocells in the enclosure. Once I’d aligned the wood, I drilled holes in it for the photocells, wired up the photocells and placed that entire piece of wood into the enclosure. The holes I drilled in the enclosure were much larger and thus I had room to align the wood with the photocells to the infrared LED’s without having to worry about aligning exactly with the holes in the enclosure.

I wrote a specific Arduino sketch in order to align the photocells which wrote the photocell values to the computer via a serial connection. Since then I have added a debug mode to the race track, pressing both the start race and track reset buttons simultaneously will engage the debug mode where the Arduino will write the photocell values out to the 7-segment race result displays so that I can check the photocell alignments.

One of the issues I ran into was that it seemed the power needed to actuate the start gate servo would draw too much from the rest of the circuit causing the infrared LED’s to dim and trigger the photocells. To get around this I added a 150ms delay to allow the circuit to recover after opening the start gate.

Circuit

None of the electronics used in the project are very complex. I collected various components mostly from RobotShop, SparkFun, Creatron and locally.

These components and their circuits are all connected to the Arduino which runs the sketch at the bottom of this post. If I get a chance to draw one up, I’ll post a circuit diagram.

Car Storage

After purchasing a good starting set of cars to go along with the drag track I needed some way to store the hoard. It seemed likely that a generic storage container would do the trick as long as the compartment sizing matched. I wasn’t all that pleased with the purpose-build die-cast storage containers. A little bit of searching revealed this Creative Options Thread Organizer which fits 48 cars almost perfectly. It’s only half-full in the photo below, with the same number of compartments on the reverse side still to be populated.

Next Steps

While it turned into a race to finish this project in time for my son’s birthday, I had initially intended to integrate a Raspberry Pi to record race statistics and another fun function, but I’ll leave that to everyone’s imagination until I get around to implementing it. The whole setup could also use some Hot Wheels stickers.

Update

I’ve since removed a section from the start incline and the straightaway to reduce the overall size of the track in order to move it to a more permanent location (rather than across our living room). Below is a short video of my son operating the shortened track, he doesn’t seem to mind the change and still enjoys the track quite a bit.

Arduino Sketch


/* Hot Wheels Drag Strip v1.2 */

#include <Servo.h>
#include <SPI.h> // Include the Arduino SPI library

// SPI SS pins
const int displayPinLane1 = 7;
const int displayPinLane2 = 4;
const int displayPinLane3 = 6;
const int displayPinLane4 = 5; 

// Photocell pins
const int photocellPinLane1 = 2;
const int photocellPinLane2 = 3;
const int photocellPinLane3 = 4;
const int photocellPinLane4 = 5;

int photocellReadingLane1;
int photocellReadingLane2;
int photocellReadingLane3;
int photocellReadingLane4;

const int photocellThreshold = 4;

// Button pins
const int startButtonPin = 2;
const int resetButtonPin = 3;

const int gateServoPin = 14;

Servo gateServo;

// Servo positions
const int openGateServoPosition = 10;
const int closeGateServoPosition = 30;

int startButtonState = 0;
int resetButtonState = 0;

int raceStatus = 0;

int lane1Status = 0;
int lane2Status = 0;
int lane3Status = 0;
int lane4Status = 0;

int currentPlace = 0;

unsigned int counterLane1 = 0;  // These variables will count up to 65k
unsigned int counterLane2 = 0;
unsigned int counterLane3 = 0;
unsigned int counterLane4 = 0;  

String displayString;
char tempString[10];  // Will be used with sprintf to create strings

int animationTimer = 0;
bool showPlace = true;

void setup()
{
  gateServo.attach(gateServoPin);
  gateServo.write(closeGateServoPosition);

  pinMode(startButtonPin, INPUT);
  pinMode(resetButtonPin, INPUT);

  // -------- SPI initialization
  pinMode(displayPinLane1, OUTPUT);
  digitalWrite(displayPinLane1, HIGH);
  pinMode(displayPinLane2, OUTPUT);
  digitalWrite(displayPinLane2, HIGH);
  pinMode(displayPinLane3, OUTPUT);
  digitalWrite(displayPinLane3, HIGH);
  pinMode(displayPinLane4, OUTPUT);
  digitalWrite(displayPinLane4, HIGH); 

  // Begin SPI hardware
  SPI.begin();
  // Slow down SPI clock
  SPI.setClockDivider(SPI_CLOCK_DIV64); 

  clearDisplays();

  s7sSendStringSPI(displayPinLane1, "Ln 1");
  s7sSendStringSPI(displayPinLane2, "Ln 2");
  s7sSendStringSPI(displayPinLane3, "Ln 3");
  s7sSendStringSPI(displayPinLane4, "Ln 4");

  // High brightness
  setBrightnessSPI(displayPinLane1, 255);
  setBrightnessSPI(displayPinLane2, 255);
  setBrightnessSPI(displayPinLane3, 255);
  setBrightnessSPI(displayPinLane4, 255);
}

void loop()
{
  animationTimer++;

  if(animationTimer > 200) {
    if(showPlace == true) {
      showPlace = false;
    } else {
      showPlace = true;
    }
    animationTimer = 0;
  }

  photocellReadingLane1 = analogRead(photocellPinLane1);
  photocellReadingLane2 = analogRead(photocellPinLane2);
  photocellReadingLane3 = analogRead(photocellPinLane3);
  photocellReadingLane4 = analogRead(photocellPinLane4);  

  resetButtonState = digitalRead(resetButtonPin);
  startButtonState = digitalRead(startButtonPin);

  if(resetButtonState == HIGH && startButtonState == HIGH) {
    // Debug mode
    clearDisplays();

    s7sSendStringSPI(displayPinLane1, String(photocellReadingLane1)+"   ");
    s7sSendStringSPI(displayPinLane2, String(photocellReadingLane2)+"   ");
    s7sSendStringSPI(displayPinLane3, String(photocellReadingLane3)+"   ");
    s7sSendStringSPI(displayPinLane4, String(photocellReadingLane4)+"   ");

    delay(1000);
  } else if(resetButtonState == HIGH && raceStatus > 0) {
    raceStatus = 0;

    clearDisplays();

    s7sSendStringSPI(displayPinLane1, "Ln 1");
    s7sSendStringSPI(displayPinLane2, "Ln 2");
    s7sSendStringSPI(displayPinLane3, "Ln 3");
    s7sSendStringSPI(displayPinLane4, "Ln 4");

    lane1Status = 0;
    lane2Status = 0;
    lane3Status = 0;
    lane4Status = 0;

    counterLane1 = 0;
    counterLane2 = 0;
    counterLane3 = 0;
    counterLane4 = 0;

    currentPlace = 0;

    gateServo.write(closeGateServoPosition);
  } else {
    if (raceStatus == 0 && startButtonState == HIGH) {

      // start the race

      gateServo.write(openGateServoPosition);

      // wait for servo to drop
      delay(150);
      raceStatus = 1;
    } else if (raceStatus == 1) {

      // race is running

      int carsAcross = 0;

      if (photocellReadingLane1 < photocellThreshold && lane1Status == 0) {
        showPlace = true;
        currentPlace++;
        carsAcross++;
        lane1Status = currentPlace;
        gateServo.write(closeGateServoPosition);
      } 

      if (photocellReadingLane2 < photocellThreshold && lane2Status == 0) {
        showPlace = true;
        if(carsAcross == 0) {
          currentPlace++;
        }
        carsAcross++;
        lane2Status = currentPlace;
        gateServo.write(closeGateServoPosition);
      } 

      if (photocellReadingLane3 < photocellThreshold && lane3Status == 0) {
        showPlace = true;
        if(carsAcross == 0) {
          currentPlace++;
        }
        carsAcross++;
        lane3Status = currentPlace;
        gateServo.write(closeGateServoPosition);
      } 

      if (photocellReadingLane4 < photocellThreshold && lane4Status == 0) {
        showPlace = true;
        if(carsAcross == 0) {
          currentPlace++;
        }
        carsAcross++;
        lane4Status = currentPlace;
        gateServo.write(closeGateServoPosition);
      } 

      updateDisplay(displayPinLane1, lane1Status, counterLane1);
      updateDisplay(displayPinLane2, lane2Status, counterLane2);
      updateDisplay(displayPinLane3, lane3Status, counterLane3);
      updateDisplay(displayPinLane4, lane4Status, counterLane4);

      if (lane1Status == 0) {
        counterLane1++;
      }

      if (lane2Status == 0) {
        counterLane2++;
      }

      if (lane3Status == 0) {
        counterLane3++;
      }

      if (lane4Status == 0) {
        counterLane4++;
      }
    }
  }

  delay(10);  // This will make the display update at 100Hz.*/
}

void clearDisplays()
{
  clearDisplaySPI(displayPinLane1);
  clearDisplaySPI(displayPinLane2);
  clearDisplaySPI(displayPinLane3);
  clearDisplaySPI(displayPinLane4);
}

void updateDisplay(int displayPin, int laneStatus, unsigned int laneCounter) {

    // Magical sprintf creates a string for us to send to the s7s.
    //  The %4d option creates a 4-digit integer.
    sprintf(tempString, "%4d", laneCounter);

    // This will output the tempString to the S7S
    if(laneStatus == 0 || showPlace == false) {
      s7sSendStringSPI(displayPin, tempString);

      // Print the decimal at the proper spot
      if (laneCounter < 10000) {
          setDecimalsSPI(displayPin, 0b00000010);  // Sets digit 3 decimal on
      } else {
          setDecimalsSPI(displayPin, 0b00000100);
      }
    } else {
      setDecimalsSPI(displayPin, 0b000000000);

      if(laneStatus == 1) {
        s7sSendStringSPI(displayPin, "1st ");
      } else if(laneStatus == 2) {
        s7sSendStringSPI(displayPin, "2nd ");
      } else if(laneStatus == 3) {
        s7sSendStringSPI(displayPin, "3rd ");
      } else {
        s7sSendStringSPI(displayPin, "4th ");
      }
    }
}

// This custom function works somewhat like a serial.print.
//  You can send it an array of chars (string) and it'll print
//  the first 4 characters in the array.
void s7sSendStringSPI(int ssPin, String toSend)
{
  digitalWrite(ssPin, LOW);
  for (int i=0; i < 4; i++)
  {
    SPI.transfer(toSend[i]);
  }
  digitalWrite(ssPin, HIGH);
}

// Send the clear display command (0x76)
//  This will clear the display and reset the cursor
void clearDisplaySPI(int ssPin)
{
  digitalWrite(ssPin, LOW);
  SPI.transfer(0x76);  // Clear display command
  digitalWrite(ssPin, HIGH);
}

// Set the displays brightness. Should receive byte with the value
//  to set the brightness to
//  dimmest------------->brightest
//     0--------127--------255
void setBrightnessSPI(int ssPin, byte value)
{
  digitalWrite(ssPin, LOW);
  SPI.transfer(0x7A);  // Set brightness command byte
  SPI.transfer(value);  // brightness data byte
  digitalWrite(ssPin, HIGH);
}

// Turn on any, none, or all of the decimals.
//  The six lowest bits in the decimals parameter sets a decimal
//  (or colon, or apostrophe) on or off. A 1 indicates on, 0 off.
//  [MSB] (X)(X)(Apos)(Colon)(Digit 4)(Digit 3)(Digit2)(Digit1)
void setDecimalsSPI(int ssPin, byte decimals)
{
  digitalWrite(ssPin, LOW);
  SPI.transfer(0x77);
  SPI.transfer(decimals);
  digitalWrite(ssPin, HIGH);
}

Maker Wedding: Animated HTML / jQuery wedding site


One of the projects I took on for our wedding was our official wedding website, I registered two domains, esterandphil.com as well as philandester.com thus avoiding any confusion. Both site pointed to the same page, for the general public a simple splash with a dynamic forest animation accomplished with the help of jQuery.

We wanted to create a page that connected with our venue which was in a barn on a conservation area. I’ve always enjoyed sine path algorithms so I created a scene where silhouetted trees scroll slowly by in a parallax-esc fashion and fuzzy Will-o’-the-wisp dots float lazily upward on sine waves. Randomly the wisps will be replaced with rain and visiting the site during the day versus during the night will result in different colours and songs.

The trees are selected randomly from one of six or seven images which then have a random amount of transparency and speed applied so that some seem further in the distance. Once they disappear off of one side they are recycled, animating in from the starting side again. Similarly the wisps have random factors applied to their size, speed, sine curve and alpha. The wisps are also interspersed on the z-axis to move in front of some trees while behind others adding to the illusion of depth. The wisps are also recycled once they’ve floated out of view.

On the live site I adjust the parameters for some popular clients, such as iOS for which I disabled animation and reduced the number of trees and wisps.

Inaccessible to the public is a guest site which used the splash page concept as a background with only the a couple of wisps animating with static trees (still randomly generated).

While this exact example is probably not portable verbatim you may find the method libraries I used as well as the technique useful. I have outlined some of the initializer functions below and linked to the libraries, some of which I have modified. The final library, animate.js, is almost completely custom code for this site and thus would have to be altered to suit your needs.

Will-o’-the-wisp Initialize

window.numberOfDots = 50;

for(i=0;i<window.numberOfDots;i++)
  window.dots.push(new dot(i));

Forest Initialize

window.numberOfTrees = 15;

for(i=0;i<window.numberOfTrees;i++)
  window.trees.push(new tree(i,'http://philandester.com/'));

Rain Initialize

var raincolor = '#fff';

new Rain('canvas', {
    speed: 500,
    angle: 20,
    intensity: 5,
    size: 10,
    color: raincolor
  });

Main Loop Start

setInterval(function(){sine()}, 25);

Raphael 1.5.2 – JavaScript Vector Library, MIT License (http://raphaeljs.com/)
http://philandester.com/rain.js

Andrew J. Peterson, NDP Software, MIT License (http://blog.ndpsoftware.com/)
http://philandester.com/colorfactory.js

Custom JavaScript Animation Library
http://philandester.com/animate.js

A Maker Wedding

Initially I wasn’t sure how much our wedding was truly going to represent my fiancée and I, after all, we wanted our family and friends to enjoy themselves and feel included — as with any large event there are a lot of expectations to manage. After deciding to craft my own Edison-style light fixtures for our reception I realized that the occasion was, in addition to a celebration of our life-long commitment to each other, an opportunity for us to showcase our creativity and perhaps introduce some of our family and friends to aspects of ourselves they may not have known existed.

In retrospect we probably took on too much, but it allowed us to feel the occasion was a true reflection of ourselves — for me this meant soldering, stripping, crimping, twisting, programming and no small amount of brow furrowing. None of these projects could’ve come together without the help of my wonderful wife Ester, who not only said yes, but also collaborated throughout and trusted me to deliver on some very important aspects of our big day. In addition, a big thanks to my dear old Dad who took time to help me with the lengthy task of wiring the Edison fixtures and to the friends and family who helped us setup and teardown these, and other installations.

Animated Arduino LED matrix lounge table top

Vinyl “flexi” record wedding invitations

XBee remote relay as photobooth RF camera trigger

Bachelor party wireless Arduino accelerometer Stab-O-Meter

JQuery Animated Wedding Website


Various puppet arms available at Obscura Antiques & Oddities, New York

Maker Wedding: Bachelor party wireless accelerometer Stab-O-Meter

Since I had disassembled the Wine-O-Meter I’d made for a friend’s bachelor party I needed to come up with something else for my own, I wanted to do an updated strongman competition. I decided to put together a wireless accelerometer to hopefully measure the speed and impact of various activities such as swinging a baseball bat, a sledgehammer, a hatchet, a tennis racket — get the idea? Sort of like the measurement tools used on shows like MythBusters or Deadliest Warrior. Along the lines of the Wine-O-Meter I dubbed the project the Stab-O-Meter as measuring arm movements reminded me of one of my favourite Futurama characters, Roberto.


My plan was to use an Arduino to read an accelerometer and use a pair of XBees to wireless relay the information to a laptop. The laptop would be running a Processing sketch to handle the high score display, reset and current readings. It took a little bit to find the right Arudino code to read the LIS331 Triple Axis Accelerometer I’d selected but it worked well once I found it. I don’t know a whole heck of a lot about accelerometers, but this one measures g-forces on three axis, x, y and z. After some trial and error I decided to add all positive g-force readings together and then add all negative g-force readings together. If the positive total was higher I used that as the current amalgamated reading otherwise I used the absolute sum of the negative values. Comment if you’re aware of a better way to translate x, y, z g-forces into a single number representing the speed of the motion (see Hank’s comment below).

Hank Cowdog

A neg X Acc means acc along the negative X axis. The magnitude of the acc is the important measurement, so a better approach would be to sum the squares of each X,Y,Z component and then take the square root (as per the Pythagorean Theorem). This computes the magnitude of the Acc regardless of the direction (or orientation of the accelerometer chips).

result = sqrt(xAcc*xAcc + yAcc*yAcc + zAcc*zAcc);


The Arduino sent the single number amalgamated reading in realtime (or as close as possible) via it’s serial connection to a XBee which in turn wirelessly relayed the serial data to a laptop running a processing sketch to read and deal with the data. The Processing sketch displayed a realtime reading bar on the right, the highest reading yet recorded in large numbers in the center and a RESET button to clear the current highest reading. With this system each contestant could reset the high score using the RESET button or the spacebar and the proceed to swing a bat or stab a tree or whatnot to find they’re personal best, which was then ranked against other’s scores on a white board.

This part worked great, however in impact scenarios (actually hitting something) it was too easy to max out the sensor, which has a max of 24g, so we restricted our games to non-impact swings. I had added hand wrap to the sensor case in order to secure it to the implement of choice, however I quickly realized that it also needed a non-slip surface for grip, I epoxied some rubber salvaged from a guitar effect pedal. Even with the hand wrap and the rubber footing the first full-force swing with a baseball bat sent the sensor soaring into a neighbouring house — duct tape provided the necessary upgrade in grip, but downgrade in polish.

The video below is, aside from my Roberto impression, an early test using a preliminary Processing sketch and no cases for the components. When I get a chance I’ll record a video of the finished setup, perhaps as I demolish my garage this weekend. Yes, it’s an odd video, but that’s what YouTube is for, right?

Parts

Arduino Sketch

// 3-axis Accelerometer
// Sparkfun Electronics Triple Axis Accelerometer Breakout - LIS331
// Arduino UNO

/* Wiring:
    UNO LIS331

    3.3V VCC
    GND GND
    10 CS
    11 SDA/SDI
    12 SA0/SDO
    13 SCL/SPC
    */

#include <SPI.h>
#include <stdlib.h>
#include <stdio.h>

#define SS 10 // Serial Select -> CS on LIS331
#define MOSI 11 // MasterOutSlaveIn -> SDI
#define MISO 12 // MasterInSlaveOut -> SDO
#define SCK 13 // Serial Clock -> SPC on LIS331

#define SCALE 0.0007324; // approximate scale factor for full range (+/-24g)
// scale factor: +/-24g = 48G range. 2^16 bits. 48/65536 = 0.0007324

// global acceleration values
double xAcc, yAcc, zAcc;

void setup()
{
  Serial.begin(9600);

  // Configure SPI
  SPI_SETUP();

  // Configure accelerometer
  Accelerometer_Setup();
}

void loop()
{
  readVal(); // get acc values and put into global variables

  int pos = 0;
  int neg = 0;

  if(xAcc > 0)
  {
    pos = pos + xAcc;
  }
  else
  {
    neg = neg + abs(xAcc);
  }

  if(yAcc > 0)
  {
    pos = pos + yAcc;
  }
  else
  {
    neg = neg + abs(yAcc);
  }

  if(zAcc > 0)
  {
    pos = pos + zAcc;
  }
  else
  {
    neg = neg + abs(zAcc);
  }

  int result = neg;

  if(pos > neg)
    result = pos;

  Serial.println(result,1);

   /*
    Serial.print(xAcc, 1);
    Serial.print(",");
    Serial.print(yAcc, 1);
    Serial.print(",");
    Serial.println(zAcc, 1);
  */

  delay(10);
}

// Read the accelerometer data and put values into global variables
void readVal()
{
  byte xAddressByteL = 0x28; // Low Byte of X value (the first data register)
  byte readBit = B10000000; // bit 0 (MSB) HIGH means read register
  byte incrementBit = B01000000; // bit 1 HIGH means keep incrementing registers
  // this allows us to keep reading the data registers by pushing an empty byte
  byte dataByte = xAddressByteL | readBit | incrementBit;
  byte b0 = 0x0; // an empty byte, to increment to subsequent registers

  digitalWrite(SS, LOW); // SS must be LOW to communicate
  delay(1);
  SPI.transfer(dataByte); // request a read, starting at X low byte
  byte xL = SPI.transfer(b0); // get the low byte of X data
  byte xH = SPI.transfer(b0); // get the high byte of X data
  byte yL = SPI.transfer(b0); // get the low byte of Y data
  byte yH = SPI.transfer(b0); // get the high byte of Y data
  byte zL = SPI.transfer(b0); // get the low byte of Z data
  byte zH = SPI.transfer(b0); // get the high byte of Z data
  delay(1);
  digitalWrite(SS, HIGH);

  // shift the high byte left 8 bits and merge the high and low
  int xVal = (xL | (xH <<8));
  int yVal = (yL | (yH <<8));
  int zVal = (zL | (zH <<8));

  // scale the values into G's
  xAcc = xVal * SCALE;
  yAcc = yVal * SCALE;
  zAcc = zVal * SCALE;
}

void SPI_SETUP()
{
  pinMode(SS, OUTPUT);

  // wake up the SPI bus
  SPI.begin();

  // This device reads MSB first:
  SPI.setBitOrder(MSBFIRST);

  /*
  SPI.setDataMode()
  Mode    Clock Polarity (CPOL) Clock Phase (CPHA)
  SPI_MODE0    0    0
  SPI_MODE1    0    1
  SPI_MODE2    1    0
  SPI_MODE3    1    1
  */
  SPI.setDataMode(SPI_MODE0);

  /*
  SPI.setClockDivider()
  sets SPI clock to a fraction of the system clock
  Arduino UNO system clock = 16 MHz
  Mode SPI Clock
  SPI_CLOCK_DIV2 8 MHz
  SPI_CLOCK_DIV4 4 MHz
  SPI_CLOCK_DIV8 2 MHz
  SPI_CLOCK_DIV16 1 MHz
  SPI_CLOCK_DIV32 500 Hz
  SPI_CLOCK_DIV64 250 Hz
  SPI_CLOCK_DIV128 125 Hz
  */

  SPI.setClockDivider(SPI_CLOCK_DIV16); // SPI clock 1000Hz
}

void Accelerometer_Setup()
{
  // Set up the accelerometer
  // write to Control register 1: address 20h
  byte addressByte = 0x20;
  /* Bits:
  PM2 PM1 PM0 DR1 DR0 Zen Yen Xen
  PM2PM1PM0: Power mode (001 = Normal Mode)
  DR1DR0: Data rate (00=50Hz, 01=100Hz, 10=400Hz, 11=1000Hz)
  Zen, Yen, Xen: Z enable, Y enable, X enable
  */
  byte ctrlRegByte = 0x37; // 00111111 : normal mode, 1000Hz, xyz enabled

  // Send the data for Control Register 1
  digitalWrite(SS, LOW);
  delay(1);
  SPI.transfer(addressByte);
  SPI.transfer(ctrlRegByte);
  delay(1);
  digitalWrite(SS, HIGH);

  delay(100);

  // write to Control Register 2: address 21h
  addressByte = 0x21;
  // This register configures high pass filter
  ctrlRegByte = 0x00; // High pass filter off

  // Send the data for Control Register 2
  digitalWrite(SS, LOW);
  delay(1);
  SPI.transfer(addressByte);
  SPI.transfer(ctrlRegByte);
  delay(1);
  digitalWrite(SS, HIGH);

  delay(100);

  // Control Register 3 configures Interrupts
  // Since I'm not using Interrupts, I'll leave it alone

  // write to Control Register 4: address 23h
  addressByte = 0x23;
  /* Bits:
  BDU BLE FS1 FS0 STsign 0 ST SIM
  BDU: Block data update (0=continuous update)
  BLE: Big/little endian data (0=accel data LSB at LOW address)
  FS1FS0: Full-scale selection (00 = +/-6G, 01 = +/-12G, 11 = +/-24G)
  STsign: selft-test sign (default 0=plus)
  ST: self-test enable (default 0=disabled)
  SIM: SPI mode selection(default 0=4 wire interface, 1=3 wire interface)
  */
  ctrlRegByte = 0x30; // 00110000 : 24G (full scale)

  // Send the data for Control Register 4
  digitalWrite(SS, LOW);
  delay(1);
  SPI.transfer(addressByte);
  SPI.transfer(ctrlRegByte);
  delay(1);
  digitalWrite(SS, HIGH);
}

Processing Sketch

 import pitaru.sonia_v2_9.*;
 import processing.serial.*;

 Sample beep;

 float high;
 int count;

 int inside = -1;
 int bx=850; // position in X of the up corner of the botton
 int by=460; // position in Y of the up corner of the botton
 int h=40;
 int w=100;

 float inByte=0;
 float drawByte=0;

 PFont f;

 Serial myPort;         // The serial port
 int xPos = 10;         // horizontal position of the graph

public void stop()
{
  Sonia.stop();
  super.stop();
}

 void setup () {
   // set the window size:
   size(1024, 550);

   high = 0;
   count = 0;

   f = createFont("Verdana",6,true);

   // List all the available serial ports
   println(Serial.list());
   // I know that the first port in the serial list on my mac
   // is always my  Arduino, so I open Serial.list()[0].
   // Open whatever port is the one you're using.
   myPort = new Serial(this, Serial.list()[0], 9600);
   // don't generate a serialEvent() unless you get a newline character:
   myPort.bufferUntil('\n');
   // set inital background:
   background(0);

  Sonia.start(this);
  beep = new Sample( "beep-02.wav" );
 }

void draw()
{
  if(keyPressed)
  {
    if(key == ' ')
    {
      high = inByte;
    }
  }

  background(0);

  //stroke(255,0,0);
  rect(xPos, 500 - inByte, xPos+20, inByte);

  textFont(f,25);
  fill(255);
  text(inByte, xPos - 10, 500 - inByte - 25);

  count = count + 1;

  if(count > 2)
  {
    count = 0;

    if(drawByte < high - 200)
    {
     beep.play();
     drawByte = drawByte + 100;
    }
    else if(drawByte < high - 10)
    {
       beep.play();
       drawByte = drawByte + 10;
    }
    else if(drawByte < high - 1)
    {
      beep.play();
      drawByte = drawByte + 1;
    }
    else if(drawByte < high - .1)
    {
      beep.play();
      drawByte = drawByte + .1;
    }
    else if(drawByte < high - .01)
    {
      beep.play();
      drawByte = drawByte + .01;
    }
    else if(drawByte < high - .001)
    {
      beep.play();
      drawByte = drawByte + .001;
    }
    else if(drawByte < high)
    {
      drawByte = high;
    }
  }

  if(drawByte > high)
  {
    drawByte = high;
  }

  textFont(f,140);
  fill(255);
  text(drawByte, 200, 325);

  rect(bx,by,w,h); // Button 

  textFont(f,25);
  fill(0);
  text("RESET", bx+10, by+30);
  fill(255);
}

void mousePressed(){
  if(!(((mouseX > (bx+w))
  ||(mouseY > (by+h)))
  ||((mouseX < bx)
  ||(mouseY < by))))
  {
      high = inByte;
  }
}

void serialEvent (Serial myPort) {
   String inString = myPort.readStringUntil('\n');

   if (inString != null)
   {
     // trim off any whitespace:
     inString = trim(inString);
     // convert to an int and map to the screen height:
     inByte = float(inString);
     inByte = map(inByte, 0, 1023, 0, 500);

     if(inByte > high)
     {
       high = inByte;
     }
   }
 }

“Spaghetti” IP Cam / Arduino Motion Detect Sprinkler

arduino motion detect sprinkler


After a neighbourhood dog decided my front lawn was a fantastic place to poop on and his owners decided that they don’t care for bylaws I set upon finding a solution. Sure you could try cayenne pepper, mothballs, ammonia or even marking your own territory (take that!) but I already had an IP camera monitoring my front yard for security purposes, so I figured I’d just hook up an Arduino and a sprinkler valve. This yard defense solution has two added benefits, it keeps my lawn healthy (I can set up timed watering through this system) and it sends offending dogs home stinkin’ wet. From me having to shovel dookie off my lawn to negligent owners having to deal with wet dog — perfect.

When the IP camera detects motion in configured regions of its video stream (the rectangles in the screenshot) it triggers one of its General Purpose Input/Output ports (GPIO). The Arduino is listening for this GPIO signal and once its received the Arduino triggers a relay which connects a 24v power supply to the sprinkler solenoid valve. The valve opens when 24v is applied to it and “sprinkles” whatever was responsible for the motion.

The TRENDnet IP camera I employed works great as it already records video of events to network or attached storage, sends email alerts with snapshots and allows manual triggering of its GPIO.

The TRENDnet monitoring plugin only works in IE but after some reverse engineering and C# coding I had an easy to use web interface for all browsers and mobile devices.

If there’s enough interest I’ll post circuit and wiring diagrams. Ensure the valves you get are non-latching 24v solenoids, some 9v valves seem tempting but are magnetic and require a more complex circuit.

Why Spaghetti? Watch more ATHF.

Parts

 
Update: Some folks have asked why not just trigger the valve relay directly from the GPIO on the camera. This could’ve been done, but then I wouldn’t have gotten as much control as I wanted. This way I can configure timing to prevent the sprinkler itself from setting off the motion detector, set up timed watering as well as trigger other devices at various timings (DSLR for reaction shots). Another addition may be an XBee based remote control or hardwired buttons for various functions.

Update: I’ve added a quick circuit diagram and simple Arduino code.

Arduino Code


int gpioPin = 1;       			// GPIO input
int val = 0;           			// value read from GPIO input
int trigPin = 13;      			// solenoid relay output

void setup()
{
	pinMode(trigPin, OUTPUT);
}

void loop()
{
  val = analogRead(gpioPin);		// read the GPIO input pin
  if (val > 500)
  {
	// GPIO triggered, open the valve
	digitalWrite(trigPin, HIGH);
  }
  else
  {
	// GPIO off, close the valve
	digitalWrite(trigPin, LOW);
  }
}