Explanation:
This is an example of how to define a subroutine.
When called, this subroutine will blink an LED for the number of times and duration determined by the input parameters.
The syntax of the subroutine is:
' Flash_LED (numtimes, OnTime, (optional) OffTime)
' Where numtimes is from 1 - 255 and OnTime/OffTime is
' from 0 - 65535 ms. If OffTime is not entered, then
' OffTime = OnTime.
Sub Flash_LED (in numtimes, in OnTime as WORD, Optional OffTime as WORD = OnTime)
repeat numtimes
set LED on
wait OnTime ms
set LED OFF
wait OffTime ms
end repeat
End SubShown below is a working example program using a Microchip PIC 18F25K22.
Change settings/ports as needed for other chips.
Connect an LED to the LED pin via a 1K series resistor.
Demonstration program:
#chip 18F25K22, 16
#define LED PORTC.1 'Led on PIN 14 via 1K resistor
DIR LED OUT
wait 1 sec
;======== MAIN PROGRAM LOOP ================
Do
Flash_LED ( 3,250 ) '3 Flashes 250 ms equal on/off time
Wait 2 Sec
Flash_LED ( 5,250,500 ) '5 flashes On 250 ms / off 500 ms
Wait 2 Sec
Flash_LED ( 10,100 ) '10 rapid flashes ' <<< calling the sub with only the required parameters
Wait 2 Sec
Loop
;==========================================
Sub Flash_LED (in numtimes, in OnTime as WORD, optional OffTime as word = OnTime)
repeat numtimes
set LED on
wait OnTime ms
set LED OFF
wait OffTime ms
end repeat
End SubKey line: Flash_LED ( 10,100 ) — since OffTime is declared Optional … = OnTime, omitting it here makes the off duration default to the same value as OnTime (100 ms), giving 10 flashes with equal 100 ms on/off times.
See Also:
- Subroutines — Optional parameters and default values in full detail
- Repeat — the loop construct used above

