Thanks to Miles Berry, Principal lecturer and subject leader for Computing Education at the University of Roehampton, and author of Switched on Computing for this month’s coding challenge!
Apple’s latest gadget, the Apple Watch, has attracted a lot of interest, joining a number of other ‘smart’ watches on the market. One of its distinctive features is the variety of different watch ‘faces’, shown on its diminutive screen, which can be used with the same time-keeping mechanism. With the attention paid to telling the time on the new maths curriculum, this month’s coding challenge is for pupils to create their own clock using MIT’s Scratch language.
Making a clock in Scratch is a big enough problem that it’s worth decomposing it into smaller parts. Whilst there are many approaches pupils could take, the simplest is probably to create an abstraction of the clock mechanism to tick over seconds, minutes and hours, and then create a clock face to show the time. Pupils can then do some iterative development to add alternative faces or other features to their clock. This is probably quite close to the way Apple would have worked in developing their watch.Step 1: Making the clock mechanism
For this, pupils will need three variables: seconds, minutes and then hours. Each of these can be set to zero initially.
Pupils can create a simple iteration loop, using the wait block to tick over one second at a time until the number hits 60, and then setting this back to zero:
They can then deal with the minutes, so that every time the seconds hit zero, the minutes increase by 1, again repeating until that hits 60, when it too gets reset to zero:
The idea for the hours is very similar, so that every time the minutes get reset, the hours increase by 1, until they hit 24, when they too get reset to zero:
Give pupils some time to play with this, checking that they have it working as it should and particularly that the tick over between minutes, hours and days works correctly. They might like to use the slide mode for the variable display (0-59 and 0-23) to experiment with this.Step 2: Designing the clock face and hands
Encourage pupils to experiment with some designs for a simple, effective clock face that they’ll be able to design in Scratch. You might like to let them explore clock design using filtered web search tools such as Google Safe Search.
Pupils will need to decompose
their design into its different parts: the face itself, and then the hour, minute and second hands. Things will be much
simpler later if the ‘centre’ of each hand sprite is at the end that will be in the centre of the clock, with the hand pointing off to the right, eg:
There’s opportunity here for pupils to provide constructive, critical feedback to one another on their designs. As with so much design, simplicity is often the key to success.Step 3: Driving the hands
Pupils need to think really carefully about exactly how the hands on their clock move. Typically the second and minute hands are easy, as they’ll just point in the direction appropriate for the time, moving every second and every minute respectively. The hour hand is harder, as this usually moves between the different numbers on the face, turning a small amount every minute.
For the three hands, pupils are likely to need commands that look something like this, but it’s more fun if they work these out for themselves!
Again, pupils will need to spend some time checking that their hands work exactly as they should, using logical reasoning
to debug their programs as necessary.Step 4: Using the real time
Once pupils have got the clock working as it should, it’s easy enough to set it to the correct time, simply using the current hour, minute and second sensing blocks to initialise the three variables.Step 5: Developing further
Encourage pupils to think of ways they could develop their clock further. For example:
- How accurate is it? Can they think of ways in which it could be made more accurate?
- Could they make a digital clock, perhaps creating sprite costumes to match those of a LED or LCD seven-segment display?
- Could they make a countdown timer or a stop watch?
- Could they make an alarm clock?
- What about a speaking clock for a visually impaired user?
There’s a working version at https://scratch.mit.edu/projects/28742256/
if you’d like to experiment with this for yourself.Let us know how you get on!
We would love to see some examples of your work. Post them on the blog or tweet us a link @switchedoncomp
Have you seen our new Learn to Code Practice Books?
They are engaging, full-colour pupil books to help children at Key Stage 2 learn to code with confidence! Find out more and download a sample.
TagsComputing and ICT
, Switched on Computing: Learn to Code Practice Books
, Switched on Computing