The QBasic Forum     RULES     Other Subforums, Links and Downloads

  
--

 Return to Index  

LED binary display to 12 places with counter

July 18 2005 at 5:03 PM
Solitaire  (Login Solitaire1)
S


Response to Clock with binary LED display for the time

 
This program uses a variation of the LED display code from the above binary clock program, expanded to 12 places. It can either display a number the user inputs, or start counting from an input number and watch the display change every 1.5 seconds. An option is included to change the counting speed or freeze the count.

=============================================================================

'Binary LED display by Solitaire
DECLARE SUB ShowVal (num AS INTEGER, bnum AS STRING)
DECLARE SUB Display (bnum AS STRING)
DECLARE FUNCTION DECTOBIN$ (num AS INTEGER)
DIM num AS INTEGER, bnum AS STRING, E AS STRING, C AS STRING
DIM t AS LONG, rate AS SINGLE, press AS INTEGER
CLS
PRINT : PRINT , , "BINARY COUNTER AND LED DISPLAY"
PRINT : PRINT TAB(8); "Type D for a single number display, C for a counter, Esc to end."
PRINT "After D entry, press D again to continue, any other key to clear. After C entry,"
PRINT "press 1-9 to adjust speed, 0 to freeze, any key to continue, Enter to clear."
VIEW PRINT 7 TO 24
DO
    E$ = UCASE$(INPUT$(1))
    CLS
    IF E$ = "D" THEN
        PRINT : PRINT
        INPUT "Enter a positive decimal integer up to 4095:  ", snum$
        num = ABS(VAL(snum$))
        IF num > 4095 THEN num = 4095
        CALL ShowVal(num, bnum$)
    ELSEIF E$ = "C" THEN
        PRINT : PRINT
        INPUT "Enter a starting number or nothing to begin with 0:  ", snum$
        num = ABS(VAL(snum$))
        IF num > 4094 THEN num = 4094
        rate = 1.5
        DO
            C$ = INKEY$
            IF C$ = CHR$(13) THEN EXIT DO
            press = VAL(C$)
            IF press <> 0 THEN
                rate = ((press + 1) * 2) / 6
            END IF
            IF C$ = "0" THEN C$ = INPUT$(1)
CALL ShowVal(num, bnum$)
            t = TIMER
            DO WHILE t + rate >= TIMER: LOOP
            num = num + 1
        LOOP UNTIL C$ = CHR$(13) OR C$ = CHR$(27) OR num = 4095
        CLS
     END IF
LOOP UNTIL E$ = CHR$(27)
VIEW PRINT
SYSTEM

FUNCTION DECTOBIN$ (num AS INTEGER)
DIM m AS INTEGER, x AS INTEGER, bin AS STRING
m = 1
FOR x = 1 TO 12
    IF (num AND m) = 0 THEN
        bin$ = "0" + bin$
    ELSE
        bin$ = "1" + bin$
    END IF
    m = m + m
NEXT x
DECTOBIN$ = bin$
END FUNCTION

SUB Display (bnum AS STRING)
DIM z AS STRING, n AS STRING, dig AS STRING, x AS INTEGER, col AS INTEGER
z$ = CHR$(176)
LOCATE 15
PRINT TAB(6); "2048"; TAB(12); "1024"; TAB(18); "512"; TAB(24); "256";
PRINT TAB(30); "128"; TAB(36); "64"; TAB(42); "32"; TAB(48); "16";
PRINT TAB(54); "8"; TAB(60); "4"; TAB(66); "2"; TAB(72); "1"

PRINT TAB(6); z$; TAB(12); z$; TAB(18); z$; TAB(24); z$;
PRINT TAB(30); z$; TAB(36); z$; TAB(42); z$; TAB(48); z$;
PRINT TAB(54); z$; TAB(60); z$; TAB(66); z$; TAB(72); z$

col = 16
n$ = CHR$(219)
FOR x = 1 TO 12
    dig$ = MID$(bnum$, x, 1)
    IF dig$ = "1" THEN
        SELECT CASE x
            CASE 1
                LOCATE col, 6
            CASE 2
                LOCATE col, 12
            CASE 3
                LOCATE col, 18
            CASE 4
                LOCATE col, 24
            CASE 5
                LOCATE col, 30
            CASE 6
                LOCATE col, 36
            CASE 7
                LOCATE col, 42
            CASE 8
                LOCATE col, 48
            CASE 9
                LOCATE col, 54
            CASE 10
                LOCATE col, 60
            CASE 11
                LOCATE col, 66
            CASE 12
                LOCATE col, 72
        END SELECT
        COLOR 9: PRINT n$
    END IF
NEXT x

COLOR 7
END SUB

SUB ShowVal (num AS INTEGER, bnum AS STRING)
DIM shownum AS STRING
CLS
bnum$ = DECTOBIN$(num)
shownum$ = LEFT$(bnum$, 4) + " " + MID$(bnum$, 5, 4) + " " + RIGHT$(bnum$, 4)
LOCATE 10, 20: PRINT "The binary value of";
COLOR 15: PRINT num;
COLOR 7: PRINT "is  "; shownum$
CALL Display(bnum$)
END SUB



    
This message has been edited by Solitaire1 on Jul 19, 2005 7:41 AM
This message has been edited by Solitaire1 on Jul 18, 2005 5:23 PM
This message has been edited by Solitaire1 on Jul 18, 2005 5:04 PM


 
 Respond to this message   
Responses

 Copyright © 1999-2008 Network54. All rights reserved.   Terms of Use   Privacy Statement