Introduction: Luna Mod Looper Basic Stamp2 Version

Introduction:

By the way, you can find the program code for this project at this link:
http://www.mediafire.com/?c0z2b1wc10uiz53

Please let me know if you have any problems downloading the code

Motivation: Make magazine is fantastic! I read most of them when I can get them at a local community college library. A while ago, I was able to read Make: 26 (http://makezine.com/26/) which has the Luna Mod Looper project. I read through the article and found out that they used a PICAxe (frown - but only because I do not have one)... However, there are lots of people out there that also do not use the PICAxe and I thought I would probably find a version for the Arduino. I searched, I found and I built. It really did not take long but in the end I was not satisfied with the sound.

I moved on to other projects and got a Propeller as my friend was using his for a Luna Mod Looper and had made several versions within a couple of weeks. After doing a couple of small labs with the Propeller I figured the best way to learn about some of the ICs I have was to go through the StampWorks Discovery kit I already had. PBASIC is simple and easy to use, quick to get projects up and running and easy to follow properly written code. The Luna Mod came back to mind once I completed a few experiments so I searched for a Basic Stamp 2 version. I found exactly 0!

So I decided to make my own and found out in the process why there are no other Luna Mod Loopers posted on the Web. They only allow for 8 places for notes in the loop, at least for the way I did it. If you are still interested in how to make one, maybe because you do not have a PICAxe, an Arduino, a Propeller or any other microcontroller (mcu) besides the BS2, read on. Also read on as the code using PBASIC is easy enough to follow. Actually, to better understand how the device featured in the magazine worked, I installed the IDE (Integrated Development Environment) for the PICAxe so I could look at the code provided. The PICAxe runs on its own version of BASIC.

Let me say here that I have no affiliation with Parallax. I am however, generally impressed with their products and the documentation that they provide. IMHO, it is some of the best available, especially for beginners.

If you just want to build the project you are completely welcome to but, please read the Conclusions as there are some interesting ideas for which I am wondering if anybody has some good ideas. You can just read the Steps for information but especially the schematics. For every schematic I provided there is either something in the schematics for the Professional Development Board or in the "StampWorks" book.

IMPORTANT TIP: Read the IMPORTANT TIP sections which are at the end of the Steps. If you are already familiar with programming and the BS2 then they are a reminder. If you are not very familiar with programming and the BS2 then they are good points to help you troubleshoot and avoid problems.

Precursory Steps:

Feel free to skip to the Step 1 if you are familiar with building mcu projects.

Block Diagram:
When building a project that you have though of, draw a block diagram. This will help you to figure out how your project is supposed to work, what types of materials you will need, and to some extent, how it will be programmed. The project I chose was not my idea but I made a block diagram to show you how one might do it. Even if the project is not your idea, make a block diagram for practice. They really do help you with visualizing how the project is supposed to work.

Note the direction of the arrows. The direction helps you see which parts are inputs and which are outputs. Inputs have the arrows pointing to the BS2 mcu. Outputs have arrows pointing from the BS2 mcu to that device.


Procedure of Operation:

The procedure of operation is a list of steps that will be used to operate the whole device. You might need more than one list if your project has different functions (too many functions for one list). You can find the list I wrote for this project's operation below:

Luna Mod Looper Steps:

1) Turn on Looper.
2) LED On light turns on.
3) Default loop plays at default tempo. Tempo LED flashes at tempo. (May change later to last loop and tempo played).
4) Adjust tempo as needed.
5) Press button to start recording. (Might change it to hold to keep recording. Currently, it will be a press to start recording and press to stop recording.)
6) Change frequency pot for each desired spot in the sequence.
7) Have 4 LEDs light up, one for each beat in a 4/4 measure.
8) Press button to stop recording.
9) Playback recorded loop. <== ADDED AFTER, for clarity and consistency.

Note that I forgot to add a step where the looper actually plays the loop. However, in the next step, the flowchart, I put in the playback. If you are doing this for a school project then you should go back and add in the steps you forgot. It will provide clarity and consistency- for your project and make it easier to follow.


