Working on algebra over the summer, we tried a little programming. This is O’s first program, which gives the chirp rate of crickets based on the temperature.
t = float(raw_input('enter temperature> '))
n=4*t-160
print "number of chirps=", n
print "t=", t
OpenSCAD bills itself as “The Programmers Solid 3D CAD Modeller”. It does this job pretty well, which is probably why I like it so much. Like POV-RAY, which I’ve used before, you create primitive objects–spheres, boxes, cylinders, etc.–and add or subtract them from one another to create the three-dimensional shapes you want.
Unlike the more graphical 3d modeling programs, like SketchUp (which I’ve played with in the past), in OpenSCAD you have to specify in the script the exact dimensions of your primitives, and how to rotate them and translate them to get them where you want them to be in space. This makes it a great language to use in geometry class, or anywhere else you want students to learn about co-ordinate systems.
The script to create the box with a circle cut out of it (see figure above) is:
Vpython requires students of make similar geometric movements of their objects and renders them nicely in 3d, but given the incentive that they can print up a tangible result of their work, I’d be willing to bet that students, especially younger ones, would be quite motivated to work with OpenSCAD. Vpython does retain the advantage that it is able to do animations, while you can only print static objects. In addition, OpenSCAD is more of a scripting language than a programming language like Python (see some of my Vpython programs here).
The OpenSCAD documentation is quite good. I also found it easy to find instructions on how to create a 3d object from a black and white image, simply by extruding it in the third dimension (by iamwil on the Cubehero Blog and by I Heart Robotics).
One note: I’m using OSX 10.6.8 at the moment, and the current version of OpenSCAD does not work on it. Since I’m loathe to upgrade, I had to use the prior release of OpenSCAD-2013.06.dmg.
Vicki Davis has a nice compilation of resources for teaching coding to kids of all ages. Of the fifteen things she lists, the ones I’ve used, like Scratch and the Raspberry Pi have been great.
Finally, relatively easy interactive 3d on the web. You can rotate and zoom into the scene. (Although it may not yet be compatible with all browsers it does work with Firefox at least).
Your browser does not support iframes.
This method uses the three.js Javascript library. Here I use it to show the volume of a rotated surface using the disk method. It’s almost identical to my calculus student’s project, except here I’m finding the volume between x=1 and x=3, using disks that are 0.5 units in height (Δx).
Since the volume of cylinder is:
where r is the radius of the cylinder.
We’re finding the volume created by a function that’s rotated around the x-axis. Using the function:
The radius of each cylinder is the value of the function for that x value, so you could write the radius as:
Therefore the volume of each disk is:
There are four disks and we use the function value at the far end of the disk to draw the disk so the total volume is:
Factoring out the π and the Δx gives:
Since Δx = 0.5, a = 1.0, and b = 3.0, we can define the number of disks as n = 4 then we can rewrite using summation formula:
reverting back to a and b gives the general equation:
On the recommendation of Mr. Schmidt, two of my students have been quite fascinated over the last few days trying to solve problems on Project Euler. They’ve been working on them together to, I suspect, the detriment of some of their other classes, but as their math teacher I find it hard to object.
An example problem is something like this:
If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.
Find the sum of all the multiples of 3 or 5 below 1000.
They’ve been solving them numerically using Python. It’s been quite fascinating to see.
Although it took us a day to figure out how to get the Raspberry Pi to work–a faulty SD card turned out to be a major delay–we still had most of a week of the Creativity Interim for students to get some projects done.
That is, until it started to snow. We lost more two days.
Still, we had a small cadre of determined students, one of whom (N.D.) decided that she wanted to make the Pi play sounds to go with the blinking LED lights.
To do this she needed to:
Learn how to use the LINIX command line to connect to the Pi and execute programs.
Learn how to write programs in Python to operate the Pi’s GPIO (input/output) pins.
Learn how to make circuits on a breadboard connected to the Pi.
A Quick and Incomplete Introduction to the Command Line: Basic Navigation
We started by using the Terminal program on my Mac to learn basic navigation.
To see a list of what’s in a folder use ls:
> ls
For more details on the items in the folder, use the -l option:
> ls -l
To see all the options available for the ls command you can lookup the manual using man:
> man ls
To create a new directory (e.g. Pi) use mkdir:
> mkdir Pi
To change directories (say to go into the Pi directory) use cd:
> cd Pi
Connecting to the Pi
I detail how to connect to a Pi that’s plugged into the wall via the local network here, but to summarize:
Use ifconfig to find the local IP address (under eth1).
> ifconfig
Use nmap to identify where the Pi is on the network. E.g.:
> sudo nmap -sP 191.163.3.0/24
Finally, connect to the Pi using the secure shell program ssh:
> ssh pi@191.163.3.214
(the default password is “raspberry”)
Wiring an LED Light Circuit
We created an initial circuit going from the #17 General Purpose Input/Output (GPIO) pin to a red LED then through a resistor then back into one of the ground pins of the Pi. We’ll turn the current going through GPIO #17 on and off via our program on the Pi. The resistor is needed to reduce the amount of current flowing through the LED, otherwise it would likely get blown out. The circuit is shown in the picture where I’ve taken the wire ribbon out for the sake of visibility.
Wiring on a breadboard is quite simple if you remember a few rules.
First, the columns of holes on the sides are connected vertically, so the right end of the yellow wire (in the + column) is connected to the resistor.
Secondly, the holes in the middle are connected horizontally, but not across the gap in the middle. That’s why the GPIO pin #17 is connected to the lower end of the black wire, and the left end of the resistor is connected to the ground (GND) pin. The LED light reaches across the two gap to connect the two rows with the upper end of the black wire and the left end of the yellow wire. Without something to bridge the gap, the circuit would not be complete.
The resistor has a resistance of 420 Ohms. You can tell by the color bands. In the sequence–yellow, red, brown–the first two bars represent numbers (yellow=4; red=2) and the third number represents a multiplier (in this case brown represents 10x).
Light: Programming the Pi in Python
Once you ssh to the Pi you can start programming. We used the command line text editor Nano. A simple text editor is all you need to write simple Python programs. We’ll create a program called flash.py (Note: it’s important to include the .py at the end of the file’s name, otherwise it becomes hard to figure out what type of file something is, and the extension also clues Nano in to allow it to color-code the keywords used in Python, making it a lot easier to write code.
pi> nano flash.py
You can then type in your program. The basic code for making a single light flash on and off ten times (see here) is:
flash.py
#!/usr/bin/env python
from time import sleep
import RPi.GPIO as GPIO
cpr = 17 ## The GPIO Pin output number
GPIO.setmode(GPIO.BCM) ## Use board pin numbering
GPIO.setup(cpr, GPIO.OUT) ## Setup GPIO Pin 7 to OUT
for i in range(10):
GPIO.output(cpr, True)
sleep(0.5)
GPIO.output(cpr, False)
sleep(0.5)
GPIO.cleanup()
This requires wiring a circuit to the GPIO pin #17. The circuit has a LED and a resistor in series, and circles back to the Pi via a grounding pin.
Run the program flash.py using:
pi> sudo python flash.py
Finally, let’s tell the program how many times to flash the light. We could create a variable in the Python code with a number, but it’s more flexible to set Python to take the number from the command line as a command line argument. Any words you put after the python in the command to run your program are automatically stored in an array called sys.argv if you import the sys module into your program. So we rewrite our flash.py program as:
flash2.py
#!/usr/bin/env python
from time import sleep
import RPi.GPIO as GPIO
import sys
cpr = 17 ## The GPIO Pin output number
GPIO.setmode(GPIO.BCM) ## Use board pin numbering
GPIO.setup(cpr, GPIO.OUT) ## Setup GPIO Pin 7 to OUT
for i in range(int(sys.argv[1])):
GPIO.output(cpr, True)
sleep(0.5)
GPIO.output(cpr, False)
sleep(0.5)
GPIO.cleanup()
So to flash the light 8 times use:
pi> sudo python flash2.py 8
Note that in your Python code you use int(sys.argv[1]) to get the number of times to flash:
sys.argv[1] refers to the first word on the command line after the name of the python file. sys.argv[0] would give you the name of the python file itself (flash.py in this case).
the int function converts strings (letters and words) into numbers that Python can understand. When Python reads in the “8” from the command line it treats it like the character “8” rather than the number 8. You need to tell Python that “8” is a number.
Sound: Using SOX
You can do a lot with sound–record, play files, synthesize notes–on the Pi using the command line program SOX. Install SOS (and mplayer and ffmpeg which are necessary as well) using:
pi> sudo apt-get install sox mplayer ffmpeg
Installation may take a while, but when it’s done, if you plug in speakers or headphones–the Pi has no onboard speakers–you can test by synthesizing an E4 note, which has a frequency of 329.63 Hz (via Physics of Music Notes), that lasts for half a second using:
pi> play -n synth .5 sin 329.63
Now play is a command line program that’s part of sox, so to use it in your Python program you have to tell Python to import the module that lets Python talk to the command line: it’s called os. Then you make a Python program to play the note using os.system like so:
test_sound.py
import os
os.system('play -n synth .5 sin 329.63')
And run the program with:
pi> sudo python test_sound.py
Combining light and sound
Now we bring the light and sound together a the Python program:
flash-note.py
#!/usr/bin/env python
from time import sleep
import RPi.GPIO as GPIO
import sys
import os
cpr = 17 ## The GPIO Pin output number
GPIO.setmode(GPIO.BCM) ## Use board pin numbering
GPIO.setup(cpr, GPIO.OUT) ## Setup GPIO Pin 7 to OUT
for i in range(int(sys.argv[1])):
GPIO.output(cpr, True)
os.system('play -n synth .5 sin 329.63')
GPIO.output(cpr, False)
sleep(0.5)
GPIO.cleanup()
which you run (repeating the note and light 5 times) with:
pi> sudo python flash-note.py 5
Notice that we’ve replaced the middle sleep(0.5) line with the call to play the note because we don’t need the delay since the note plays for 0.5 seconds.
Playing a Tune
The student, N.D., spent some time working through this programming and adding wiring to the Pi breadboard in order to play the first few notes of Mary Had A Little Lamb. By the time we ran out of time, and she had to do her presentation to the rest of the upper-school, she’d come up with this:
red-light-flash.py
#!/usr/bin/env python
from time import sleep
import os
import sys
import RPi.GPIO as GPIO
os.system('play -n synth 2 sin 543.21')
cpy = 22
cpr = 17
cpw = 23
GPIO.setmode(GPIO.BCM) ## Use board pin numbering
GPIO.setup(cpy, GPIO.OUT) ## Setup GPIO Pin 0 to OUT
GPIO.setup(cpr, GPIO.OUT)
GPIO.setup(cpw, GPIO.OUT)
for i in range(int(sys.argv[1])):
GPIO.output(cpy, True)
GPIO.output(cpr, False)
GPIO.output(cpw, False)
os.system('play -n synth 1 sin 578.00')
GPIO.output(cpy, False)
GPIO.output(cpr, True)
GPIO.output(cpw, False)
os.system('play -n synth 2 sin 440.00')
GPIO.output(cpy, False)
GPIO.output(cpr, False)
GPIO.output(cpw, True)
os.system('play -n synth 1 sin 400.00')
GPIO.cleanup()
which is run (5 times) using:
pi> sudo python red-light-flash.py 5
Creating User-Defined Functions
(As a note to N.D., because we ran out of time before we could get to it.)
You’ll notice it takes four lines to turn a light on, turn the other lights off, and play the note. You’ll also note that you have to repeat the exact same code every time you want to play a specific note and it becomes a pain having to repeat all this every time, especially if you want to play something with more notes. This is the ideal time to define a function to do all the repetitive stuff.
So we’ll create a separate function for each note. Mary has a little lamb uses the notes E4, D4, and C4. So we get their frequencies from Physics of Music Notes and create the functions:
Using English words like “blue eyes” to represent genes in DNA strings with the DNA Writer runs the risk that students start to wonder if actual genes are coded in English.
I’d say it was a small risk, but today I did have that question from a couple of students today.
Fortunately, it was quite easy to disabuse them of the impression: they didn’t actually believe it, but they just had to know for sure.
I did like one of the questions though, “Does that mean that Spanish people have DNA written in Spanish?”
Embedding the Tiles
With that caveat, since I, and a few of my students, like the pretty patterns the DNA Writer produces (see above), I created a way to embed the color sequences into other webpages like this blog.
By default, the embedded image links back to the DNA Writer website, but you can adjust it so that it does not. Instead, the nucleobase tiles will change color when you click on them. The color changing helps keep track of where you are if you’re trying to string the sequence in beads.
For academic purposes, you can also change the message you get when the mouse hovers over the tiles. By default it give the plain English translation, but you can make it say whatever you want, or even have it just show the base sequence.