Back to Activity
Tutorial
Morse code
{% trans 'Sorry, your browser does not support canvas -- please either update your browser or submit your requirements by phone or email.' %}

Accelerometer

Loading Blockly...

Step 1 /7

Introduction

You can easily create a morse code transmitter with just a couple of blocks to check if a button is pressed and play a tone.

Step 2 /7

Setting up music leg

First you need to set leg 0 into music mode.

  • Click the blue cog on the start block and tick the checkbox next to leg 0 input/output, click the cog again to close this window.

  • Now click the leg 0 drop down that has appeared on the start block and select music (PWM) output.

Step 3 /7

Creating your signals

  • From the Music menu, drag in a start tone block and snap it below your start block.

  • From the Sprite menu, drag in a draw sprite block and snap it below your start tone block.

  • Also from the Sprite menu, drag in a build sprite block and snap it to your draw sprite block. Tick all the boxes on your build sprite block.

Step 4 /7

Looping your program

You will want your program to loop so it will continuously respond to button presses.

  • From the Loops menu, drag in a repeat while loop and snap it below your draw sprite block.

Step 5 /7

Button presses

If you ran your program now, it would beep forever. We want it to sleep (turn off the sound and LEDs) when the buttons are not pressed.

  • From the Loops menu, drag in another repeat while loop and snap it inside your first repeat while loop.

  • From the Logic menu, drag in an and block and snap it to your inner repeat while loop. Click its drop-down arrow and select or.

  • From the Inputs/Outputs menu, drag in two button A pressed blocks and snap them into the gaps of your or block.

  • From the Basics menu, drag in a sleep until button changes block and snap it inside your outer repeat while loop, below your inner repeat while loop.

Step 6 /7

Test your code

Click the Play button to run your code. Click the buttons on the CodeBug emulator to flash the LEDs and tone.

Step 7 /7

Download your code

Click the Download button to download your program and then load it onto your CodeBug.