Flowchart:

It is a good idea to look up the symbols for a flowchart in a textbook or search for them on the Web. This will help you to decide which symbols to use for your flowchart. Your flowcharts can be simple or complicated. For complicated projects, you should have a flowchart that shows operation of the project as a whole. For that same project you will need other flowcharts documenting operation of different functions. The flowchart I made was very simple as it is not a very complicated one. Looking back at the block diagram confirms this.


List of Components:

1) Basic Stamp 2 module   x 1
2) NOPB, normally open pushbutton  x 1
3) 10 kΩ potentiometer   x 3
4) 220 Ω resistor    x 3
5) 470 Ω resistor    x 3
6) 10 kΩ resistor    x 2
7) LM386 audio op amp   x 1
8) 1 kΩ resistor    x 2
9) 4.7 kΩ resistor    x 1
10) 10 Ω resistor    x 1
11) 10 uF capacitor, electrolytic  x 2
12) 0.01 uF capacitor    x 1
13) 0.1 uF capacitor    x 5
14) 33 nF capacitor    x 1
15) 220 uF capacitor, electrolytic  x 1
16) 8 Ω or 4 Ω speaker (salvaged ?)  x 1
17) 9 V or 12 V power supply  x 1
18) 5 V power supply    x 1

For the 9 V and 5 V power supplies there are a couple of ways to accommodate these requirements:
Option 1: Use a 9 V AC-DC converter wall-wart type power supply and an LM7805 IC with two capacitors as shown on the specification sheet. These are usually something like a 0.33 uF capacitor on the input and a 0.1 uF capacitor on the output.
Option 2: Use an LM7809 and an LM7805 power supply. The LM7809 would be built the same as the 5 V power supply.

Here is just one site that can be used to look up spec sheets:
Datasheet Catalog: http://www.datasheetcatalog.com/

OPTIONAL: For this project I used my Professional Development Board (PDB) made by Parallax. The components used come from the schematics for the PDB and the book "StampWorks" which is also from Parallax.

If you are a little more familiar with electronics then you can substitute components as desired. The schematics will be provided throughout the Instructable. Parallax provides most of their books and schematics for their products for free. The PDB comes with a 12 V wall-wart power supply but the schematic for the audio op amp section shows a 9 V supply on pin 6 of the LM386. Either voltage power supply should work as the main one needed is a 5 V power supply. The PDB has both connections for a Vin which gives you access to the power supplied by wall-wart and a 5 V supply made available from the LM1084 found onboard.

Parallax website: http://www.parallax.com/

If you have the PDB then you should need only the following list of components:
1) 0.1 uF capacitor  x 2
2) 8 Ω or 4 Ω speaker  x 1
3) jumper wires  x lots


Here is the latest video - it is prototype #3!!



Real-time playing of the Luna Mod Looper BS2 Version, prototype #3:



1st Video, 1st Prototype:



1st Protoype, Improvement:

Step 1: Look Up Musical Notes Online or in a Book

Using my process steps and my flowchart helped me to figure out what features I wanted to work on first. Sometimes it is best to follow the flowchart because it provides you with a methodical line of reasoning. When it comes time to make decisions about what happens next in the program then you might decide to follow the flow of the decision (places where there is a "Yes" or "No"). Remember to leave options in the programming or at least know that you will have to go back and follow the path of a different choice later.

Look up the frequencies for musical notes for a standard Western Music type system. Here is the site I looked up:
http://www.phy.mtu.edu/~suits/notefreqs.html

The BS2 uses integer math. That means that number resulting from any calculations will result in an integer answer. Any numbers on the fractional side of the decimal point will be truncated. The notes I used for my program have been rounded to the nearest integer. This might result in slightly off-pitch sounding notes but that is what is available.

Start your program with the standard PBASIC instructions:
' {$STAMP BS2}
' {$PBASIC 2.5}

These are commented out but the IDE reads them anyway. If you enter in the wrong statements, it will affect your program. I just use the buttons at the top of the IDE to enter these instructions. The program has been heavily commented so read the comments to find out why I did much of what I did.

