Program your Lego NXT robot in a Scratch-like language

Electric Guitar

When we decided to build the NXT electric guitar project [4], we found that programming in the native Lego software was very complex, with a difficulty rating of 4/4. Doing the same project in Enchanting, however, turned out to be quite easy. Because we were short some Lego pieces called for in the original project, we made a simpler guitar (Figure 5). (Step-by-step building directions to make the more complex guitar are available online.)

Figure 5: A simplified electric guitar.

To begin the programming, you need to configure the ultrasonic sensor by clicking the Sensing button, then selecting the Configure Sensors button. Next, drag the ultrasonic sensor block to the port you are using (Figure 6).

Figure 6: Configuring an ultrasonic sensor.

For our guitar project, we used an octave (eight notes), and each note was 3cm on the guitar neck. Our code had eight if blocks in which we defined the distance from the ultrasonic sensor to the neck slider and the note to play in that range (Figure 7). When you click on the note in the play note block, a little keyboard pops up to show you the key.

Figure 7: The if block and notes.

In the final code (Figure 8), a forever block is used to keep the program running. An if block is used to determine whether you have pushed the Enter key; if so, a note is played based on the distance of the slider bar from the ultrasonic sensor.

Figure 8: Electric guitar code.

Some future project ideas could include:

  • Use the left or right buttons to change the instrument.
  • Use the left or right buttons to change the beat length.
  • Create some tunes that people could try to play back.

Reloading Lego Firmware

If you ever need to reload the original Lego NXT firmware, the Lego software has an Update NXT Firmware option (Figure 9). You can find an Enchanting tutorial online [5].

Figure 9: Reloading the original NXT firmware.

The Author

Brooke and Leah Metcalfe are 12-year-old twins who live in Burlington, Canada. When they aren't doing computer projects with their dad, they like swimming, paddleboarding, and skiing.

Buy this article as PDF

Express-Checkout as PDF

Pages: 4

Price $2.95
(incl. VAT)

Buy Raspberry Pi Geek

SINGLE ISSUES
 
SUBSCRIPTIONS
 
TABLET & SMARTPHONE APPS
Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content