Thursday, 29 October 2009

Frequency and Sound Part 2

Storing and Retrieving sequence of Musical notes:
A good way of saving musical notes is to store them in the EEPROM. (Electrically Erasable Programmable Read-Only Memory). To do this use the DATA diractive.
Syntax for the DATA diractive -
     Symbol} DATA {Word} DataItem {, {Word} DataItem,...}
Here is an example of how to use the DATA diractive to store the characters that corespond to musical notes:
                 Notes DATA "C","C","G","G","A","A","G"

You can use the READ command to acces these characters.  The letter "C" is located at addreaa Notes + 0, and a second letter "C" is located at Notes + 1, and so on.  For example, if you want to load the last letter "G" into a byte variable called noteLetter, use the cammand:
         READ Notes + 6, noteLetter

You can also store lists of numbers using the DATA diractive.  Frequency and duration values that the BASIC Stamp uses for musical notes need to be stored in word variables because they are usually greater than 225.  This is how to do that with a DATA diractive.

  Frequencies DATA Word 2093, Word 2093, Word 3136, Word 3136,
                                 Word 3520, Word 3520, Word 3136

Because each of these values occupies two bytes, accesing them with the read command is different from accessing characters.  The first 2093 is at Frequencies + 0, but the second 2093 is located at Frequencies + 2. The first 3136 is located at Frequencies + 4, and the second 3136 is locaded at Frequencies + 6.
Here is a FOR...NEXT loop that places the Notes DATA into a variable called noteLetter, then it plasec the Frequencies DATA into a variable named noteFreq -
      FOR index = 0 to 6
           READ Notes + index, noteLetter
           READ Frequencies + (index * 2), Word noteFreq
           DEBUG noteLetter, "          ", DEC noteeq, CR
      NEXT

Here's an example program that demonstrates how to use the DATA directive to store lists and how to use the READ command to access the valuin that list.
Coding:





Video of TwinklTwinkle:


A better system for storing and retrieving music:
By using Bytes, instead of Words, you can write programs that store twice as much music in the BASIC Stamp.
Here's an eample that introduces how to store musical information in a way that relates to the concepts of notes, durations and rests.  Tempo is also introduced. Only the note characters are stored in the Notes DATA directive because LOOKUP and LOOKDOWN commands will be used to be match up letters to their corresponding friequencies.

         Notes                      DATA            "C","D","E","C","C","D","E","C","E","F","G","E","F","G","Q"
         Durations                 DATA             4,   4,   4,   4,   4,   4,   4,   4,   4,   4,    2,   4,   4,   2
         WholeNote             CON                2000


The first number in the Durations DATA directive tells the program how long the first note in the Notes Data directive should last.  The second duration is for the second note, and so on.
Here's a list of what each duration means:
  •   1 - whole note
  •   2 - half note
  •   4 - quarter note
  •   8 - eighth note
  •   16 - sixteenth note
  •   32 - thirty-second note
Ater each value is read from the Durations DATA  diractive, it is devided into the WholeNote value to get the Duration used in the FREQOUT command.
The "Q" in the Notes DATA is for quit, and a DO WHILE...LOOP checks foe "Q" each time through the loop.  You can insert a rest between notres by inserting a "P".
When you use the lower-case version of the note, it will play a flat note.  For example, if you want to play B-flat, use "b" instead of "B".
Example Program - Frere Jacques.
Coding:





Video:


How does it all work:
The Notes and Durations DATA directives combined with the WholeNote constant are used to store all of the musical data used by the program.
Even though a FOR...NEXT loop is no longer used to access the data, there still has to be a variable (index) that keeps track of which DATA entry is being read in Notes and Durations. The offset variable is used in the LOOKDOWN and LOOKUP commands to select a particular value.  The NoteLetter variable stores a character accessed by the READ command.  LOOKUP and LOOKDOWN commands are used to convert this character into a friequency value.  This value is stored in the NoteFreq variable and used as the FREQOUT command's Freq1 argument.  The noteDuration variable is used in a READ command to recieve a value from the Durations DATA.  It is also used to calculate the Duration used in the FREQOUT command.
The main loop keeps executing until the letter 'Q' is read from the Notes DATA.
A READ command gets a character from the Notes DATA, and stores it in the noteLetter variable. The noteLetter variable is then used in a LOOKDOWN command to set the value of the offset variable. Remeber that offset stores a 1 if "b" is detected and a 2 if "B" is detected and a 3 if "C: is detected and so on.  The offset value is then used in a LOOKUP command to figure out what the value of the noteFreq variable should be.  If offset is 1, noteFreq will be 1865, if offset is 2, noteFreq will be 1976 and so on.
The READ command uses the value of index to place a value from the Durations DATA into noteDuartion.
        READ Durations + index, noteDuration
