Saturday 14 October 2017

Microbit Line Tracking - hidden parameters

Every day of this past week BT Adastral Park have been running a Crumblebot based line tracking event for Primary and Secondary schools in Suffolk and adjacent counties. That has meant over 100 children per day having great fun while reinforcing some of the crucial parts of the KS2 Computer Science curriculum.

With my CAS hub leader and STEM Ambassador hats on I volunteer at these events in support of the great work that Pam Popay, Jill Southgate and all the BT volunteers put into delivering a great set of instructive and practical days.

As a code club leader at one of the school's attending, Woodbridge Primary School, we are beginning to do some work with microbits and have a few at the club so I thought it would be interesting to implement line tracking using a microbit based robot. I purchased the Kitronik bot which uses visible light instead of infrared to help "show" the principle of operation. Having built the bot and tested the sensitivity of the LDRs some minor tweaking was necessary to pull up the output of the comparator op amps to trigger the digital threshold on the microbit. Having completed the build it was on to a bit of coding..........

In Code Club we are using the Java blocks editor so I used this to code the line following script that Kitronik supply with their build instructions.

Their script uses the LEDs to show which direction the bot is travelling in for each of the detector states (very useful) which I coded using the basic show led block:


What I failed to spot was the additional parameter lurking at the end of the script's show led code; a subtle ",0" which is really important. This parameter is not an option in the above block and what is even less obvious is that the block adds a default 400ms pause in operation. Not a lot of good when following a line!

To overcome this but keep using block based coding it is necessary to convert the block code to javascript and add the ,0 into the led command:


basic.forever(() => {
if (true) {
basic.showLeds(`
. . # . .
. # # # .
# . # . #
. . # . .
. . # . .
`,0)
} else {
    
}
})

Then convert the code back to blocks which results in a script statement that cannot be converted to a block (a bit like a line of assembler in BASIC) but which has the 0 millisecond pause set ....


and results in a line tracking algorithm that works.







No comments:

Post a Comment