Play Some BASIC Stamp Tones

Try it Out

  • Download the BASIC Stamp Tilt Tones Code
  • Connect your board to your computer and a power supply.
  • Unzip and run BASIC Stamp Tilt Tones.bs2
  • Verify that when your board is laying flat, it makes a constant tone with a single LED lit up.
  • Try tilting the board to change the tone and light up different LEDs.

 

' BASIC Stamp Tilt Tones.bs2
'
' {$STAMP BS2}
' {$PBASIC 2.5}

led    VAR  Nib                               ' I/O pin number of LED to turn on/off
note   VAR  Word                              ' note to play, in Hz
index  VAR  Nib                               ' holds state of tilt sensor's output pins
beat   CON  500                               ' how long to play each note, in ms

index = 0                                     ' initialize index to 0

DO

 index.BIT0 = IN14                            ' read P14 tilt sensor state, store in bit 0 of index
 index.BIT1 = IN15                            ' read P15 tilt sensor state, store in bit 1 of index

 LOOKUP index,[4, 6, 8, 10], led              ' index selects LED to light
 LOOKUP index,[2093, 2349, 2637, 2794], note  ' index selects a note (frequency in Hz) to play

 HIGH led                                     ' turn LED on
 FREQOUT 1, beat, note                        ' play note on P1 piezo for beat ms
 LOW led                                      ' turn LED off

LOOP

If nothing lights up when you try the circuit, double check that you have the LEDs’ cathode leads connected properly to GND and not to the I/O pins.

 

How it Works

The 4-Directional Tilt Sensor uses two I/O pins; we have them connected to P14 and P15 in our example.  The diagram below shows what each I/O pin will detect when the sensor is tilted in each direction.

Let’s take a peek inside the program.  First, the variables led, note, and index are declared, along with a constant named beat that is initialized to 500.  Also, index is initialized to zero.

led    VAR  Nib  
note   VAR  Word
index  VAR  Nib  
beat   CON  500  
                 
index = 0       

Now for the action.  The code inside the DO…LOOP checks the sensor output, and uses that information to select a note to play and an LED to light up. Let’s look at each line to see exactly how that happens.

DO                 
                   
 index.BIT0 = IN14
 index.BIT1 = IN15

The line index.BIT0 = IN14 translates to “check the input state of I/O pin P14 to see if the sensor is sending a 0 or a 1, and store it in the lowest bit (bit 0) of the variable named index.”  Similarly, the next line stores the input state of P15 in bit 1 of index.

Recall that index was declared as a NIB, a 4-bit variable, initialized to zero (binary 0000). So, depending on which way the sensor is tilted, these two lines of code will set the index variable to binary 0000, 0001, 0010, or 0011. That's the same as decimal 0, 1, 2, and 3. 

Next, the freshly-set index variable is used in two LOOKUP statements.  

 LOOKUP index,[4, 6, 8, 10], led             
 LOOKUP index,[2093, 2349, 2637, 2794], tone

The first line translates to “find the 'index-th' element in the list and assign that value to the led variable.”  For example, if you tilt the board towards you, P14 and P15 both detect a 0, making the value of index 0000. The 0th element in the list is 4 (counting 0,1,2,3) so that’s the value that gets assigned to the led variable.

In the next line, index is used again to select the corresponding frequency to assign to the note variable. Since index is still 0000, the 0th value 2093 is assigned to note. Continuing with this example,

HIGH led               
FREQOUT 1, beat, note
LOW led               

...HIGH led turns on the LED connected to P4.  Then, the piezo speaker on P1 plays the 2093 Hz note. The note lasts for 500 ms, since that’s how the constant beat was initialized at the beginning of the program.  After that, LOW led turns the P4 led back off, then loop repeats.