IMPORTANT TIP: When programming this device, I suggest connecting the parts in the order provided in the steps. You can see how the program works by following the code including the parts that branch off into subroutines. However, to really learn how to use the BS2 and PBASIC you need to figure out how each instruction works. To do this you can do a couple of things:
Use the Parallax book or PDF "What's a Microcontroller?" and the "StampWorks" book or PDF. When you install the Basic Stamp Editor IDE you will automatically get the PDF "Basic Stamp Syntax and Reference Manual" that is accessible under the Help menu of the IDE. You do not have to do every experiment or review every instruction, but just the ones you need to use.

Step 2: Connect the Speaker and Get the Default Playback Working

The speaker was connected to P0 of the BS2 and the speaker input of the audio op amp. Here is the schematic from the Parallax PDF and a photo of the setup for this step (see pictures above):

Write in at least two constants for the notes of the music in your array. Eight notes would be better. Make sure you have rounded the numbers. I used a spreadsheet program to do that work.

As with any program that you write for any mcu, write your programs in small sections so you can test and troubleshoot easily. Writing long sections of code will usually lead to trouble and headaches when it does not work or work the way it is supposed to.

To make sure that I could get the BS2 to even play a  musical note using a constant's name I wrote a quick and small program. This should work:

' {$STAMP BS2}
' {$PBASIC 2.5}

' -- Program: TestFrequencies.bs2
' -- Purpose: This program tests the limits of the frequencies that can be heard from a BS2 module and a speaker.

' -- I/O Definitions --

speaker PIN          0

' -- Constants --

Cm0  CON 16         
Ds8  CON 4978   

' -- Variables --

lowFreq   VAR        Word


' -- Code --

Main:
  FREQOUT speaker, 1000, Ds8
  GOTO Main

*****************************
The program provided above ends with the
GOTO Main
instruction.


IMPORTANT TIP: The DEBUG instruction is your friend. Say hi to it for me. I use it all the time and am well acquainted with it. When the program is doing something funny and I just cannot figure it out by going through the code and checking my wiring, I start placing DEBUG instructions to figure things out. Once you figure out the area of your problem, try to isolate the problem. It is often something such as an improperly sized variable. There were a couple of times when I declared something as a Byte when it should have been a Word. The actual figuring out might require formatting of your DEBUG statements or it might require that you stare at it for a while until you ask yourself the right questions.

Step 3: Connect the Frequency Pot and Get the Program to Display the Values From It

Set up the frequency pot according to the schematic provided above (taken from the "StampWorks" PDF):

If you would like to see a picture of the Frequency pot connected refer to the photo in the previous step. It has the Frequency pot and the speaker connected. The picture with this step has both the Frequency and the Tempo pots connected.

It is best to connect only one of these sets of pots although you will need two for the project. Work with the first one until you understand how it works with the RCTIME instruction. Then connect the second set identical to the first except connected to a different output pin.

Remember to use the DEBUG instruction to be able to see what the values are as you turn the pot. Make sure you use the proper formatting. You can look up the different types of formatting used in DEBUG statements but generally you will use something like this:
DEBUG ? variableName

