JodyTriOmega
Published

Project 3 Simon Says Play Piano

This is a musical practice instrument to practice hearing and repeating a sequence of notes.

IntermediateProtip120
Project 3 Simon Says Play Piano

Things used in this project

Hardware components

Circuit Playground Express
Adafruit Circuit Playground Express
×1
Alligator Clips
Alligator Clips
×1
Li-Ion Battery 1000mAh
Li-Ion Battery 1000mAh
×1

Hand tools and fabrication machines

Box, General Purpose
Box, General Purpose
Pool Noodle Black

Story

Read more

Code

Keyboard

JavaScript
input.touchA1.onEvent(ButtonEvent.Click, function () {
    if (isOn) {
        irCode = 0
        network.infraredSendNumber(irCode)
    }
})
input.touchA2.onEvent(ButtonEvent.Click, function () {
    if (isOn) {
        irCode = 1
        network.infraredSendNumber(irCode)
    }
})
input.touchA3.onEvent(ButtonEvent.Click, function () {
    if (isOn) {
        irCode = 2
        network.infraredSendNumber(irCode)
    }
})
input.touchA4.onEvent(ButtonEvent.Click, function () {
    if (isOn) {
        irCode = 3
        network.infraredSendNumber(irCode)
    }
})
input.touchA5.onEvent(ButtonEvent.Click, function () {
    if (isOn) {
        irCode = 4
        network.infraredSendNumber(irCode)
    }
})
input.touchA6.onEvent(ButtonEvent.Click, function () {
    if (isOn) {
        irCode = 5
        network.infraredSendNumber(irCode)
    }
})
input.touchA7.onEvent(ButtonEvent.Click, function () {
    if (isOn) {
        irCode = 6
        network.infraredSendNumber(irCode)
    }
})
let irCode = 0
let isOn = false
pins.A0.setPull(PinPullMode.PullUp)
light.setBrightness(5)
light.showRing(
`blue white blue white blue white blue white blue white`
)
forever(function () {
    while (!(pins.A0.digitalRead())) {
        isOn = true
        network.infraredSendNumber(101)
        light.showRing(
        `blue white blue white blue white blue white blue white`
        )
    }
    isOn = false
    network.infraredSendNumber(100)
    light.clear()
})

Microphone

JavaScript
function checkPlayerMelody () {
    correctNotes = 7
    for (let index = 0; index <= 6; index++) {
        if (playersInput[index] == melody[index]) {
            music.baDing.playUntilDone()
            music.rest(music.beat(BeatFraction.Breve))
        } else {
            correctNotes += -1
            if (playersInput[index] < melody[index]) {
                music.playMelody("B C5 B C5 - - - - ", 200)
            } else {
                music.playMelody("D C D C - - - - ", 200)
            }
        }
    }
    if (correctNotes == 7) {
        inputCounter = 0
        playersInput = []
        input.setLoudSoundThreshold(255)
        music.magicWand.playUntilDone()
        music.rest(music.beat(BeatFraction.Breve))
        makeNewMelody()
        for (let value of melody) {
            playNoteList(value)
        }
        input.setLoudSoundThreshold(130)
    } else {
        inputCounter = 0
        playersInput = []
        input.setLoudSoundThreshold(255)
        music.wawawawaa.playUntilDone()
        input.setLoudSoundThreshold(130)
    }
}
function makeNewMelody () {
    list = []
    melody = [Math.randomRange(0, 7), Math.randomRange(0, 7), Math.randomRange(0, 7), Math.randomRange(0, 7), Math.randomRange(0, 7), Math.randomRange(0, 7), Math.randomRange(0, 7)]
}
function playNoteList (noteNum: number) {
    if (noteNum == 0) {
        music.playTone(262, music.beat(BeatFraction.Double))
    } else if (noteNum == 1) {
        music.playTone(294, music.beat(BeatFraction.Double))
    } else if (noteNum == 2) {
        music.playTone(330, music.beat(BeatFraction.Double))
    } else if (noteNum == 3) {
        music.playTone(349, music.beat(BeatFraction.Double))
    } else if (noteNum == 4) {
        music.playTone(392, music.beat(BeatFraction.Double))
    } else if (noteNum == 5) {
        music.playTone(440, music.beat(BeatFraction.Double))
    } else if (noteNum == 6) {
        music.playTone(494, music.beat(BeatFraction.Double))
    } else if (noteNum == 7) {
        light.showAnimation(light.theaterChaseAnimation, 2000)
    } else {
        music.siren.playUntilDone()
    }
}
function trackPlayerInput (playersNote: number) {
    playersInput[inputCounter] = playersNote
    inputCounter += 1
    if (inputCounter >= 7) {
        checkPlayerMelody()
    }
}
input.onLoudSound(function () {
    if (isOn) {
        light.showAnimation(light.runningLightsAnimation, 2000)
        light.showRing(
        `red red purple red red red red purple red red`
        )
        trackPlayerInput(7)
    }
})
network.onInfraredReceivedNumber(function (irCode) {
    if (irCode == 0) {
        music.playTone(262, music.beat(BeatFraction.Half))
        trackPlayerInput(0)
    } else if (irCode == 1) {
        music.playTone(294, music.beat(BeatFraction.Half))
        trackPlayerInput(1)
    } else if (irCode == 2) {
        music.playTone(330, music.beat(BeatFraction.Half))
        trackPlayerInput(2)
    } else if (irCode == 3) {
        music.playTone(349, music.beat(BeatFraction.Half))
        trackPlayerInput(3)
    } else if (irCode == 4) {
        music.playTone(392, music.beat(BeatFraction.Half))
        trackPlayerInput(4)
    } else if (irCode == 5) {
        music.playTone(440, music.beat(BeatFraction.Half))
        trackPlayerInput(5)
    } else if (irCode == 6) {
        music.playTone(494, music.beat(BeatFraction.Half))
        trackPlayerInput(6)
    } else if (irCode == 100) {
        isOn = false
    } else if (irCode == 101) {
        if (isOn == false) {
            isOn = true
            inputCounter = 0
            playersInput = []
            input.setLoudSoundThreshold(255)
            for (let value of melody) {
                playNoteList(value)
            }
            input.setLoudSoundThreshold(130)
        }
    } else {
        music.siren.play()
    }
})
let list: number[] = []
let playersInput: number[] = []
let correctNotes = 0
let melody: number[] = []
let inputCounter = 0
let isOn = false
isOn = false
inputCounter = 0
makeNewMelody()
for (let index = 0; index <= 6; index++) {
    console.log(melody[index])
}
light.setBrightness(3)
music.setVolume(175)
forever(function () {
    while (isOn) {
        light.showRing(
        `red red purple red red red red purple red red`
        )
    }
    light.clear()
    music.stopAllSounds()
})

Credits

Jody
4 projects • 0 followers
Contact
TriOmega
4 projects • 0 followers
Contact

Comments

Please log in or sign up to comment.