Then, noteDuration is set equal to the WholeNote constant devided by the noteDuration.  If note duration starts out as 4 from a READ command, it becomes 2000 / 4 = 500. If noteDuration is 8, 1500 / 8 = 250.
       noteDuration = WholeNote / noteDuration
Now that noteDuration and noteFreq are determined, the FREQOUT command plays the note.
       FREQOUT 9, noteDuration, noteFreq
Each time through the main loop, the index value must be increased by one.  When the main loop gets back to the beggining, the first thing the probram does is read the next note, using the index variable.

       index = index + 1
   LOOP

I had a few problems with that one - I didnt really understand it at first. But after reading it a couple times, I'm starting to understand it more and know whats going on.
Entering musical data is much easier when all you have to do is record notes and durations.
I editted the last program to play Beethoven's Fifth Symphony, just by replacing the notes and durations DATA diractives.

Adding Musical Features:
Cell phones that play musi to let you know that someone is calling have three features that were not supported by the previouse section:
  • The play "dotted" notes.
  • They determine the whole note duration from a value called tempo
  • They play notes from more than one octave.
The term "dotted" refers to a dot used in sheet music to indicate that a note should be played 1.5 times as long  as its normal duration.  In this example, a zero means there is no dot white a 1 means there is a dot:
         Dots            DATA          0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0
Cell phones typically take the tempo for a song in beats per minute.  This is the same as saying quarter notes per minute.
         BeatsPerMinute         CON     200

CELL PHONE RINGTONES:
One of the most widely used way of composing, recording and posting notes is one that features strings of text that describe each note in the song. Haere is an example of how the first few notes from Beethoven's 5th look like in RTTTL format:
              Beethoven5:d=8,o=7,b=125:g,g,g2d#,p,f,f,f,2d
This format for storing musical data is called RTTTL, which stand for Ringing Tone Text Transfer Language. 

RTTTL Ringtone Player Application Program
This application program is pretty long; in the book they said I can download it from the parralax site, but I couldnt find it so I had to write the whole thing.
Reveille Video:


Coding:
















I changed the song in the coding by simply replacing the RTTTL_File DATA directive with different strings of coding.
JollyGoodFellow video:


OK thats done with Music, I have like two more chapters to post up.
Hope you enjoyed it!

Thursday, 22 October 2009

Frequency and Sound Part 1

To be honest, this was the hardest chapter so far and I think it would be a good idea to get back to it when I finish the book to the end.  I think it was so hard because there were so many new commands and rules introduced.

So I'll start from the basics. 
The rate of high/low signals sent to the speaker from the microcontroller is called the frequency - it determines the tone or pitch of the beep.
Each time a high/low repeats itself is called a cycle - Number of cycles per second (reffered to as Herts, Hz)

The Piezoelectric speaker:
In this activity I will be experimenting with sending some different signals to the Piezoelectric speaker.


 Piezoelectric Speaker Schematic symbol and drawing.

First of all, I built this simple circuit -



How does the Piezo Speaker work?
When a guital string vibrates it causes change in air pressure, and these changes in air pressure are what your ear detects as a tone.  The faster the change in air pressure the higher the pitch, and the slower the change, the lower the pitch.
When high/low signals are applied to the speakers positive terminal, the element inside the piezo speaker, called the piezoelectric element, it vibrates, and causes changes in air pressure.

Programming speaker control:
The FREQOUT command is a convenient way of sending high/low signals to a speaker to make sound.
FREQOUT command syntax -
       FREQOUT Pin, Duration, Freq1 {, Freq2}

