Name:
tune
Syntax:

For M2 parts except 08M2 -
TUNE pin, speed, LED_mask, (note, note, note...)

For 08M2 and other 8-pin parts -
TUNE LED_option, speed, (note, note, note...)

For all other parts -
TUNE pin, speed, (note, note, note...) 

pin - is a variable/constant which specifies the i/o pin to use (not available on 8 pin devices, which are fixed to output 2).

speed - is a variable/constant (1-15) which specifies the tempo of the tune.

notes - are the actual tune data generated by the Tune Wizard.

LED_mask - (for M2 parts except 08M2) is a variable/constant which specifies if other PICAXE outputs (on the same port as the piezo) flash at the same time as the tune is being played. For example use %00000011 to flash output 0 and 1.

LED_option - (for 08M2 and other 8-pin parts) is a variable/constant (0 -3) which specifies if other 8 pin PICAXE outputs flash at the same time as the tune is being played.

0 - No outputs
1 - Output 0 flashes on and off
2 - Output 4 flashes on and off
3 - Outputs 0 and 4 flash alternately
Description:

The tune command allows musical 'tunes' to be played. Playing music on a microcontroller with limited memory will never have the quality of commercial playback devices, but the tune command performs remarkably well. Music can be played on economical piezo sounders (as found in musical birthday cards) or on better quality speakers.

Most users will use the 'Tune Wizard' to automatically generate the tune command, by either manually sequentially entering notes or by importing a mobile phone ring tone.

All tunes play on a piezo sounder or speaker, connected to the output pin (must be output 2 (leg 5) of the 8 pin devices). On all 8 pin and all M2 parts other outputs can be enabled to cause them to 'flash' in time to the music. The LEDs 'toggle' on/off at the end of every note. On 14/18/20 pin chips to have two (or more) LEDs flash alternately set one LED high, and the other low, before the tune command is used. 

To play back MP3 music files please see the SPE035 MP3 Player

PICAXE Tune Wizard

Looking for RTTTL ringtones? - see here

The Tune Wizard allows musical tunes to be created for the PICAXE. Tunes can be entered manually using the drop-down boxes if desired, but most users will prefer to automatically import a mobile phone monophonic ringtone. These ringtones are widely available on the internet in RTTTL format (used on older Nokia phones). Note the PICAXE can only play one note at a time (monophonic), and so cannot use multiple note (polyphonic) ringtones.

To start the Tune Wizard click the PICAXE>Wizard>Tune Wizard menu. The easiest way to import a ringtone from the internet is to find the tune on a web page.

Highlight the RTTTL version of the ringtone in the web browser and then click Edit>Copy.

Move back to the Tune Wizard and then click Edit>Paste Ringtone.

To import a ringtone from a saved text file, click File>Import Ringtone. Once the tune has been generated, select whether you want outputs to flash as the tune plays. The tune can then be tested on the computer by clicking the 'Play' menu (if your computer is fitted with soundcard and speakers). The tune played will give a rough idea of how the tune will sound on the PICAXE, but will differ slightly due to the different ways that the computer and PICAXE generate and playback sounds. On older computers the tune generation may take a couple of seconds as generating the tune is very memory intensive.

Once your tune is complete click the 'Copy' button to copy the tune command to the Windows clipboard. The tune can then be pasted into your main program.

Ring Tone Tips & Tricks:

1. After generating the tune, try adjusting the tempo by increasing or decreasing the speed value by 1 and listening to which 'speed' sounds best.
2. If your ringtone does not import, make sure the song title at the start of the line is less than 50 characters long and that all the text is saved on a single line.
3. Ringtones that contain the instruction 'd=16' after the description, or that contain many notes starting with 16 or 32 (the odd one or two doesn't matter) will not play correctly at normal speed on the PICAXE. However they may sound better if you double the PICAXE processor speed by using a 'setfreq m8' command before the tune command.
4. The PICAXE import filters 'round-down' dotted notes (notes ending with '.'). You may wish to change these notes into longer notes after importing.

Using the TUNE command with the 08M and 08M2

For the 08M and 08M2 the syntax of the tune command is 'TUNE LED_option, speed, (note, note, note...)'.

Tune output is always on output 2 for the 08M and 08M2 and this cannot be changed. If a pin identifier is used as the 'LED_option' this will not change the pin used for tune output but may cause other pins to become outputs and activated. This may cause incorrect program operation and may adversely affect hardware attached to those pins.

Note that the PICAXE Editor and PICAXE compilers will not present an error message when a pin identifier is used for 'LED_option'. The pin identifier will however be taken as a 'LED_option', not as a pin identifier, and the affect on other outputs will vary depending on which pin identifier is specified.

 

Applies To:
All (except 08, 18, 18A, 18X, 28, 28A, 28X, 40X)
See Also:
Related Create:
Share:
Print:

Submit Your Own Code!

You must be logged in to submit code examples. Login now.