|
| 1 | +// SPDX-FileCopyrightText: 2018 Collin Cunningham for Adafruit Industries |
| 2 | +// |
| 3 | +// SPDX-License-Identifier: MIT |
| 4 | + |
| 5 | +/* MIDI Solenoid Drummer |
| 6 | + * for use with Adafruit Feather + Crickit Featherwing |
| 7 | + * assumes a 5V solenoid connected to each of Crickit's four Drive ports |
| 8 | + */ |
| 9 | + |
| 10 | +#include "Adafruit_Crickit.h" |
| 11 | +#include "MIDIUSB.h" |
| 12 | + |
| 13 | +Adafruit_Crickit crickit; |
| 14 | + |
| 15 | +#define NUM_DRIVES 4 |
| 16 | +int drives[] = {CRICKIT_DRIVE1, CRICKIT_DRIVE2, CRICKIT_DRIVE3, CRICKIT_DRIVE4}; |
| 17 | +int cym = CRICKIT_DRIVE4; |
| 18 | +int kick = CRICKIT_DRIVE3; |
| 19 | +int snare = CRICKIT_DRIVE2; |
| 20 | +int shake = CRICKIT_DRIVE1; |
| 21 | +int hitDur = 8; //solenoid on duration for each hit (in milliseconds) |
| 22 | + |
| 23 | +void setup() { |
| 24 | + |
| 25 | + if (!crickit.begin()) { |
| 26 | + while (1); |
| 27 | + } |
| 28 | + |
| 29 | + for (int i = 0; i < NUM_DRIVES; i++) |
| 30 | + crickit.setPWMFreq(drives[i], 1000); //default frequency is 1khz |
| 31 | + |
| 32 | + test(); //test solenoids at start |
| 33 | +} |
| 34 | + |
| 35 | +void loop() { |
| 36 | + |
| 37 | + midiEventPacket_t rx = MidiUSB.read(); //listen for new MIDI messages |
| 38 | + |
| 39 | + switch (rx.header) { |
| 40 | + case 0x9: //Note On message |
| 41 | + handleNoteOn( |
| 42 | + rx.byte1 & 0xF, //channel |
| 43 | + rx.byte2, //pitch |
| 44 | + rx.byte3 //velocity |
| 45 | + ); |
| 46 | + break; |
| 47 | + default: |
| 48 | + break; |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +void handleNoteOn(byte channel, byte pitch, byte velocity) { |
| 53 | + |
| 54 | + switch (pitch) { |
| 55 | + case 24: //kick = C1/24 |
| 56 | + hit(kick); |
| 57 | + break; |
| 58 | + case 25: //snare = C#1/25 |
| 59 | + hit(snare); |
| 60 | + break; |
| 61 | + case 26: //shake = D1/26 |
| 62 | + hit(shake); |
| 63 | + break; |
| 64 | + case 27: //cymbal = D#1/27 |
| 65 | + hit(cym); |
| 66 | + break; |
| 67 | + default: |
| 68 | + break; |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +void hit(int drum) { |
| 73 | + crickit.analogWrite(drum, CRICKIT_DUTY_CYCLE_MAX); //turn solenoid all the way on |
| 74 | + delay(hitDur); // wait |
| 75 | + crickit.analogWrite(drum, CRICKIT_DUTY_CYCLE_OFF); //turn solenoid all the way off |
| 76 | +} |
| 77 | + |
| 78 | +void test() { //for debugging |
| 79 | + hit(cym); |
| 80 | + delay(400); |
| 81 | + hit(kick); |
| 82 | + delay(400); |
| 83 | + hit(snare); |
| 84 | + delay(400); |
| 85 | + hit(shake); |
| 86 | + delay(400); |
| 87 | +} |
0 commit comments