Pin - Value you can use to choose what I/O pin to use.
Duration - Value that tells the FREQOUT command how long the tone should play for (in milliseconds).
Freq1 - Argument is used to set the frequency of the tone, in Herts.
Freq2 - An optional Freq2 argument that can be used to mix frequencies.

This is the first test that I did on a Piezoelectric Speaker -


And the coding -




Here are a fiew ActionTones -


Coding -






Mixing Tones:
When you mix 2000 Hz with 20001 Hz, the tone will fade in and out once every second, at a frequency of 1 Hz.  If you mix 2000 Hz with 2002 Hz, it will fade in and out twice a second, and so on.

Mixing Tones Video -


Coding for MixingTones -




I did some programming on that but whats coming up next is the real stuff so check it out -

Musical Notes and Songs:


This is basically an electronic piano.  Rightmost piano keys and their frequencies.
With this, you can create any song, using the FREQOUT command.
Here's some music for you -
DoReMiFaSolLaTiDo:


Coding -




This is the part that gets pretty complicated, but its pretty cool -
Storing and Retrieving sequence of Musical notes:
A good way of saving musical notes is to store them in the EEPROM. (Electrically Erasable Programmable Read-Only Memory).  To do this use the DATA diractive.
Syntax for the DATA diractive -
            {Symbol} DATA {Word} DataItem {, {Word} DataItem,...}

Here is an example of how to use the DATA diractive to store the characters that corespond to musical notes:
      Notes DATA "C","C","G","G","A","A","G"

This is what I put up so far today, I'll put up the rest later or another day.

Monday, 19 October 2009

Light Meter video

Oh yeah, here is a video to show you how the Light Meter works -

The Light Meter

Hi everyone!
Here I'm going to show you how I leared how to make a simple light meter with a 7-segment LED display, a photoresister, a capacitor and resistors.
On the way I learned some programming tricks that saves time and prevents typing error.  I learned how to use subroutines.  A DO...LOOP is usually called the main routine because all the main activity happens in it.
PBASIC has some commands that can be used to make the program jump out of the main routine, do a job, and then return right back to the same spot in the main routine.  This will allow me to keep each segment of code that does a particular job somewhere other than the main routine.  This process is called executing a subroutine.
Here's an example -

DO
   GOSUB Subroutine_Name
   DEBUG "Next command"
LOOP

       Subroutine_Name:
          DEBUG "This is a subroutine..."
          PAUSE 3000
       RETURN


The command GOSUB Subroutine_Name causes the program to jump to the Subroutine_Name: label.  When the program gets to that label, it keeps running and exicuting commands untill it gets to a RETURN statement.  Then the program goes back to the command that comes after the GOSUB command.

Now I'm going to show you how I programmed the light meter using subroutines.

Here is the programming text -




This is how it works -
Variable declaration:
I think its always best to declair variables (and constants) at the beggining of the program.

Initializing:
This is the section where things have to be done once at the beggining of the program.

Get_RC_Time:
This takes the RC time measurement on the photoresister circuit.  This subroutine has a PAUSE command that charges up the capacitor and the time period to charge up the capacitor is very small.  The RCTIME command sets the value of the time variable. This value is then used by the second subroutine.

Delay:
All this subroutine does is contain a PAUSE.

Update_Display:
The LOOKUP command in this subroutine contains a table with six bit patterns that are used to create the circular pattern on the display.  By adding one to the index variable each time the subroutine is called, it causes the next bit patern in the sequence to get placed in OUTH.


I could change the speed of the light spinning by either changing the programmin or replacing the capacitor with a smaller one.

Friday, 16 October 2009

Hey guys you got to see this:

http://www.botanicalls.com/archived_kits/twitter/

Absolutely Genius.

Monday, 12 October 2009

Photoresistor Basic Plot Lite data plotting.

Hi everyone!
I just finished with the 'Measuring Light' chapter from my microcontroller book.  This chapter was really great, I really enjoyed experimenting with the sensor and mostly liked building the Light Meter.  Sounds cool right?  But before I post up anything about the Light Meter, I'm going to show you some of the graphs that my Basic Plot Lite plotted for me when varying the light intensity on the photoresister. (Yes I finally got it all to work). So here are some graphs -


First of all, here's the coding I used to run and plot the data. (This gives me an idea - from now on instead of pasting the programming I'll print screen).