Once you get the Frequency Pot showing values on the DEBUG terminal, work on getting the BS2 to play the frequencies for the values. This will most likely require some scaling as your maximum and minimum values you get from the RCTIME variable will not equal the full range of musical notes (as found in the table on the website I provided in Step 1. Here is the link again:
http://www.phy.mtu.edu/~suits/notefreqs.html

Check the program for the whole reasoning for each number. In the end, I used a spreadsheet to calculate the interval for each potFreqVal as it corresponds to each note. The range does nor work out exactly as desired so it was adjusted a little by hand and when the notes either fell short of the entire range or stopped outside the range then I just accepted that range for what it was. You can adjust the interval for what you would like. Remember that the BS2 only works with integers and that the numbers to the right of the decimal place will be truncated and not rounded. Also remember that the numbers in your spreadsheet are under you control.

Step 4: Know Your Mcu Limits and Connect the Record Pushbutton

You might think that connecting the LEDs would be next but I think you need to figure out how the looping section of the program works before connecting LEDs. They need to be synced with the tempo. Syncing the LEDs was actually one of the problems that gave me the most trouble. It had something to do with more than one Boolean expression in my IF statements related to the LEDs but I cannot tell you why. My IF ELSEIF statements contained two Boolean expressions and there were no problems due to that. Now, back to what really happens in Step 4.

WARNING!!: Before you go any farther you need to know that all mcu's have limits on how much current they can supply and sink. Supply current is how much current exits the pin and it goes into some external devices. Sink current means that current flows into the pin and through the mcu to ground (Vss in this case). The limits for the BS2 are as follows (double check the "Basic Stamp Syntax and Reference Manual," page 14, Table 1.2): each pin can sink 25 mA and source 20 mA. Each group of pins, P0 - P7 and P8 - P15 should not collectively sink more than 50 mA or source 40 mA if using an external 5 V regulator. Those numbers are sink 50 mA or source 40 mA for all I/O pins if using the onboard 5 V regulator.

IF YOU EXCEED THESE LIMITS YOU WILL DAMAGE YOUR MCU!!! Sometimes you just loose an I/O pin but worse things might happen. This means you need to use your Ohm's Law, V=IR.

You have probably noticed at this point that the record buttons and the tempo pot are on P15 and P14, respectively and the rest of the I/O Pins. This is due to the information provided in the Warning above.

On with the show:
The record button needs to be connected. Connect a jumper wire from P15 to PB0 in the pushbutton section if you are using a PDB. If not then connect a NOPB as show in the schematic below. This also happens to be the way the PDB has its pushbuttons wired except anything can be connected on the PDB, not just P15.

Look at the picture to see:
You can see the green wire as it forms an arc across the solderless breadboard from P15 to PB0 on the PDB.

In order to get the pushbutton to work you will most likely use the BUTTON command. My instruction looks like the following:

BUTTON recBtn, 0, 255, 0, btnData, 1, Record

Check the "Basic Stamp Syntax and Reference Manual" for a thorough explanation. Here, recBtn is the pin#, 0 is the Downstate, 255 is the Delay, 0 is the Rate, btnData is the Workspace, 1 is TargetSate, and Record is the Address. The recBtn is 15 for P15 and 255 is used because I do not want an auto-repeat but still want the debounce. Rate is 0 because I do not want any measurement of cycles. I gave btnData only a Byte of variable space because the numbers should be low anyway. The TargetState is 1 because I want the button to trigger the BS2 into record mode when it is still pressed. Setting it to 0 would cause it to go to Record when it was released. The Address is Record. Record is the subroutine that is triggered when the button is pressed.

Here is a prime spot to practice programming techniques. You do not have to write all the finished code at once here. Your Record subroutine could be just one or two DEBUG statements. Doing so will tell you that your Button instruction worked and sent the BS2 into Record mode. If you try to write all of your code at once then you are often frustrated because of the problems that need to be sorted first.

Item a) Write just one DEBUG statement such as:
DEBUG "You are in Record Mode"

Item b) Add a second and maybe a third statement as follows:
DEBUG ? potFreqVal
DEBUG ? potTempoVal

Item c) When you see the right words show up in your DEBUG Terminal you know that you are in Record mode but you also know that the program is using the variables from the two pots you have for frequency and tempo. Now you can work on getting the rest of the Record mode to work, making sure that the frequencies get placed in your array.

For this step I decided to use a series of IF ELSEIF statements. The tricky part is getting a good range set up and making sure you Boolean expressions have the right numbers for each note. I decided to use just the one comparator for each expression instead of using two.

One comparator : <, > which stand for less than and greater than respectively
Two comparators: <=, >= which stand for less or equal to and greater than or equal to respectively

I used the one comparator as I figured it would take a little longer to evaluate the expression with two comparators as opposed to one comparator.

