Shop OBEX P1 Docs P2 Docs Learn Events
Trouble understanding the "count" function. — Parallax Forums

Trouble understanding the "count" function.

brupbrup Posts: 14
edited 2009-06-03 12:56 in BASIC Stamp
confused.gif I have a bs2. I have a program that blinks a led on and off. I need the led to blink 10 times and then end the program. Any ideas on how to do this? Do I use a count command? If so how do I do that?

Comments

  • BeanBean Posts: 8,129
    edited 2009-06-02 14:42
    Brup,
    The COUNT command is used to count INCOMING pulses.
    I would use a FOR...NEXT loop

    FOR temp = 1 TO 10
    ' Your blink code
    NEXT

    Of course you will have to declare the variable "temp".

    Bean.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    There is a fine line between arrogance and confidence. Make sure you don't cross it...

    ·
  • Mike GreenMike Green Posts: 23,101
    edited 2009-06-02 14:43
    The COUNT statement is used to count external pulses. It stops your program for the amount of time specified in the COUNT statement, counts how many pulses occur on an I/O pin you specify during that time, and leaves the count in the variable you specify, then your program continues.

    For your particular needs, look at the FOR / NEXT statement chapters in the Stamp Basic Syntax and Reference Manual. You can download this from Parallax if you don't have it already. There are also examples in the "What's a Microcontroller?" tutorial also downloadable from Parallax.

    Go to the main Parallax webpage and click on the Resources tab, then click on Downloads, then Stamps in Class Downloads.
  • GiuseppeGiuseppe Posts: 51
    edited 2009-06-02 14:55
    If you just want the LED to blink on and off, just use a for next loop

    counter VAR   Byte      ' counter variable to hold what count number you are on
    led     PIN   15            ' one leg of LED connected to this pin with proper resistance in series
    
    FOR counter = 1 TO 10   ' counter starts off as 1
    
      HIGH led                     ' whatever pin you wish to change state
      PAUSE 500                  ' half second pause
      LOW led                      ' same pin you wish to change state
      PAUSE 500                  ' half second pause
    
    NEXT                         ' adds one to counter and returns to FOR again if counter < 10
                                    ' when counter = 10 then the loop breaks and goes to next piece of code or just put END to stop the program
    
    



    To change how any times it blinks just change the 10 to whatever number of blinks you want. To change duration, change the pause duration. Hope this helps.
  • brupbrup Posts: 14
    edited 2009-06-02 18:56
    roll.gif Thanks that was great help. But I want to control 4 relays for a certain time and at the end a tone
    will sound.I have the program and the program for tone working but I cannot get it to step to the next relay
    any ideas? I want to use the same sound for each relay. How do i get it to second relay after sound.
    (next,return)?

    relay 1
    sound
    relay 2
    sound
    relay 3
    sound
    relay 4
  • Mike GreenMike Green Posts: 23,101
    edited 2009-06-02 19:43
    You want to make "sound" into a subroutine. Then you can call it with GOSUB each place you need it. Read the chapter on the GOSUB and RETURN statements in the Stamp Manual.
  • brupbrup Posts: 14
    edited 2009-06-02 21:06
    I did read the info can I use a sub if the sound program·has a sub in it's self?
  • jccjcc Posts: 4
    edited 2009-06-03 00:58
    Yes!

    GOSUB relay1 'contains Relay ON, Time, Relay OFF code, ends with RETURN
    GOSUB sound 'contains code for tone including nested sub, end with RETURN
    PAUSE xxx 'optional pause before relay2
    GOSUB relay2
    GOSUB sound
    PAUSE
    GOSUB relay3 .....

    a more efficient way would be to have one generic Relay subroutine to which you would pass parameters indicating which output you wish to control and how much time.
  • brupbrup Posts: 14
    edited 2009-06-03 11:20
    lol.gif Great info thanks. Did that. How can the stamp be paused for 15 minutes 900ms. It resets
    after 255 so it never reaches 900ms.
  • GiuseppeGiuseppe Posts: 51
    edited 2009-06-03 12:56
    I guess you could also use a for next loop for higher time

    
    counter    VAR      Byte  'holds the counter
    
            FOR counter = 1 TO 15     'cycles 15 times
              PAUSE 60000             '60,000 milliseconds = 60 seconds = 1 minute
            NEXT                      'when counter = 15, loop breaks
    
            'total duration of time will be 900,000 milliseconds which is 15 minutes
    
    
    



    The pause function is limited but I think this for... next loop can just cycle what the pause function is capable of and achieve your desired pause. Hope this helps!
Sign In or Register to comment.