Here's a test graph - just messing around with the light.


I have gradually decreased the light intensity falling on the photoresister, meaning I'm increasing the resistance - increasing the RC-time. Then I'm slowly increasing the light intensity, and then quickly moving my hand on and off the photoresister (thats why I got those sharp peaks).


Here's another graph.

On the next post I'll post up all the other projects I did with the photoresister.




Basic Plot Lite

In the past couple of days, I have been having some problems with matching the COM (Communication Port) number on the Debug Terminal with the COM number on the Basic Plot Lite program.  The Debug Terminal only gave me an option of COM 10, where Basic Plot Lite only gave me options of COM 1 to COM 4, therefore I could not get the program to plot my data that I have collected with a photoresister using a RC circuit.
So I upgraded the Basic Plot Lite program to see if the newer version gave me more COM options.  Unfortunately, the new version only gave me option to COM 9. 
After messing around with the settings in some hope that my options would be in a wider range, Mark (my brother) gave me this advice to simply change the USB port that I'm connecting the serial cable in.  With exitement, after having to download all the programs on a different computer I finally managed to get COM 4, allowing me to now finally use the Basic Plot Lite program and visually see my data on a graph.

Saturday, 10 October 2009

Radio controlled car

Hi,  I took apart the old radio controlled car and the circuit does not look to complicated.  It has one chip - TOSHIBA 8752U
T 8 1 4 0
M*SIA
Looks like it has loads of capacitors and tronsistors and some sort of a electromagnet.  Im gonna check out, and try and get some information on the chip and see if it is programmable.





Friday, 9 October 2009

Amaising LED flashlight

Check this awesome flashlight out! -

7-segment LED display projects

I completed the Digital Display chapter. I had so much fun in this chapter and I would like to share some of the projects that I did.
First i'll give you some information and facts about 7-segment LED displays.
A 7–segment display is a rectangular block of 7 lines of equal length that can be lit selectively to display digits and some letters.
Here are two pictures of how to work the display and the schematic for the 7-segment.


Pin numbers.



Schematic drawing for 7-segment LED display.


 The Common Cathode means that all the cathodes (negative connection) are connected together (they share a common connection point).

Some interesting facts –
- The 7-segment LED display is called a parallel device because you have to use more than one I/O line at a time to send data to the device.
- The wires that transmit the HIGH/LOW signals from the BASIC Stamp to the 7-segment LED display are called a parallel bus.

I’m going to show you now how to display some digits:
- There are 8 different BASIC Stamp I/O pins that send high/low signals to the 7-segment LED display

REMINDER:
8 bits: A binary number that has 8 digits is said to have 8 bits. Each bit is a slot where you can store either a 1 or a 0.
A byte is a variable that contains 8 bits.

Special Variables:
DIRH – A variable that controls the direction (input or output) of I/O pins P8 through P15.
OUTH – Controls the high or low signals that each I/O pin sends.

Here is a program that displays the digits 0 through 9 on the display -






 Schematic diagram for the 7-segment LED display.
The video –



How it works:
OUTH = 000000 initially turns off all the LED’s. (8 zero’s because there are 8 sets of I/O pins; P8 through P15)
DIRH = %11111111 sets all I/O pins P8 through P15 to outputs.

*The % is used to tell the BASIC Stamp Editor that the number is a binary number. Example: The binary number 001100 is the same as the decimal number 12.*
*All BASIC Stamp I/O pins start out as inputs.* - This is called a “default”. You have to tell a BASIC Stamp I/O pin to become an output before it starts sending a high or low signal.

I then spelt out A through F –



A through F video –



After I learned how to display numbers and letters and decided to experiment and spell something out of my own –




I spelt out ‘Richard is cool’. Here is the video –

Watch the video –


Notice I added a full stop at the end and did a blink at the O’s, I did those by my self.
I then learned how to make things easier with the LOOKUP command, that let’s you ‘look up’ elements in a list.

Example –
LOOKUP index, [ 7, 85, 19, 167, 28], value

2 variables – index and value.
If the index is 0, value stores the 7.
If index is 1, value stores 85, and so on.

I was then introduced to the LOOKDOWN command that gives you an index based on a number (the opposite to LOOKUP).

