cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Have your say in shaping JMP's future by participating in the new JMP Wish List Prioritization Survey
Choose Language Hide Translation Bar
New Year Countdown

Problem

You need a countdown timer for New Year's Eve, and for some reason, you need it built from unlikely display boxes.

Solution

Use a LineUp box to hold a bunch of spacer boxes to form 7-segment LED displays. Control the color of the spacer boxes to make the digits.7-segment display countdown clock7-segment display countdown clock

seglength = 50; // long dimension of one of the 7 segments
segwidth = 3; // narrow dimension
digitspace = 10; // padding around a 7-segment digit, left and right
padding = 40; // padding around the display, top/bot/left/right
on = HLS Color( 0.3, 0.7, 1.0 ); // green
off = HLS Color( 0, 0.2, 0 ); // gray
hseg = Spacer Box( size( seglength, segwidth ), color( on ) ); // a horizontal segment
vseg = Spacer Box( size( segwidth, seglength ), color( on ) ); // a vertical segment
corn = Spacer Box( size( segwidth, segwidth ), color( off ) ); // a corner
cent = Spacer Box( size( seglength, seglength ), color( off ) ); // a center
digit = H List Box( // a complete digit
    Spacer Box( size( digitspace, 1 ) ), // space to left of digit
    // lineup box, 3 wide, holds the H and V segments and other filler bits
    // < H >
    // V ~ V
    // < H >
    // V ~ V
    // < H >
    Lineup Box( N Col( 3 ), corn, hseg, corn, vseg, cent, vseg, corn, hseg, corn, vseg, cent, vseg, corn, hseg, corn ),
    Spacer Box( size( digitspace, 1 ) ) // space to right of digit
);
dot = Spacer Box( size( 1.5 * segwidth, 1.5 * segwidth ), color( on ) );
gap = Spacer Box( size( segwidth, seglength ), color( off ) );
colon = V Center Box( H List Box( Spacer Box( size( digitspace, 1 ) ), V List Box( dot, gap, dot ), Spacer Box( size( digitspace, 1 ) ) ) );
decoder = [// which segs are on?
0 0 1 0 0 1 0,  // 1
1 0 1 1 1 0 1, // 2
1 0 1 1 0 1 1, // 3
0 1 1 1 0 1 0, // 4
1 1 0 1 0 1 1, // 5
1 1 0 1 1 1 1, // 6
1 0 1 0 0 1 0, // 7
1 1 1 1 1 1 1, // 8
1 1 1 1 0 1 1, // 9
1 1 1 0 1 1 1  // 0
];

segIdx = [2 4 6 8 10 12 14];// segment subscripts in each digit

setdigit = Function( {digit, value},
    {LB, iseg, decodeIdx = If( value == 0, 10, value ), codes = decoder[decodeIdx, 0]},
    For( iseg = 1, iseg <= 7, iseg++,
        LB = digit[Lineup Box( 1 )];
        LB[Spacer Box( segIdx[iseg] )] << color( If( codes[iseg], on, off ) );
    );
);

New Window( "7 segment",
    Border Box( Left( padding ), Right( padding ), top( padding ), bottom( padding ),
        H List Box(
            hour10 = digit << clonebox,
            hour1 = digit << clonebox,
            colon,
            minute10 = digit << clonebox,
            minute1 = digit << clonebox,
            colon,
            second10 = digit << clonebox,
            second1 = digit << clonebox
        ),
        <<backgroundcolor( off )
    )
);

target = 1jan2018; // new years eve count down if within 99 hours

lastSecond = -1;
While( 1,
    Wait( .05 );
    now = Today();
    If( target > now > target - In Hours( 100 ),
        time = target - now;
        h = Floor( time / 3600 ); // for the countdown the hours can be >23
        time = time - h * 3600; // adjust the time for the minutes and seconds
    , // else
        time = now;
        h = Hour( time ); // counting up, hours does not include days, etc
    );
    m = Minute( time );
    s = Second( time );
    If( s != lastSecond,
        lastSecond = s;
        setdigit( hour10, Floor( h / 10 ) );
        setdigit( hour1, Mod( h, 10 ) );
        setdigit( minute10, Floor( m / 10 ) );
        setdigit( minute1, Mod( m, 10 ) );
        setdigit( second10, Floor( s / 10 ) );
        setdigit( second1, Mod( s, 10 ) );
    );
);

Discussion

I think this countdown clock will revert to a normal 24-hour clock when it is not within 99 hours of the target time. It is a bit hard to test.

See Also

https://en.wikipedia.org/wiki/Seven-segment_display

JSL Cookbook

If you’re looking for a code snippet or design pattern that performs a common task for your JSL project, the JSL Cookbook is for you.

This knowledge base contains building blocks of JSL code that you can use to reduce the amount of coding you have to do yourself.

It's also a great place to learn from the experts how to use JSL in new ways, with best practices.