Thursday, 8 October 2009

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:

3 comments:

  1. Sorry for the ugly layout, i'll try and fix that up later.
    And Mark and Tom, I dont know how to add a video that I have made personally.

    ReplyDelete
  2. Hey man, I blog posted the answer to that question. I have few comments/questions of my own:

    You wrote: "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."

    --I think thats pretty cool too man! Good system protection there...


    Also I see that the STEP argument can actually be used to control the acceleration of the servo - pretty neat.


    A Question: you know the time variable of your "pot". Is that the time it takes to charge discharge the RC circuit?

    ReplyDelete
  3. Yep thats right. I found that fart a bit confusing, but understand that initially i had to build a circuit with a pot, capacitor and resister, and the Debug Terminal told me what the RC time was when the pot was fully turned clockwise and anticlockwise.

    ReplyDelete