So in the previous programs instead of writing a whole huge list one by one what I want to spell out, I can just write a list of the numbers or letters that I’m going to be using, and then write in square brackets what I want to say, and the LOOKUP command will see in order what letter I want to spell, lets say A, so it goes and looks for the letter A in the list, reads the binary code and flashes it out. This saves time and prevents mistakes to be done in writing the binary code over and over. You will see this in the FISH CHIP And dIP example.

In this program I spelt out FISH CHIPS And dIP using the LOOKUP command. -




Here’s the Video –

 

Oh yeah, here’s a picture of my work station :). Notice the awesome Norway picture on the right guy’s! And I got a small shelf for my components, it’s under the speaker and it cost only £1.25 from the charity shop, it smells a bit like electricity ;) – I just have to put labels and start using it.



My work station.

Enjoy!





Thursday, 8 October 2009

First video upload

Hey Richard, Loved your last blog post - we're all looking forward to the videos!  The easiest way to include a video in you blog post is to first upload it onto youtube (you know how to do that right - should be pretty simple...)


Then you go on the You tube page. Look in the area that the arrow in the image below is pointing at.




On the very bottom of that area there is the word "Embed" followed by a string of html code. Copy this code and paste it into the place in your blogg you want your video to go. Thats it! HEre look - I tried it with the video you sent us about the "sixth sense":



BTW really liked the video!  Really glad to see you are watching TED lectures.  Loads more can be found on the official website: www.ted.com (usually they are up on youtube as well).  Hey, how about the next time you see a cool vid - find it on youtube and embed it here for all of us to comment on it (including your own comments as well)...!?

First Programs Published

Hi, here are a few projects that I programmed using the BASIC Stamp Homework Board (PARALLAX).
The first couple projects are going to involve a Servo (Parallax Standard Servo). I’m going to show you how it works and how to program it.

A Servo is controlled by very brief high signals. They are sent every 20 ms.
The PULSOUT command can be used to send a pulse.
A command syntax for the PULSOUT command –

        PULSOUT Pin, Duration.

- The Pin argument is a number that tells the BASIC Stamp which I/O the signal is sent out.

- The Duration argument is not in milliseconds. The Duration is a number of 2-millionth-of-a-second (µs) time durations that you want the high signal to last.

To make the servo rotate properly I had to convert from milliseconds to a Duration that I can use for the PULSOUT.
Duration = number of ms x 500

Duration of a mystery PULSOUT –
Number of ms = Duration / 500 ms

PULSOUT Duration – values between 500 and 1000
Number of pulses - values between 1 and 65534


I did some projects that involved entering the PULSOUT Duration value and the Number of pulses on the Debug Terminal, which read the values and sent pulses to the verto and moved it for a very brief moment like a pulse.
Because the verto has certain limits, it is important to give the PULSOUT Duration and the pulses in the given range or the verto can be damaged. So I added additional lines in my programming so that if a value was given outside its range, a message would show up on the Debug Terminal that the value is too high or too low. I thought this was pretty cool.

Here is the programming text to control the limits -




I wrote a program that would control how fast the servo in rotating.
You can use a FOR…NEXT loop to make a servo sweep through its range of motion like this:


    FOR counter = 500 TO 1000
    PULSOUT 14, counter
    PAUSE 20
    NEXT

FOR…NEXT loops have an option STEP argument. The STEP argument can be used to make the servo rotate faster. For example, you can use the STEP argument to add 8 to counter each time through the loop (instead of 1) by modifying the FOR statement like this:

       FOR counter = 500 TO 1000 STEP 8

I made the servo rotate in the opposite direction by counting down. I made the StartValue argument smaller than the EndValue argument:

       FOR counter = 1000 TO 500 STEP 8

I then made it rotate faster by increasing the STEP argument:

       FOR counter = 500 TO 1000 STEP 20


After having fun with all of that I made the servo rotate using a potentiometer. It’s kind of like a wheel of a micro controlled car. When I rotated the variable resister clockwise, the verto turned clockwise and when I turned the variable resister anticlockwise the verto turned anticlockwise.

Programming for ControlServoWithPot:




