Shop OBEX P1 Docs P2 Docs Learn Events
Cleaner code for programming songs into BS2 wanted — Parallax Forums

Cleaner code for programming songs into BS2 wanted

humble scubahumble scuba Posts: 1
edited 2008-04-09 00:31 in BASIC Stamp
Hey, all.

I'm pretty new to this whole basic stamp thing, and robotics in general, really.

I've programmed a short little song into my chip, and it plays recognizably through a piezo buzzer. The code is just a little bulky, and I was wondering if there's a cleaner way to do it.

Here's the code:


a CON 831
b CON 932
c CON 1047
d CON 1109
e CON 1245
f CON 1397
ah CON 1661


DO
FREQOUT 0, 100, a
FREQOUT 0, 100, b
FREQOUT 0, 100, d
FREQOUT 0, 100, b
FREQOUT 0, 300, f
FREQOUT 0, 300, f
FREQOUT 0, 500, e

PAUSE 50

FREQOUT 0, 100, a
FREQOUT 0, 100, b
FREQOUT 0, 100, d
FREQOUT 0, 100, b
FREQOUT 0, 300, e
FREQOUT 0, 300, e
FREQOUT 0, 500, d

PAUSE 50

FREQOUT 0, 100, a
FREQOUT 0, 100, b
FREQOUT 0, 100, d
FREQOUT 0, 100, b
FREQOUT 0, 400, d
FREQOUT 0, 200, e
FREQOUT 0, 200, c
FREQOUT 0, 200, b
FREQOUT 0, 300, a

PAUSE 100

FREQOUT 0, 250, a
FREQOUT 0, 400, e
FREQOUT 0, 600, d

PAUSE 200

FREQOUT 0, 100, a
FREQOUT 0, 100, b
FREQOUT 0, 100, d
FREQOUT 0, 100, b
FREQOUT 0, 300, f
FREQOUT 0, 300, f
FREQOUT 0, 500, e

PAUSE 50

FREQOUT 0, 100, a
FREQOUT 0, 100, b
FREQOUT 0, 100, d
FREQOUT 0, 100, b
FREQOUT 0, 400, ah
FREQOUT 0, 200, c
FREQOUT 0, 200, d
FREQOUT 0, 200, c
FREQOUT 0, 300, b

PAUSE 50

FREQOUT 0, 100, a
FREQOUT 0, 100, b
FREQOUT 0, 100, d
FREQOUT 0, 100, b
FREQOUT 0, 400, d
FREQOUT 0, 200, e
FREQOUT 0, 200, c
FREQOUT 0, 200, b
FREQOUT 0, 300, a

PAUSE 100

FREQOUT 0, 250, a
FREQOUT 0, 400, e
FREQOUT 0, 600, d

PAUSE 100
LOOP

To put it simply, I looked up the frequencies of the notes I wanted to play, declared them as a constant, then called on the notes instead of having to type out the actual frequency over and over again.

Any help cleaning this up or rewording it would be greatly appreciated, though. Thanks.

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2008-04-08 23:09
    Probably the only thing you could do differently would be to store the frequency and duration as words in EEPROM using the DATA statement, then using READ to fetch these values into variables, then do a FREQOUT statement using the variables. If there was a frequency of zero, that would cause a PAUSE to be done.

    Look in the Basic Stamp Manual in the sections on the DATA and READ statements.
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2008-04-09 00:31
    Here is a thread that may also help.

    -Phil
Sign In or Register to comment.