Sniff is a "Scratch-like" programming language that's designed to help Scratchers move gently from Scratch to more conventional languages. They can start writing programs, without having to learn a new language because Sniff is based on Scratch. They learn a little more about variables, compiling, syntax errors (!), and they can have fun controlling real hardware while they're doing it.

Friday, 18 April 2014

Lighting McQueen Vs the Batmobile

I've been wanting to build some kind of speed trap experiment for ages, and today I finally built it, and it worked out great - total component costs less than $1 to answer the question that every five year old boy, and everyone who's ever been a five year old boy wants answered: Could Lightning McQueen beat the Batmobile in a straight race?

To answer this, we need to build a speed trap to measure their top speed. As speed is just distance divided by time, the easiest way to do that is to trigger something at one point on the track to start a timer, and then trigger further along the track to stop the timer. We can measure the distance, divide it by the measured time and we've got our answer!

There are bunch of different triggers we could use but by far the best is a light dependant resistor. These cost pennies, and I had a bag of 20 or so, so I melted a couple of holes at each end of a piece of hot wheels track and put an LDR in each.

The LDR has a low resistance when there's light shining on it, and a higher resistance when not. The ones I had measured around 10Kohms so I wired the LDR at the top of the track between 5V and Arduino A0, and a 10K resistor from A0 to 0V. I repeated this at the other end of the track with Arduino A1 to stop stop the measurement.

I also used an LCD Shield for display and a Sensor Shield to make hooking this up easier, but they're strictly optional.

make lcd device
make message string

make startTrigger analog input A0
make endTrigger analog input A1

make startTime number
make recordedTime number
make recordedSpeed number
make distance number

when start
.set distance to 0.3
..tell lcd to "clear"
..set message to "Hot Wheels Speed Trap"
..tell lcd to "show"
..set message to "ready..."
..tell lcd to "show"

First thing the code does is create an LCD device and two analog inputs. To calibrate the system we need to measure the distance between the sensors, and for a standard piece of hot wheels track it turns out that they end up exactly 30cm apart. Then we display a "ready..." message on the LCD.

..wait until startTrigger < 0.5
..set startTime to timer

Now we just wait for the start gate to be triggered. The value of 0.5 might need a bit of tweaking depending on your lighting conditions and components, but for me using a 10K resistor in a normally lit room, 0.5 worked pretty reliably. 

..wait until endTrigger < 0.5

We could use the same code to wait for the timer to end, but if you're timing over a longer piece of track you might like to use:

..repeat until endTrigger < 0.5
...tell lcd to "clear"
...set message to [timer - startTime ]
...tell lcd to "show"

to give a runing timer through the speed trap. However over a single track piece removing this improves accuracy, and you couldn't really see it any way.

..set recordedTime to timer - startTime
..set recordedSpeed to distance/recordedTime
..set message to "time       :"
..set message to join message [ recordedTime ]
..tell lcd to "show"
..set message to "speed (m/s):"
..set message to join message [ recordedSpeed ]
..tell lcd to "show"
..wait 5 secs

Now we just calculate the elapsed time, and speed. Of course this is in metres per second, so multiplying by 3.6 gives kilometres per hour.

..set recordedSpeed to recordedSpeed*3600/1000
..set message to "speed (kph):"
..set message to join message [ recordedSpeed ]
..tell lcd to "show"
..wait 5 secs

So what to the results tell us? Well both cars clocked in at 0.10 seconds for the 30cm distance, giving a speed of 3m/s or around 11km/h when run down a ramp about 1m high.

That's a potential energy (mgh) of 10m (where m is the mass of the car, h is the height and g is gravity which I'm calling 10 cause its close enough). That gets turned into  kinetic energy(1/2 mv^2or about 5m (1/2m*3*3 - I'm rounding up again!). That's actually pretty good as there's a lot of friction to be dealt with and we're turning the car through 90 degrees as it wants to drop down, but we push it forwards, so 50% efficient isn't at all bad. The best we could hope for on this track is about 4.5m/s or 16km/h.

There's lots more you can do with this - different cars, different heights, different shapes of tracks.

My money's on this guy - on the ground he only does 0.1kph but on the way to Mars he clocked in at around 21,000kph!

No comments:

Post a Comment