This was pretty complicated. First of all I had to see what the smallest and largest value of the time variable was for my pot. I did this by connecting it with a capacitor and a resistor. As I turned the pot, the Debug Terminal told me what the time variable was when my pot was fully turned clockwise: (1)
and fully turned anticlockwise: (623)

Now that I got these values, I had to adjust them so they match to the values of 500 and 1000 that are needed to control the servo with the PULSOUT command. To do this I had to use multiplication and addition. I had to multiply the input values (1 and 623) by something to make the difference between the clockwise and anticlockwise values 500 instead of 622.

Here’s the math work for the multiplication (scaling):
Time (max) = 623 x (500/623) = 500
Time (min) = 1 x (500/623) = 0.803

After these values were scaled, I had to do the offset step:
Time (max) = 500 + 500 = 1000
Time (min) = 0.803 + 500 = 500

The */ operator is built into PBASIC for scaling by fractional values, like 0.803. Here are the steps for using */ applied to 0.803 –
1. Place the value or variable you want to multiply by a fractional value before the */ operator.

        time = time */

2. Take the fractional value that you want to use and multiply it by 256 (I HAVE NO CLUE WHY THAT SPECIFIC NUMBER HAS TO BE MULTIPLIED BY THE FRACTION)

        new fraction value = 0.803 x 256 = 205.568

3. Round off to get rid of anything to the right of the decimal point.

        New fraction value = 205

4. Place that value after the */ operator.

        time = time */ 205

Now that the scaling is done I just I just have to add the offset of 500.

        time = time */ 205
        time = time + 500

Now, time is ready to be recycled into the PULSOUT command’s Duration argument.

        time = time */ 205
        time = time + 500
        PULSOUT 14, time

Video of ControleServoWithPot:



To make things more interesting I added the Bi-colour LED circuit to it. When I turned the pot (variable resister/ potentiometer) clockwise the LED shined red and green when the pot was turned the other way.

Video of ControlServoWithPotBiColourLed:

Tuesday, 6 October 2009

Control a verto with a potentiometer

Hey guy's!
I created this really cool circuit and programmed it - I created a "Microcontrolled verto": Basically its two circuits put together - the verto circuit and the potentiometer circuit. When you twist the potentiometer a bit clockwise the verto moves the same distance (degrees) clockwise..just like a microcontroller switch for a car. What do you think? (I'll put up some video's another day)
After I did that, I added an additional circuit - the Bi-colour LED cicuit: so when I twist the variable resister (potentiometer) slightly clockwise the LED is red, if twisted counterclockwise the LED is green! Im going to post some videos of that soon aswell. And all the explanations.

Now i'm going to be working on the digital display (I already memorised the pins and positions of the display :D )

Tell me what you think and hopefully see you guy's soon!

Friday, 2 October 2009

Remember...

Remember Richard:  Plan - Do - Review - Plan - Do - Review - Plan - Do - Review - ...

Make sure you exercise that simple rule and you will make sure you make the best of your gap year.  You can implement this cycle on a monthly, weekly and daily basis ( I even sometimes do it every couple of hours)

Even if you only spend 10min each day to sit down and plan what you want to acheive, and then review it that evening. For example:

Day 1:
PLAN:
Today I will do the following:
  1. Clean up my work area
  2. Build compnent three from page 47 of the Robot Bonanza book
  3. program that timer that I thought of last night.  The rotating arm could really use that.
(Save the post and go off and DO what you said you would.  In the evening come back to the post and edit it:)

REVIEW:

Managed to get #1 ok, took a little longer than I expected but its so shiny!  Had trouble with getting the circuit to work for #2  I don't know enough about potentiometers at the moment...  Got the first half of the program for the timer done.  Looking snazzy.

That way when you plan for the next day you can look at the review and plan to learn about potentiometers and finish the timer program.

Remember also to keep your goals for each day SMART - Specific, Measurable, Acheivable, Realistic and Timed.  You can ask Dad more about that.  So to make your Goals realistic and achaivable, its probably best not to have too many.  Set yourself a time limit (3hrs or 20min, by lunchtime etc) and make sure there is a way to tell you've acheived it (so measure how many pages you want have read etc) .

Looking forward to seeing how you get on!
What are you going to put up here Richard?
Hi Richard Cool blog.  Can't wait to see what goes up here...

this is my first super awesome rad post

i like eating apples