wait(milliseconds)

Wait


Syntax: wait( milliseconds )
Description: Waits a given amount of time.
Parameters: milliseconds An integer number representing the delay in milliseconds. The sign of the number is ignored. A negative number results in a delay of the same value as the positive number. The wait time assumes that the default 1MHz clock is running. The maximum wait value is 32767 (32 seconds)
Returns: None
Related: halt()


 

Example:

pindir(P1_0,OUT)               # Set port P1_0 as an output (this is the Launchpad red led)
while 1:                       # do forever
   out(P1_0, 0)                # set the output pin P1_0 (red led) to a 0 (off)
   wait(500)                   # Wait for 500ms  (half a second)
   out(P1_0, 1)
   wait(200)                   # Wait for 200 is the same as 0.2 second

Note the maximum value for a wait time is just over 32 seconds. This is because the maximum integer value is 32767. If a wait time of greater then 32 seconds is required then place the wait command in a for loop or a while loop.

Example:

pindir(P1_0,OUT)               # Set port P1_0 as an output (this is the Launchpad red led)
while 1:                       # do forever
   out(P1_0, 0)                # set the output pin P1_0 (red led) to a 0 (off)
   for i in range(60):         # loop 60 times, therefore this results in a 1 minute wait time
       wait(1000)              # Wait for 1000ms (a second) each time round the loop
   out(P1_0, 1)
   for i in range(60):
       wait(1000)

A negative wait value is the same wait time as the positive value.

Example:

pindir(P1_0,OUT)               # Set port P1_0 as an output (this is the Launchpad red led)
r = 0
while 1:                       # do forever
   r = random( r )             # get a random number between -32768 and +32767
   out(P1_0, 0)                # set the output pin P1_0 (red led) to a 0 (off)
   wait(r)                     # the wait value r can be poisive or negative value
   out(P1_0, 1)
   wait(r)

This example will flash the red led however the wait delay is a random number between -32768 and +32767 , any negative random numbers are treated the same as a positive  number, and results in a positive delay.

The actual time that the wait command waits depends on the current clock speed. The normal clock rate for a mpy program is 1MHz. This results in a wait time in milliseconds which is equal the value. If the system clock SCLK is different to this value the wait time will be scaled by the different SCLK frequency ratio. See system clock .

Also the wait command enters a software loop while waiting. It does not use a Timer block nor does it enter a low power mode while waiting.