LED Flash Routines

copyright, Peter H. Anderson, Baltimore, MD, Nov, '99

This collection of routines was developed to illustrate some of the basic programming concepts of the BX24. They use no more than the two surface mount LEDs and a wire (pushbutton) to ground on Pin 12.

LEDFL_1 simply turns off the green LED and flashes the red LED. LEDFL_2 extends this to flash either the green or red LED depending on the state of a pushbutton on Pin 12. LEDFL_3 perfroms the same function using a subroutine.

Program LEDFL_4 illustrates how a quantity may be displayed using flashes and LEDFL_5 dials a telephone number (simulated using the red LED) and then sends the quantity using the green LED.

' LEDFL_1.Bas (BX24)
'
' Turns off Green surface mount LED and flashes Red LED.
'
' Illustrates use of "const", "do - loop", "PutPin", "Sleep".
'
' Note that the surface mount LEDs are turned on with a 0.
'
' Copyright, Peter H. Anderson, Baltimore, MD, Nov, '99

Const RedLED as Byte = 25
Const GreenLED as Byte = 26

Sub Main()

Do
Call PutPin(GreenLED, 1)  ' turn the green LED off

Call PutPin(RedLED, 0)	' turn Red LED on
Call Sleep(0.5)
Call PutPin(RedLED, 1)	' turn it off
Call Sleep(0.25)
Loop

End Sub

' LEDFL_2.Bas (BX24)
'
' Flashes Red surface mount LED if pushbutton PB0 on Pin 12 is at ground.
' Otherwise the Green LED is flashed.  Note that the flash rates are
' different.
'
' Illustrates the use of "If Then, Else, End If", configuring a pin as an
' input with an internal pullup
'
' Copyright, Peter H. Anderson, Baltimore, MD, Nov, '99

Const RedLED as Byte = 25
Const GreenLED as Byte = 26
Const PB0 as Byte = 12

Sub Main()

Do
Call PutPin(PB0, 3) 	' make PB0 an input with internal pullup

If (GetPin(PB0)=0) Then	' read the pushbutton
Call PutPin(GreenLED, 1)  ' turn the green LED off

Call PutPin(RedLED, 0)	' turn Red LED on
Call Sleep(0.5)
Call PutPin(RedLED, 1)	' turn it off
Call Sleep(0.25)
Else
Call PutPin(RedLED, 1)	' turn the red LED off

Call PutPin(GreenLED, 0)	' turn green LED on
Call Sleep(0.1)
Call PutPin(GreenLED, 1)	' turn it off
Call Sleep(0.1)
End If
Loop
End Sub

' LEDFL_3.Bas (BX24)
'
' Performs the same function as LEDFL_2.Bas except flashing of the LED
' is implemented in a subroutine.
'
' Note that in calling the sub, parameters are copied, in the same order,
' to the subroutine.  Note that Pin, NumTimes, OnTime and OffTime are
' local variables known only to Sub FlashLED which are dynamically created
' and exist only when the program is executing the subroutine.
'
' Copyright, Peter H. Anderson, Baltimore, MD, Nov, '99

Const RedLED as Byte = 25
Const GreenLED as Byte = 26
Const PB0 as Byte = 12

Sub Main()

Do
Call PutPin(PB0, 3) 	' make PB0 an input with internal pullup

If (GetPin(PB0)=0) Then	' read the pushbutton
Call PutPin(GreenLED, 1)  ' turn the green LED off
Call FlashLED(RedLED, 1, 0.5, 0.25)
' flash Red LED, one time, 0.5 secs on and 0.25 secs off
Else
Call PutPin(RedLED, 1)	' turn the red LED off
Call FlashLED(GreenLED, 1, 0.1, 0.1)
' flash Green LED, one time, 0.1 secs on and 0.1 secs off
End If
Loop
End Sub

Sub FlashLED(ByVal Pin as Byte,  ByVal NumTimes as Integer, _
ByVal OnTime as Single, ByVal OffTime as Single)
Dim N as Integer

For N = 1 to NumTimes
Call PutPin(Pin, 0)
Call Sleep(OnTime)
Call PutPin(Pin, 1)
Call Sleep(OffTime)
Next

End Sub

' LEDFL_4.Bas (BX24)
'
' Assumes the Green LED is a Sonalert or similar noise maker.  Program
' causes the quantity 123 to be sent as "beep" / "beep, beep" /
' "beep, beep, beep".  Note that in the case of a zero; e.g., 101,
' the zero is sent as 10 beeps.
'
' An application might be to send a quantity such as temperature over a
' phone line.
'
' Illustrates use of the MOD operator.  Note that Integer divide is \.
'
' Copyright, Peter H. Anderson, Baltimore, MD, Nov, '99

Const RedLED as Byte = 25
Const GreenLED as Byte = 26
Const PB0 as Byte = 12

Sub Main()

Dim Temperature as Integer, Temp as Integer
Dim Digit as Integer

Temperature = 103		' this might be the result of an A/D conversion
Temp = Temperature
Digit = Temp \ 100	' number of hundreds

If (Digit = 0) Then
Digit = 10
End If

Call FlashLED(GreenLED, Digit, 0.25, 0.25)	' send hundreds
Call Sleep(2.0)	' delay between digits

Temp = Temp MOD 100	' take the remainder
Digit = Temp \ 10		' number of tens