Note that in the final version of the program provided that there are three ranges of notes contained within three subroutines. You will need the necessary code after the IF ELSEIF statements to take the sorted frequency value and play it so you can hear the notes you are selecting by turning the potentiometer. If you wait to include those statements I doubt the Record subroutine will work as planned. For the first two ranges of IF ELSEIF statements, the last ELSEIF statement contains an instruction to go to the next range of notes by using the GOSUB Subroutine_Name command.

Note that the BUTTON instruction is used in the second (of three) times here. This allows the person to press the button at any time and have the program exit the Record mode and switch into Play mode. Actually, the BS2 actually will run through all the sequential code before coming upon the BUTTON instruction so you might have to wait briefly before the change happens.

IMPORTANT TIP: There are a few functions available with the Basic Stamp Editor IDE that you should become familiar with using. Two very useful ones are the Syntax Check and the Memory Map. I say this here because when I wrote the IF ELSEIF nested statements I found out that you can only have 16 ELSEIF statements in addition to the opening IF statement. Using the Memory Map brought this to my attention. The Syntax Check will do the same thing. If you have more then it will show up when you use these functions. Memory Map us useful to find out how much variable space you have left. It also lets you know how much more space you have to write code. Syntax Check is good to run before you try to Run your program. It will let you know very quickly if any syntax is out of order without the extra time spent trying to access the mcu. Once you spend enough time programming mcu's you will appreciate both functions.

Step 5: Get the Playback Subroutine Working

By now you should have your speaker, the Frequency pot and the Tempo pot connected and of course the all important Record button. The LEDs are still not connected at this point. The Play subroutine needs to be written.

What is the name of this project? The Luna Mod Looper right? The key is right in the name. You will need a loop to playback the notes recorded.

The main method for making sounds with the BS2 is the FREQOUT instruction. I use it as show below:
FREQOUT speaker, actualTempo, loopList(cntr)

The arguments are as follows:
speaker: the pin # to which the speaker or audio amp is connected,
actualTempo: the scaled tempo obtained from an equation using the variable potTempoVal, and
loopList(cntr): the variable found in the cntr place of the array called loopList

Looking at the code shows how the playback loop was used.

You might want to write a simple loop first just to see if the program will playback the loop once. Once you do that you can work on having the loop play continuously. Note that the BUTTON instruction is included here as well. This is because you want to be able to record at any time if you want to change what you have.

Step 6: Connect the LEDs and Sync Them to Playback and Record Tempo

Now you can connect the LEDs. First connect the tempo LED and work on getting it to function properly. Adding the other two LEDs might make you want to move to quickly and end up with more errors than you can effectively troubleshoot. See the schematic below for how to connect each LED.

The tempo LED is connected to P3 and LED0. When putting things together you can arrange this any way that you want as long as it will not damage the mcu and it makes sense to you. I placed the tempo LED to the far right. It makes sense to me partially because it is the farthest to the right. In the later pictures you will see I placed the other LEDs apart from one another to allow for me to tell their functions apart from one another. I could have used different coloured LEDs if I want to place them on the solderless breadboard. That can cut down on confusion as to what function each LED has. If I ever build a permanent Luna Mod Looper I will use different coloured LEDs if it makes sense to do so.

As I said before, this was one of the most troublesome part of the project to get operating correctly. When I wrote the code and loaded into the BS2 it sort of worked but not exactly the way I wanted it to. Troubleshooting involved inserting DEBUG statements where there were none before.

If your code just will not seem to work for any new little change that you make, sometimes it requires a new approach or a slightly different change. To light the tempo LED I had to take out one or more Boolean expressions and write it into a new ELSEIF statement. It worked but I do not know why it would not work the first way I did it.

Once I had the tempo LED working the way I wanted it to I moved on to the last part of the project - getting the record LEDs working. This required placing code in a subroutine I had not intended. I had not considered it before and I thought this code would be placed in the obvious place of the Light_Leds subroutine. It could have been but it probably would have required another variable or two. I did not want to add more variables at this point as with the final version there are only 4 bits left (or a Nib or half a Byte or 1/4 of a Word).

So, I placed the code in the Record subroutine. This almost worked perfectly until I ran the new program and found an LED stayed on when unintended. This mean placing a piece of code in the Play subroutine that would make sure both record LEDs were turned off.

