Shop OBEX P1 Docs P2 Docs Learn Events
Help writing a program for basic stamp board — Parallax Forums

Help writing a program for basic stamp board

Sda7Sda7 Posts: 8
edited 2013-02-28 17:40 in BASIC Stamp
Hi all
Iam new to this forum and i dont have a good programming skills and i need a help in programming.

I need a program that when getting a signal from (IN3) = (logic 1) the servo turns to 90 degree and hold its position for 3sec then resetting back to 0 degree.

Board type 555-28188


Stamp BS2
PBASIC 2.5

Thats all
Thanks

Comments

  • LeonLeon Posts: 7,620
    edited 2013-02-27 11:11
    What have you done so far?
  • doggiedocdoggiedoc Posts: 2,245
    edited 2013-02-27 11:14
    A really good reference for the beginner is What's A Microcontroller? Student Guide. It has a very detailed table of contents that will get you started.

    I assume this a school assignment and that you have some other reference materials for your particular board as well. Is that true?

    Doc
  • Sda7Sda7 Posts: 8
    edited 2013-02-27 11:18
    I took a code from the book and tryed editing it but i didnt get the result i want :
    I know my program may be full of errors but this what i got so far

    Duration VAR word
    Duration = 0
    PAUSE 10

    DO
    IF IN3 = 1 THEN
    IF Duration < 1150 THEN
    Duration = 350
    ENDIF
    ENDIF
    PULSOUT 14, Duration
    PAUSE 100

    DEBUG HOME, DEC4 duration, " = duration" ( i dono what this line do)
    LOOP
  • Sda7Sda7 Posts: 8
    edited 2013-02-27 11:18
    No. Its not an assignment
  • Mike GreenMike Green Posts: 23,101
    edited 2013-02-27 11:42
    You need to do two things: 1) Download and start going through "What's a Microcontroller?"; 2) Start off with a written statement of what you want your program to do, step by step. If you want to start off with an existing program and modifying it, try the program first with the Stamp Editor and a Stamp and make sure you understand first how the existing program works, then start making modifications one by one and see what they do.

    Parallax folks have spent a lot of time and energy making up tutorials like "What's a Microcontroller?" to help people get started with a Stamp. You'll learn more and better working your way through that and the "Basic Stamp Syntax and Reference Manual" than with folks here duplicating the effort.
  • Sda7Sda7 Posts: 8
    edited 2013-02-27 11:46
    This is what i did , and here where iam now,
    Thats why i asked for help.
  • Mike GreenMike Green Posts: 23,101
    edited 2013-02-27 11:51
    So, what is your program supposed to do? ... I can try to guess, but that's not the best way to do this.
  • Sda7Sda7 Posts: 8
    edited 2013-02-27 11:54
    The program written above is missing the timer i guess, i just need a program that move the servo to 90 deg an dhold that position for 3 sec then resetting the servo to 0 deg.
  • Mike GreenMike Green Posts: 23,101
    edited 2013-02-27 11:59
    So, you need a piece of code that moves a servo to the 90 degree position and another piece of code that moves a servo to the 0 degree position, then you need to modify the first piece to stay in that position for 3 seconds. How would you do those pieces? Use [ code ] tags to show your code like this:

    attachment.php?attachmentid=78421&d=1297987572
  • Sda7Sda7 Posts: 8
    edited 2013-02-27 12:00
    If i know how , i wont be here.
  • Mike GreenMike Green Posts: 23,101
    edited 2013-02-27 12:21
    Chapter #4 of "What's a Microcontroller?" starting on page 93 (for version 3.0 of the tutorial) goes into detail on how a servo motor works and how to drive it from a Stamp. You should be able to figure out how to make the servo move as you've asked based on that. A servo uses a series of pulses for control that are roughly 20ms apart and the width of the pulse indicates the desired servo position. The servo will usually hold its position if the pulses stop, but it can be turned then. If the pulses continue, the servo will maintain its position even if an attempt is made to physically move it to another position. 20 pulses will move the servo and hold it for a total of 20 x 20ms = 400ms (or 0.4 sec). 100 pulses will do it for 100 x 20ms = 2 seconds.
  • Mike GreenMike Green Posts: 23,101
    edited 2013-02-27 12:33
    If you've already worked through all the examples in that section of the tutorial, then maybe you should extract the "meat" of the pieces you want. You need one piece that moves the servo to 90 degrees and holds it for 3 seconds total (including the movement). The text shows you how to come up with that piece. Just take the FOR / NEXT statement part and that's one code piece. Then you need another piece that moves the servo to 0 degrees for long enough to move plus a little bit. You may want to experiment with a real servo to see how long it takes to move through its whole range. You then take these two pieces and put them one after another and surround them with the statements to declare any variables needed and tell the Stamp Editor what kind of Stamp you're using. Look at the examples in the tutorial for this.
  • Mike GreenMike Green Posts: 23,101
    edited 2013-02-27 12:46
    This sort of appeal comes up often. Most of the time it's a school assignment, but not always. We're not here to write code for people. We're here to educate them and help solve problems (not solve them ourselves ... although we do that sometimes when they're difficult even for us). Often that involves pointing them to existing reference material that, as mentioned before, others have spent a lot of time and effort putting together for just this sort of purpose. Sometimes we can point people to other examples or even provide examples sometimes that are not otherwise available, particularly for complex problems or very specific cases. We expect people to take the advice offered and to spend the time and effort to refine their questions so we can try to understand where their stumbling block might be. Sometimes it takes a couple of tries to find out just where the confusion might be.
  • Sda7Sda7 Posts: 8
    edited 2013-02-27 13:00
    Ok , i won't argue about you writing a code, but it wont kill you if i do so , it will save me time.

    If i came empty handed and i said write me a program from scratch that would be lazy.
    But i already have a code and i know how to get signal from (IN3) and move the servo to a certian location, but i dont know how make a timer to hold the positon of the servo then reset it back to its first position.

    Writing the code for this Part of the program wont hurt anyone
  • Mike GreenMike Green Posts: 23,101
    edited 2013-02-27 13:46
    I'm trying to get you to back up a couple of steps. Your code doesn't make any sense based on the description of what you want your program to do, so it's time to throw it out and start from the beginning. Chapter #4 is a very thorough introduction to servos and basic servo movement. Several of the code examples are just what you need for starting points. Your comment about a timer tells me that you don't really understand how servo control works, so maybe going over Chapter #4 again may help. You need to take the two pieces I mentioned for movement to 0 degrees and to 90 degrees, then these need to be put together and the first needs to be lengthened to take 3 seconds, but you take this a step at a time ... first put together, test, then modify. After that, you can make it all conditional on a pushbutton if you want.
  • Sda7Sda7 Posts: 8
    edited 2013-02-28 14:39
    I wrote this code

    The servo start the movement to the desired position when (IN4 = 1)
    But it doesnt move back to the starting point when (IN4 = 0) or after the timer finish the 3 seconds.

    Whats wrong with my code?
    Duration var wrod
    Counter var word
    
    Do 
    If IN4 = 1 then
    For counter = 1 to 132
    Pulsout 14, 650
    Pause 20
    Next
    
    Elseif IN4 = 0
    For counter = 1 to 132
    Pulsout 14,350
    Pause 20
    Next
    
    
    Endif
    Loop
    
    
    
  • Mike GreenMike Green Posts: 23,101
    edited 2013-02-28 16:12
    Your program, simplified, works this way:
    DO
      IF IN4 = 1 THEN
    '  first action - about 2.6 seconds
      ELSEIF IN4 = 0 THEN
    '  second action - about 2.6 seconds
      ENDIF
    LOOP
    

    When IN4 is 1, your program will continually do the first action. When IN4 is 0, your program will continually do the second action. In either case, IN4 will be retested about every 2.6 seconds.

    I suggest you throw out the IF statement for now and experiment with just
    DO
    '  first action
    '  second action
    LOOP
    

    Try inserting DEBUG statements between the above statements and see what happens.
  • playinmyblues_nsccplayinmyblues_nscc Posts: 38
    edited 2013-02-28 17:11
    When learning how to program, unless you have an excellent memory and immediately understand how a program works, then it really does help to write a program and edit it in different ways to find out how things work. I end up writing multiple programs for one project. Some programs work better than others and some provide different results. For each different program I name the program for what it does differently than the others. For my current Basic Stamp 2 project I have 18 different programs. Each one has a slightly different purpose.

    Actually, when working with any program it is a good practice to write the program in small steps. It can be tempting to write everything at once, especially when there is an example program provided. Do your best to avoid this temptation. If will actually save you hours of work over a one week period. For the StampWorks book that I am working through I often write two or three programs for each exercise in the book. I will often write little bits of the program and use DEBUG statements to pop up different values on the screen so I can immediately see what values are being used in the program.

    A good trick to help you to figure out if the correct values are being calculated and used properly in your program is to insert PAUSE 500 or PAUSE some_number instructions that allow you to have a look at the values before the next values are calculated and displayed. Later you can remove the DEBUG and PAUSE statements to speed up your program. While I have not used this method, it is recommended to use it if you use a lot of DEBUG statements:

    Conditional compilation statements is the method. An example appears on page 217 of the StampWorks PDF. It shows how to use an IF statement and a DEBUG statement as a block of code so that if you want the DEBUG's to disappear at run-time, you simply change one number in the program, in one place and they all disappear. If you have a lot of DEBUG's, it really speeds up the program.

    Another good method used with DEBUG statements is to learn how to manipulate the cursor used to write values to the DEBUG terminal so that certain values will simply overwrite the old values. This provides the effect of a running clock or a running count depending on how you format the DEBUG's.

    Good luck and have fun!
  • playinmyblues_nsccplayinmyblues_nscc Posts: 38
    edited 2013-02-28 17:40
    If you want an example of an edited program that uses DEBUG statements and cursor manipulation then you have a look at my post here: http://forums.parallax.com/showthread.php/146305-SW-Exp-14-Scanning-and-Deboucing-Multiple-Inputs

    The StampWorks book is available for download on the Parallax website. Just search for the StampWorks kit and find the download link near the bottom on the page.
Sign In or Register to comment.