Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Problem 1003. Make a KITT-scanner on the command line

Created by J-G van der Toorn

Did you know that you can actually remove characters from the command-line window? Just send a 'backspace' character to the output, e.g. with fprintf(char(8)); (ASCII code 8 is a backspace), and the last character is removed. This way, you can write a line, and remove it afterwards, by sending as many backspace characters as the line was long. However, you only move the cursor back, so when you want to clear a line, you have to move first the cursor to its beginning, then overwrite the line with blanks (spaces, ' '), and then move the cursor back once more. E.g. with

 fprintf(repmat([char(8) ' ' char(8)],lenght_of_last_line,1));

Your task is to program a KITT-scanner (the red sweeping light in front of the black car in the TV-series Knight Rider) on the command line. The bar has a width specified by the first parameter L of the function. The characters which the bar is made of are specified in the 2nd parameter, S, of the function. The first character of S is the actual light. The last character is 'background', and the tail of the light is defined by the characters in between. For example

 kitt(10,'#=~-')

tells it to show up as

 '#=~-------'

It starts with the light on the left, tail to the right, and the light moving to the right in the next step. So,

 '=#--------'

followed by

 '~=#-------'

and

 '-~=#------'

and so on. You see, the headlight supersedes the tail, when the head and tail overlap. Otherwise, the characters just show up with in the order defined in S. The 'frame rate' of the scanner should be 1/2 Hz, or one sweep back and forth in 2 seconds, but this can not be checked by Cody. But you are encouraged to watch the result on your own screen. To check your code, the function should output a character array with the full sequence, until the first step is repeated, with every row a step in the sequence (including the repeated last step). And off course, try to avoid just hard-coding the result.

Tags

Problem Group

Solution Statistics

5 correct solutions 8 incorrect solutions
Last solution submitted on Sep 24, 2013

Problem Comments