IMPORTANT TIP: At any time you decide, you can insert DEBUG statements in ANY part of the program. This can help to make sure that previously written sections of code are working properly. Such statements can also be useful to determine how other parts of code are affecting the new code and vice versa. IMPORTANT TIP WITHIN THE IMPORTANT TIP: You can end up with a lot of DEBUG statements in different parts of code. Make sure to make certain DEBUG statements have words in them that allow you to tell instantly from what part of the program they are executed. Also note that you can always use PAUSE # (example: PAUSE 1000 to pause the program for 1000 milliseconds) to slow the program down so you can have a decent look at the information.

Step 7: Conclusions

See the photo provided above for the finished Luna Mod Looper BS2 version prototype:

Of course, there was a lot of cleanup of the code to remove DEBUG statements (can slow down the program), and unnecessary variables. There are lots of times that you will find that a program will not be coded the way you originally thought. This means cleaning things up that are not needed and making new variables and sections of code.

This version sounds much better than the Arduino version. Then again, the Arduino version was connected directly to a speaker whereas the BS2 version has that nice audio amp. I have not been able to compare it to my friend's Propeller version.

In the future I would like to make another BS2 version that allows for more notes in the loopList. That means going back to the StampWorks book and working through the experiments to figure out how to use external devices such as the MicroChip 24LC32 EEPROM. Hopefully the EEPROM will allow for more loop space as well as functionality allowing saving of loops for playback for the next time the Luna Mod is turned on. My friend suggested chaining multiple Luna Mod's together which would be an interesting project. See the second picture provided above of another version of a BS2 beside the PDB:

This other version is called a Stamp Stack II and it fits right on a solderless breadboard. Power is provided with something such as a 9 V battery. The Stamp Stack II has a low voltage dropout regulator onboard so you can run your battery quite low and the mcu will still work. Look it up if you like.

Look for the video on YouTube. It can be found under the name "Luna Mod Looper Basic Stamp 2 Version."

I almost forgot this part:

There are some ideas I had for possible improvements. Here are the ones I can think of now:

1) Figure out an equation that allows for small scale RCTIME values to be converted into larger frequency values. See the third picture provided above of a graph of single unit step values versus frequency values:

If you look up the type of curve you might find it looks similar to a curve for this equation (but mirror on a vertical axis):
v = VF + (Vi - VF)e-t/τ

where v = instaneous voltage for a capacitor charging with a preceding series resistor,
VF = final voltage,
Vi = initial voltage,
t = time, and
τ = tau, an RC time constant => R is resistance, C is capacitance.

The purpose of using this equation would be to perform the frequency calculations in real time with the  pot values from the RCTIME instruction stored as Byte variables thus increasing the total notes to 16.

Another possibility for decreasing pot RCTIME values would be to use smaller value potentiometers thus decreasing the amount of time it takes for the capacitor to discharge to a low state. See the RCTIME instruction in the "Basic Syntax and Reference Manual" book or PDF.

2) Connect two or more Luna Mod Looper, BS2 versions. This might work as there is still some variable space to work with. It might also be possible with straight coding with numbers. There is rougly 1/2 the programming space left with the current version.

3) Sound projects often sound better with effects. Maybe I can come up with some BS2 based sound processing effects. I know the BS2 is very limited but it is worth a shot. Afterall, nobody else seems to have built a BS2 version of the Luna Mod Looper.

Maybe just some plain old analog effects would be good. There are also some good mcu based effect using other mcu's.

4) To add to the ability to accurately pinpoint certain frequencies, additional pots could be added. Each pot would have its own range of notes. It would probably be necessary to program some sort of precedence so that one pot could not override another pot as it is being used.

5) Adding an LCD screen so you can see the notes as they are entered would be cool.

6) Another function to make use of the LCD screen would be to able to add individual notes as you want.

7) Be able to keep the loops you make in memory for the next time you turn on the looper.

Please let me know if you have any ideas for improvements, criticisms or ideas for this project.