global [backgroundlight count targetcount] global [song-loop-on song-pop-on] to jack-in-the-box ; number of turns before jack pops (must be > 1) settargetcount 12 setbackgroundlight sensor 0 setcount 0 setsong-loop-on 0 when [sees-shadow?] [ setcount (count + 1) print count ; if still need more turns to reach target ifelse (count < targetcount) [ ; start the music on the first count if (count = 1) [ play-song-loop ; start the song on the first turn ] ] [ ; else target has been reached stop-song-loop ; stop the song pop-jack ; pop! goes the weasel wait 50 setcount 0 ; reset the count to start over again ] wait 5 ] end ; test if the crank is covering the light sensor to sees-shadow? output ((sensor 0) - backgroundlight) > 40 end to pop-jack launch [ pop-music ] a, onfor 40 thisway onfor 30 thatway stoprules ; stop pop-music end to play-song-loop setsong-loop-on 1 forever [ pop-goes-weasel ] end to stop-song-loop setsong-loop-on 0 end to pop-goes-weasel waituntil [song-loop-on] play_note 64 5 play_note 64 3 play_note 66 5 play_note 66 3 play_note 68 2 play_note 71 2 play_note 68 2 play_note 64 2 play_note 64 5 play_note 64 2 play_note 66 2 play_note 66 2 play_note 68 5 play_note 64 2 play_note 64 2 play_note 64 2 play_note 66 2 play_note 66 2 play_note 68 2 play_note 71 2 play_note 68 2 play_note 64 2 end to pop-music note 88 5 note 66 3 note 69 2 note 66 5 note 64 3 end to play_note :freq :dur if ( song-loop-on) [ note :freq :dur ] end