Exploring Functions through Music

Exploring Functions through Music

This week our Year 7 Robotics (13 year old) students explored Functions using the mBot and Apple's Swift programming language.

Functions are a seemingly abstract topic which isn't covered in Mathematics curriculum until the end of high school. However, students simplified the process by analysing the structure of a song and coming to the conclusion that by grouping sequential statements into a function they can produced ideal outputs, over and over again.

The video on the right shows the result of students trying to re-create The Imperial March from Starwars using the speakers on their mBot and Swift.  The code to create this yourself along with the link the the mBot with Swift Playground coding curriculum are shown below.


For this code to work you'll need to download the Swift Playground book from the following subscription link.

func firstSection(){
    playSound(tone: .a4, meter: .half)
    playSound(tone: .a4, meter: .half)
    playSound(tone: .f4, meter: .quarter)
    playSound(tone: .c5, meter: .eighth)
    playSound(tone: .a4, meter: .half)
    playSound(tone: .f4, meter: .quarter)
    playSound(tone: .c5, meter: .eighth)
    playSound(tone: .a4, meter: .whole)
        
    wait(duration: 0.5)
        
    playSound(tone: .e5, meter: .half)
    playSound(tone: .e5, meter: .half)
    playSound(tone: .e5, meter: .half)
    playSound(tone: .f5, meter: .quarter)
    playSound(tone: .c5, meter: .eighth)
    playSound(tone: .a4, meter: .half)
    playSound(tone: .f4, meter: .quarter)
    playSound(tone: .c5, meter: .eighth)
    playSound(tone: .a4, meter: .whole)
        
    wait(duration: 0.5)
}
    
func secondSection(){
    playSound(tone: .a5, meter: .half)
    playSound(tone: .a4, meter: .quarter)
    playSound(tone: .a4, meter: .eighth)
    playSound(tone: .a5, meter: .half)
    playSound(tone: .l4, meter: .half)
    playSound(tone: .g5, meter: .eighth)
    playSound(tone: .l3, meter: .eighth)
    playSound(tone: .f5, meter: .eighth)
    playSound(tone: .l3, meter: .quarter)
        
    wait(duration: 0.325)
        
    playSound(tone: .a4, meter: .quarter)
    playSound(tone: .l2, meter: .half)
    playSound(tone: .d5, meter: .half)
    playSound(tone: .l1, meter: .eighth)
    playSound(tone: .c5, meter: .eighth)
    playSound(tone: .a4, meter: .eighth)
    playSound(tone: .c5, meter: .quarter)
        
    wait(duration: 0.325)
}
    
    
//Play first section
firstSection()

//Play second section
secondSection()
    
//Variant 1
playSound(tone: .f4, meter: .quarter)
playSound(tone: .g4, meter: .half) //415?
playSound(tone: .f4, meter: .quarter)
playSound(tone: .a4, meter: .eighth)
playSound(tone: .c5, meter: .half)
playSound(tone: .a4, meter: .quarter)
playSound(tone: .c5, meter: .half)
playSound(tone: .e5, meter: .half)
    
wait(duration: 0.5)
    
//repeat Second section
secondSection()

//Variant 2
playSound(tone: .f4, meter: .quarter)
playSound(tone: .g4, meter: .half) //415
playSound(tone: .f4, meter: .quarter) //375
playSound(tone: .c5, meter: .eighth)
playSound(tone: .a4, meter: .half)
playSound(tone: .f4, meter: .quarter) //375
playSound(tone: .c5, meter: .eighth)
playSound(tone: .a4, meter: .whole)
    
wait(duration: 0.650)
Color Switch with Swift Playgrounds

Color Switch with Swift Playgrounds

Animate with Keynote & Swift Playgrounds

Animate with Keynote & Swift Playgrounds