If (Digit = 0) Then
Digit = 10
End If

Call FlashLED(GreenLED, Digit, 0.25, 0.25)
Call Sleep(2.0)	' delay between digits

Digit = Temp MOD 10	' units

If (Digit = 0) Then
Digit = 10
End If

Call FlashLED(GreenLED, Digit, 0.25, 0.25)
Call Sleep(2.0)	' delay between digits

Do		' loop indefinitely
Loop

End Sub

Sub FlashLED(ByVal Pin as Byte,  ByVal NumTimes as Integer, _
ByVal OnTime as Single, ByVal OffTime as Single)
Dim N as Integer

For N = 1 to NumTimes
Call PutPin(Pin, 0)
Call Sleep(OnTime)
Call PutPin(Pin, 1)
Call Sleep(OffTime)
Next

End Sub

' LEDFL_5.Bas (BX24)
'
' Assumes RED LED is dial pulse relay.
'
' When depressed PB0, phone goes off hook and waits 2.0 seconds and assumes
' dial tone is present.  The phone number is then dial pulsed at 50 percent
' break at 10 pulses per second.  On completion, a wait of 2.0 secs.  The
' quantity is then "sent" ten times followed by going back on-hook.
'
' Assumes the Green LED is a Sonalert or similar noise maker.  Program causes
' the quantity 123 to be sent as "beep" / "beep, beep" / "beep, beep, beep".
' Note that in the case of a zero; e.g., 101, the zero is sent as 10 beeps.
'
' An application might be to send a quantity such as temperature over a phone line.
'
' Illustrates use of subroutines, use of arrays, passing arrays by reference
' to a sub or function.
'
' Note that an array is passed to a subroutine ByRef.  That is, the address
' of the first element is passed.
'
' As dial pulsing is one of "Off, On", a new subroutine FlashLEDInv was
' developed.  Note the typecast CInt of Digit.
'
' Copyright, Peter H. Anderson, Baltimore, MD, Nov, '99

Const RedLED as Byte = 25
Const GreenLED as Byte = 26
Const PB0 as Byte = 12

Sub Main()

Dim Temperature as Integer, N as Integer
Dim TelNum(1 to 20) as Byte

TelNum(1) = 1	' intialize the tel number

TelNum(2) = 8
TelNum(3) = 0
TelNum(4) = 0

TelNum(5) = 5
TelNum(6) = 5
TelNum(7) = 5

TelNum(8) = 1
TelNum(9) = 2
TelNum(10) = 1
TelNum(11) = 2

TelNum(12) = &Hf	' meaning there is no more

Temperature = 103	' this might be the result of an A/D conversion

Do		' loop until pushbutton PB0 is depressed
Call PutPin(RedLED, 1)	' be sure on hook
Call PutPin(GreenLED, 1)
Call PutPin(PB0, 3)
Loop Until (GetPin(PB0) = 0)

Call PutPin(RedLED, 0)	' go off hook
Call Sleep(2.0)

Call DialTelNum(TelNum)

Call Sleep(5.0)		' give some time for party to answer phone

For N = 1 To 10		' send the quantity ten times
Call SendQuantity(Temperature)
Call Sleep(2.0)		' pause between sending
Next

Call PutPin(RedLED, 1)	' back on hook
End Sub

Sub DialTelNum(ByRef Tel() as Byte)
Dim N as Integer
Dim Digit as Byte
For N=1 to 20
Digit = Tel(N)
If (Digit = &Hf) Then
Exit For
End If

If (Digit = 0) Then
Digit = 10
End If

Call FlashLEDInv(RedLED, CInt(Digit), 0.05, 0.05)
Call Sleep(1.0)	' one second between digits
Next
End Sub

Sub SendQuantity(ByVal Q as Integer)

Dim Digit as Integer

Digit = Q \ 100	' number of hundreds

If (Digit = 0) Then
Digit = 10
End If

Call FlashLED(GreenLED, Digit, 0.25, 0.25)	' send hundreds
Call Sleep(2.0)	' delay between digits

Q = Q MOD 100	' take the remainder
Digit = Q \ 10		' number of tens

If (Digit = 0) Then
Digit = 10
End If

Call FlashLED(GreenLED, Digit, 0.25, 0.25)
Call Sleep(2.0)	' delay between digits

Digit = Q MOD 10	' units

If (Digit = 0) Then
Digit = 10
End If

Call FlashLED(GreenLED, Digit, 0.25, 0.25)
Call Sleep(2.0)	' delay between digits

End Sub

Sub FlashLED(ByVal Pin as Byte,  ByVal NumTimes as Integer, _
ByVal OnTime as Single, ByVal OffTime as Single)
Dim N as Integer

For N = 1 to NumTimes
Call PutPin(Pin, 0)
Call Sleep(OnTime)
Call PutPin(Pin, 1)
Call Sleep(OffTime)
Next
End Sub

Sub FlashLEDInv(ByVal Pin as Byte,  ByVal NumTimes as Integer, _
ByVal OffTime as Single, ByVal OnTime as Single)
Dim N as Integer

For N = 1 to NumTimes
Call PutPin(Pin, 1)
Call Sleep(OnTime)
Call PutPin(Pin, 0)
Call Sleep(OffTime)
Next
End Sub