Created
November 29, 2018 19:34
-
-
Save nbogie/91eb01462042710ea757e5ce265b1da1 to your computer and use it in GitHub Desktop.
Rough first funky solenoid drummer for micro:bit with adafruit's crickit board. It just plays a hardcoded rhythm with random variation. Needs https://makecode.microbit.com/beta and the adafruit crickit extension.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
input.onButtonPressed(Button.A, function () { | |
playRhythmLooped(funkyDrummer, 16, 90); | |
}) | |
input.onButtonPressed(Button.B, function () { | |
for (let i = 0; i < 200; i++) { | |
driveNum = Math.randomRange(1, 4) | |
strike(driveNum); | |
crickit.setPixelColor([16711680, 65280, 255, 16776960][driveNum - 1]) | |
basic.pause(100) | |
} | |
}) | |
function noStrike() { | |
basic.pause(16) | |
} | |
function playRhythmOnce(rhythm: number[][], pauseMs: number) { | |
rhythm.forEach(function (info) { | |
let drumNumber: number; | |
if (info.length === 1) { | |
drumNumber = info[0]; | |
} | |
else { | |
drumNumber = Math.randomRange(0, 100) < info[1] ? | |
info[0] : (info.length === 3 ? info[2] : 0); | |
} | |
if (drumNumber > 0) { | |
strike(drumNumber); | |
crickit.setPixelColor([80, 16711680, 255, 16776960][drumNumber - 1]); | |
} else { | |
noStrike(); | |
//crickit.setPixelOff(); | |
} | |
basic.pause(pauseMs); | |
}); | |
} | |
function playRhythmLooped(rhythm: number[][], numRepeats: number, pauseMs: number) { | |
for (let r = 0; r < numRepeats; r++) { | |
playRhythmOnce(rhythm, pauseMs); | |
} | |
} | |
function strike(driveNum: number) { | |
let d = getDrive(driveNum); | |
d.analogWrite(1023); | |
basic.pause(8) | |
d.analogWrite(0); | |
basic.pause(8) | |
} | |
function getDrive(driveNum: number) { | |
return [ | |
crickit.drive1, | |
crickit.drive2, | |
crickit.drive3, | |
crickit.drive4][driveNum - 1]; | |
} | |
function randomDrive() { | |
return getDrive(Math.randomRange(1, 4)); | |
} | |
let funkyDrummer: number[][] = [ | |
[2], [1], [2, 50], [1], | |
[3], [3, 15, 1], [1], [3], | |
[1], [3], [2], [2, 90], | |
[3], [1], [1], [3, 30, 1]] | |
let driveNum = 0 | |
basic.showLeds(` | |
# # . . . | |
. . # # . | |
. . . . . | |
. # # # # | |
. # # # # | |
`) | |
basic.pause(500); | |
basic.clearScreen(); | |
[1, 2, 3, 4].forEach(function (dn) { getDrive(dn).setFrequency(1000); }); | |
crickit.setPixelColor(0x008000); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment