'=========================================================================== ' Subject: NUMERIC LED CONTROL EXAMPLES Date: 12-17-98 (17:52) ' Author: David Szafranski Code: LB ' Origin: digital_paris@csi.com Packet: LIBERTY.ABC '=========================================================================== ' (4) Numeric LED control examples. ' This demonstration shows using the LED for a clock, countdown, date, and an input device ' where the LEDs can be changed by left or right mouse click or by keyboard input into ' a textbox. Feel free to use/copy/modify in any way! ' David Szafranski November 1998 digital_paris@csi.com nomainwin WindowWidth = 390 WindowHeight = 250 UpperLeftX = int((DisplayWidth - WindowWidth) / 2) UpperLeftY = int((DisplayHeight - WindowHeight) / 2) button #main.button1, "Start Clock", [clock], UL, 223, 20, 150, 30 button #main.button2, "Start Countdown", [countdown], UL, 223, 70, 150, 30 button #main.button3, "Set LED", [setLED], UL, 275, 120, 98, 30 button #main.button4, "Get Date", [date], UL, 223, 170, 150, 30 textbox #main.textbox1, 223, 125, 40, 22 graphicbox #main.led, 5, 5, 213, 210 open "LED Examples" for window as #main print #main, "trapclose [quit]" print #main.led, "fill black; down" print #main.led, "when rightButtonDown [main.led.RBD]" 'detect button click for changing LEDs down one print #main.led, "when leftButtonDown [main.led.LBD]" 'detect button click for changing LEDs up one ' Note: LEDs are spaced at 25 pix and are each 37 pix high x 20 pix wide spacing = 25 'spacing offset for each LED ' numeralNo = 1 'LED number if more than one is used ' xoffset = 10+(spacing*(numeralNo-1)) ''x offset in graphic box ' yoffset = 10 'y offset in graphic box ' print 3 LEDs for using as numeric input device numeralNo = 1 'LED number xoffset = 10+(spacing*(numeralNo-1)) 'x placement in graphicbox yoffset = 110 'y placement in graphicbox num$ = "0" 'string which contains numeric digit 0-9 gosub [drawLED] 'sub which draws the LED numeralNo = 2 xoffset = 10+(spacing*(numeralNo-1)) yoffset = 110 num$ = "0" gosub [drawLED] numeralNo = 3 xoffset = 10+(spacing*(numeralNo-1)) yoffset = 110 num$ = "0" gosub [drawLED] [mainLoop] ' input aVar$ scan time$ = time$() hour$ = left$(time$,2) minutes$ = mid$(time$, 4, 2) seconds$ = right$(time$, 2) if countdownFlag = 1 then if seconds$ <> oldSeconds$ then gosub [changeCountdown] end if if clockFlag = 1 then ' if time has changed, then redraw the time LED if hour$ <> oldHour$ then gosub [changeHour] if minutes$ <> oldMinutes$ then gosub [changeMins] if seconds$ <> oldSeconds$ then gosub [changeSecs] end if oldHour$ = hour$ oldMinutes$ = minutes$ oldSeconds$ = seconds$ goto [mainLoop] [clock] clockFlag = 1 time$ = time$() 'get time hour$ = left$(time$,2) 'get hour minutes$ = mid$(time$, 4, 2) 'get minutes seconds$ = right$(time$, 2) 'get seconds gosub [changeHour] 'draw LED time for first time gosub [changeMins] gosub [changeSecs] print #main.led, "color red; backcolor red" print #main.led, "place 65 20" print #main.led, "boxfilled 70 25" print #main.led, "place 65 35" print #main.led, "boxfilled 70 40" print #main.led, "place 140 20" print #main.led, "boxfilled 145 25" print #main.led, "place 140 35" print #main.led, "boxfilled 145 40" oldHour$ = hour$ 'keep track of change in time variables oldMinutes$ = minutes$ oldSeconds$ = seconds$ goto [mainLoop] [countdown] countdownFlag = 1 'this example uses the clock for a countdown but other methods could be used to change countdown value count = 99 'initiate countdown variable goto [mainLoop] [main.led.RBD] if MouseX < 10 or MouseX > 85 then goto [mainLoop] 'reject clicks outside area of LED if MouseY < 110 or MouseY > 147 then goto [mainLoop] numeralNo = int((MouseX-10)/spacing)+1 'determine which LED was clicked ClickCount(numeralNo) = ClickCount(numeralNo)-1 'keep track of LED counters if ClickCount(numeralNo) < 0 then ClickCount(numeralNo) = 9 'start over from 9 xoffset = 10+(spacing*(numeralNo-1)) yoffset = 110 num$ = str$(ClickCount(numeralNo)) gosub [eraseNum] gosub [drawLED] ledOut$ = str$(ClickCount(1))+str$(ClickCount(2))+str$(ClickCount(3)) 'get final output number print #main.textbox1, ledOut$ goto [mainLoop] [main.led.LBD] if MouseX < 10 or MouseX > 85 then goto [mainLoop] 'reject clicks outside area of LED if MouseY < 110 or MouseY > 147 then goto [mainLoop] numeralNo = int((MouseX-10)/spacing)+1 'determine which LED was clicked ClickCount(numeralNo) = ClickCount(numeralNo)+1 'keep track of LED counters if ClickCount(numeralNo) > 9 then ClickCount(numeralNo) = 0 'start over from 0 xoffset = 10+(spacing*(numeralNo-1)) yoffset = 110 num$ = str$(ClickCount(numeralNo)) gosub [eraseNum] gosub [drawLED] ledOut$ = str$(ClickCount(1))+str$(ClickCount(2))+str$(ClickCount(3)) print #main.textbox1, ledOut$ goto [mainLoop] [setLED] print #main.textbox1, "!contents?" input #main.textbox1, ledIn$ ledIn$ = trim$(ledIn$) ok = 0 'flag to check if any non numeric characters have been entered for i = 1 to len(ledIn$) test$ = mid$(ledIn$, i, 1) if asc(test$) < 48 or asc(test$) > 57 then ok = 1 'check for any non numeric characters next i if ok = 1 then print #main.textbox1, "" : goto [mainLoop] if len(ledIn$) > 3 then ledIn$ = left$(ledIn$,3) if len(ledIn$) = 1 then ledIn$ = "00" +ledIn$ 'add leading zeros if len(ledIn$) = 2 then ledIn$ = "0" + ledIn$ for i = 1 to len(ledIn$) numeralNo = i num$ = mid$(ledIn$, i, 1) xoffset = 10+(spacing*(numeralNo-1)) yoffset = 110 gosub [eraseNum] gosub [drawLED] ClickCount(i) = val(num$) 'reset LED counters to new values next i goto [mainLoop] [date] d$ = date$( ) month$=left$(d$,3) if month$="Jan" then month$="01" if month$="Feb" then month$="02" if month$="Mar" then month$="03" if month$="Apr" then month$="04" if month$="May" then month$="05" if month$="Jun" then month$="06" if month$="Jul" then month$="07" if month$="Aug" then month$="08" if month$="Sep" then month$="09" if month$="Oct" then month$="10" if month$="Nov" then month$="11" if month$="Dec" then month$="12" year$=right$(d$,2) day$=left$(right$(d$,8),2) numeralNo = 1 xoffset = 10+(spacing*(numeralNo-1)) yoffset = 160 num$ = left$(month$,1) gosub [drawLED] numeralNo = 2 xoffset = 10+(spacing*(numeralNo-1)) yoffset = 160 num$ = right$(month$,1) gosub [drawLED] numeralNo = 3 xoffset = 10+(spacing*(numeralNo-1)) yoffset = 160 num$ = "-" gosub [drawLED] numeralNo = 4 xoffset = 10+(spacing*(numeralNo-1)) yoffset = 160 num$ = left$(day$,1) if num$ = " " then num$ = "0" gosub [drawLED] numeralNo = 5 xoffset = 10+(spacing*(numeralNo-1)) yoffset = 160 num$ = right$(day$,1) gosub [drawLED] numeralNo = 6 xoffset = 10+(spacing*(numeralNo-1)) yoffset = 160 num$ = "-" gosub [drawLED] numeralNo = 7 xoffset = 10+(spacing*(numeralNo-1)) yoffset = 160 num$ = left$(year$,1) gosub [drawLED] numeralNo = 8 xoffset = 10+(spacing*(numeralNo-1)) yoffset = 160 num$ = right$(year$,1) gosub [drawLED] goto [mainLoop] [quit] close #main end [changeHour] number$ = hour$ gosub [getNumbers] numeralNo = 1 xoffset = 10+(spacing*(numeralNo-1)) yoffset = 10 'y offset in graphic box num$ = num1$ if num1$ <> oldhour1$ then gosub [eraseNum] gosub [drawLED] end if oldhour1$ = num1$ numeralNo = 2 xoffset = 10+(spacing*(numeralNo-1)) yoffset = 10 'y offset in graphic box num$ = num2$ gosub [eraseNum] gosub [drawLED] return [changeMins] number$ = minutes$ gosub [getNumbers] numeralNo = 4 xoffset = 10+(spacing*(numeralNo-1)) yoffset = 10 'y offset in graphic box num$ = num1$ if num1$ <> oldmin1$ then gosub [eraseNum] gosub [drawLED] end if oldmin1$ = num1$ numeralNo = 5 xoffset = 10+(spacing*(numeralNo-1)) yoffset = 10 'y offset in graphic box num$ = num2$ gosub [eraseNum] gosub [drawLED] return [changeSecs] number$ = seconds$ gosub [getNumbers] numeralNo = 7 xoffset = 10+(spacing*(numeralNo-1)) yoffset = 10 'y offset in graphic box num$ = num1$ if num1$ <> oldsec1$ then gosub [eraseNum] gosub [drawLED] end if oldsec1$ = num1$ numeralNo = 8 xoffset = 10+(spacing*(numeralNo-1)) yoffset = 10 'y offset in graphic box num$ = num2$ gosub [eraseNum] gosub [drawLED] return [changeCountdown] count$ = str$(count) if len(count$) < 2 then count$ = "0" + count$ 'add 0 for single digit numbers number$ = count$ gosub [getNumbers] numeralNo = 1 xoffset = 10+(spacing*(numeralNo-1)) yoffset = 60 num$ = num1$ if num1$ <> oldcount1$ then gosub [eraseNum] gosub [drawLED] end if oldcount1$ = num1$ numeralNo = 2 xoffset = 10+(spacing*(numeralNo-1)) yoffset = 60 num$ = num2$ gosub [eraseNum] gosub [drawLED] count = count - 1 if count < 0 then countdownFlag = 0 'stop countdown return [getNumbers] 'find two digit LED , change this for using more LED numbers num1$ = left$(number$,1) num2$ = right$(number$,1) return '=================== Critical LED functions ================================= ' Required Inputs: ' numeralNo = 1 'LED number ' xoffset = 10+(spacing*(numeralNo-1)) 'x placement in graphicbox ' yoffset = 110 'y placement in graphicbox ' num$ = "0" 'string which contains numeric digit 0-9 [drawLED] if num$ = "0" then gosub [zero] if num$ = "1" then gosub [one] if num$ = "2" then gosub [two] if num$ = "3" then gosub [three] if num$ = "4" then gosub [four] if num$ = "5" then gosub [five] if num$ = "6" then gosub [six] if num$ = "7" then gosub [seven] if num$ = "8" then gosub [eight] if num$ = "9" then gosub [nine] if num$ = "-" then gosub [dash] return [eraseNum] 'erase previous number print #main.led, "place "; xoffset-2; " "; yoffset print #main.led, "color black; backcolor black; boxfilled "; xoffset-2 + 21; " "; yoffset+37 return ' Create 7 LED segments out of lines, need xoffset and yoffset for location in graphic box [seg1] print #main.led, "color red"; print #main.led, "line "; xoffset+1; " "; yoffset; " "; xoffset+13; " "; yoffset print #main.led, "line "; xoffset; " "; yoffset+1; " "; xoffset+14; " "; yoffset+1 print #main.led, "line "; xoffset+1; " "; yoffset+2; " "; xoffset+13; " "; yoffset+2 print #main.led, "line "; xoffset+2; " "; yoffset+3; " "; xoffset+12; " "; yoffset+3 print #main.led, "line "; xoffset+3; " "; yoffset+4; " "; xoffset+11; " "; yoffset+4 return [seg4] print #main.led, "color red"; print #main.led, "line "; xoffset+3; " "; yoffset+32; " "; xoffset+11; " "; yoffset+32 print #main.led, "line "; xoffset+2; " "; yoffset+32+1; " "; xoffset+12; " "; yoffset+32+1 print #main.led, "line "; xoffset+1; " "; yoffset+32+2; " "; xoffset+13; " "; yoffset+32+2 print #main.led, "line "; xoffset; " "; yoffset+32+3; " "; xoffset+14; " "; yoffset+32+3 print #main.led, "line "; xoffset+1; " "; yoffset+32+4; " "; xoffset+13; " "; yoffset+32+4 return [seg6] print #main.led, "color red"; print #main.led, "line "; xoffset+3; " "; yoffset+16; " "; xoffset+11; " "; yoffset+16 print #main.led, "line "; xoffset+2; " "; yoffset+16+1; " "; xoffset+12; " "; yoffset+16+1 print #main.led, "line "; xoffset+1; " "; yoffset+16+2; " "; xoffset+13; " "; yoffset+16+2 print #main.led, "line "; xoffset+2; " "; yoffset+16+3; " "; xoffset+12; " "; yoffset+16+3 print #main.led, "line "; xoffset+3; " "; yoffset+16+4; " "; xoffset+11; " "; yoffset+16+4 return [seg7] print #main.led, "color red"; print #main.led, "line "; xoffset-2; " "; yoffset+3; " "; xoffset-2; " "; yoffset+16 print #main.led, "line "; xoffset-1; " "; yoffset+2; " "; xoffset-1; " "; yoffset+17 print #main.led, "line "; xoffset; " "; yoffset+3; " "; xoffset; " "; yoffset+18 print #main.led, "line "; xoffset+1; " "; yoffset+4; " "; xoffset+1; " "; yoffset+17 print #main.led, "line "; xoffset+2; " "; yoffset+5; " "; xoffset+2; " "; yoffset+16 return [seg5] print #main.led, "color red"; print #main.led, "line "; xoffset-2; " "; yoffset+6+15; " "; xoffset-2; " "; yoffset+19+15 print #main.led, "line "; xoffset-1; " "; yoffset+5+15; " "; xoffset-1; " "; yoffset+20+15 print #main.led, "line "; xoffset; " "; yoffset+4+15; " "; xoffset; " "; yoffset+19+15 print #main.led, "line "; xoffset+1; " "; yoffset+5+15; " "; xoffset+1; " "; yoffset+18+15 print #main.led, "line "; xoffset+2; " "; yoffset+6+15; " "; xoffset+2; " "; yoffset+17+15 return [seg2] print #main.led, "color red"; print #main.led, "line "; xoffset-2+13; " "; yoffset+5; " "; xoffset-2+13; " "; yoffset+16 print #main.led, "line "; xoffset-1+13; " "; yoffset+4; " "; xoffset-1+13; " "; yoffset+17 print #main.led, "line "; xoffset+13; " "; yoffset+3; " "; xoffset+13; " "; yoffset+18 print #main.led, "line "; xoffset+1+13; " "; yoffset+2; " "; xoffset+1+13; " "; yoffset+17 print #main.led, "line "; xoffset+2+13; " "; yoffset+3; " "; xoffset+2+13; " "; yoffset+16 return [seg3] print #main.led, "color red"; print #main.led, "line "; xoffset-2+13; " "; yoffset+6+15; " "; xoffset-2+13; " "; yoffset+17+15 print #main.led, "line "; xoffset-1+13; " "; yoffset+5+15; " "; xoffset-1+13; " "; yoffset+18+15 print #main.led, "line "; xoffset+13; " "; yoffset+4+15; " "; xoffset+13; " "; yoffset+19+15 print #main.led, "line "; xoffset+1+13; " "; yoffset+5+15; " "; xoffset+1+13; " "; yoffset+20+15 print #main.led, "line "; xoffset+2+13; " "; yoffset+6+15; " "; xoffset+2+13; " "; yoffset+19+15 return ' create numbers from segments [zero] gosub [seg1] gosub [seg2] gosub [seg3] gosub [seg4] gosub [seg5] gosub [seg7] return [one] gosub [seg2] gosub [seg3] return [two] gosub [seg1] gosub [seg2] gosub [seg6] gosub [seg4] gosub [seg5] return [three] gosub [seg1] gosub [seg2] gosub [seg6] gosub [seg4] gosub [seg3] return [four] gosub [seg7] gosub [seg6] gosub [seg3] gosub [seg2] return [five] gosub [seg1] gosub [seg7] gosub [seg6] gosub [seg4] gosub [seg3] return [six] gosub [seg1] gosub [seg7] gosub [seg6] gosub [seg4] gosub [seg5] gosub [seg3] return [seven] gosub [seg1] gosub [seg2] gosub [seg3] return [eight] gosub [seg1] gosub [seg2] gosub [seg3] gosub [seg4] gosub [seg5] gosub [seg6] gosub [seg7] return [nine] gosub [seg1] gosub [seg2] gosub [seg6] gosub [seg7] gosub [seg3] gosub [seg4] return [dash] gosub [seg6] return